https://docs.djangoproject.com/en/1.4/topics/email/#the-emailmessage-class
这是发送电子邮件的更 django'y 方式:
# attempt to send out a welcome email
try :
t = loader.get_template('email_templates/membership/register.html')
c = Context({
'user' : user,
'site' : Site.objects.get(id=settings.SITE_ID)
})
msg = EmailMessage('Welcome to Site', t.render(c), settings.EMAIL_HOST_USER, to=[user.email,])
msg.content_subtype = "html"
msg.send()
except :
messages.info(request, _(u'Our email servers are encountering technical issues, you may not recieve a welcome email.'))
在我的 settings.py 中:
import os
EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
EMAIL_HOST= 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = os.environ['SENDGRID_PASSWORD']
注意:SENDGRID_USERNAME 和 SENDGRID_PASSWORD 是由 heroku 插件添加为环境变量的,您可能在设置文件中嵌入了实际凭据,这很好。
那么为什么您的电子邮件没有抛出异常?https://docs.djangoproject.com/en/1.4/topics/email/#django.core.mail.get_connection
fail_silently 参数控制后端应如何处理错误。如果 fail_silently 为 True,则电子邮件发送过程中的异常将被静默忽略。