通过 sendgrid 从我的 django 网站发送电子邮件
Sending emails from my django website, via sendgrid
我想向在我的网站上注册的所有用户发送帐户确认电子邮件,因此它必须是一封包含激活 link 的 HTML 电子邮件。这就是我正在尝试的方式:
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
def my_view(request):
current_site = get_current_site(request)
mail_subject = 'Welcome To IIITU Alumni Network'
to_email = email
ctx = {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
}
message = Mail(
to_emails = to_email,
from_email = '<my email address>',
subject = mail_subject,
#I made this html page specifically for sending confirmation email
#I am not sure if this is the correct way to do it
html_content = render_to_string('network/email.html', ctx)
)
try:
#I have this key as an environment variable
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
#Now this is where the problem is
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
return HttpResponse("A confirmation Email has been sent to your Institute email address. Please Confirm Your email to complete your registration")
except Exception as e:
print(e.message)
return HttpResponse("The Server seems too busy!! Sorry for the inconvenience. Please try again later, or contact administrator if this problem persists.")
呈现此特定视图时,它会抛出以下错误消息:
'UnauthorizedError' object has no attribute 'message'
因为这不会通过 SMTP 服务器发送电子邮件,根据我的理解,我不需要向我的 settings.py 文件添加任何内容(我确实将 'sendgrid' 添加到我安装的我使用 pip 安装后的应用程序)有人可以指出我做错了什么吗?我已经坚持了一段时间了...
感谢您的宝贵时间
我认为这一行出错了。
...
except Exception as e:
print(e.message)
...
无论发生什么异常,它都没有消息 属性。
两种解决方案。
print(e)
删除 try except 块并尝试找出发生的异常,然后专门捕获它。
我想向在我的网站上注册的所有用户发送帐户确认电子邮件,因此它必须是一封包含激活 link 的 HTML 电子邮件。这就是我正在尝试的方式:
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
def my_view(request):
current_site = get_current_site(request)
mail_subject = 'Welcome To IIITU Alumni Network'
to_email = email
ctx = {
'user': user,
'domain': current_site.domain,
'uid':urlsafe_base64_encode(force_bytes(user.pk)),
'token':account_activation_token.make_token(user),
}
message = Mail(
to_emails = to_email,
from_email = '<my email address>',
subject = mail_subject,
#I made this html page specifically for sending confirmation email
#I am not sure if this is the correct way to do it
html_content = render_to_string('network/email.html', ctx)
)
try:
#I have this key as an environment variable
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
#Now this is where the problem is
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
return HttpResponse("A confirmation Email has been sent to your Institute email address. Please Confirm Your email to complete your registration")
except Exception as e:
print(e.message)
return HttpResponse("The Server seems too busy!! Sorry for the inconvenience. Please try again later, or contact administrator if this problem persists.")
呈现此特定视图时,它会抛出以下错误消息:
'UnauthorizedError' object has no attribute 'message'
因为这不会通过 SMTP 服务器发送电子邮件,根据我的理解,我不需要向我的 settings.py 文件添加任何内容(我确实将 'sendgrid' 添加到我安装的我使用 pip 安装后的应用程序)有人可以指出我做错了什么吗?我已经坚持了一段时间了...
感谢您的宝贵时间
我认为这一行出错了。
...
except Exception as e:
print(e.message)
...
无论发生什么异常,它都没有消息 属性。
两种解决方案。
print(e)
删除 try except 块并尝试找出发生的异常,然后专门捕获它。