所以我使用 heroku 和 sendgrid 并认为我的 settings.py 配置正确:
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']
我正在使用这样的 send_mail 发送电子邮件(使用 pre_save 接收器来检查属性是否已更改...还将“myemail@gmail.com”替换为我的实际电子邮件):
@receiver(pre_save,sender=LotteryEntry)
def send_email_if_invited(sender,instance,**kwargs):
try:
obj = LotteryEntry.objects.get(pk=instance.pk)
except LotteryEntry.DoesNotExist:
#could send email here telling them when they should know
pass
else:
if not obj.invited == instance.invited:
l = Dinner.objects.get(id=instance.dinner.id)
u = User.objects.get(id=instance.user.id)
message = "You have been chosen to attend %s! Here is the description of the event: %s. We will see you there!" % (l.title,l.description)
send_mail('You have been invited!',message,'myemail@gmail.com',[u.email],fail_silently=False)
日志显示它正在发送..但我没有在我的收件箱中收到它,sendgrid 插件帐户部分也没有说我已经发送了电子邮件。以前有人有这个问题吗?
编辑:
这是我导入的 settings.py
import os
import sys
还有我的模型文件(send_mail 发生的地方):
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import *
from django.dispatch import receiver
from django.core.mail import send_mail
这是 send_mail 调用的日志:
Content-Type: text/plain; charset="utf-8"
2012-05-28T21:16:49+00:00 app[web.1]: MIME-Version: 1.0
2012-05-28T21:16:49+00:00 app[web.1]: Content-Transfer-Encoding: quoted-printable
2012-05-28T21:16:49+00:00 app[web.1]: Subject: You have been invited!
2012-05-28T21:16:49+00:00 app[web.1]: From: msencenb@stanford.edu
2012-05-28T21:16:49+00:00 app[web.1]: To: msencenb@stanford.edu
2012-05-28T21:16:49+00:00 app[web.1]: Date: Mon, 28 May 2012 21:16:49 -0000
2012-05-28T21:16:49+00:00 app[web.1]: Message-ID: <20120528211649.1.49209@2a33c2a2-9b5e-4da9-994c-15de8157702e>
2012-05-28T21:16:49+00:00 app[web.1]:
2012-05-28T21:16:49+00:00 app[web.1]: You have been chosen to attend Web Development Pt. 1! Here is the descripti=
2012-05-28T21:16:49+00:00 app[web.1]: on of the event: Knight Management Center, 5/23/12 12:30 to 2 PM. We will s=
2012-05-28T21:16:49+00:00 app[web.1]: ee you there!
2012-05-28T21:16:49+00:00 app[web.1]: -------------------------------------------------------------------------------
2012-05-28T21:16:49+00:00 app[web.1]: [28/May/2012 16:16:49] "POST /admin/dinners/lotteryentry/1/ HTTP/1.1" 302 0
2012-05-28T21:16:49+00:00 heroku[router]: POST morning-frost-2949.herokuapp.com/admin/dinners/lotteryentry/1/ dyno=web.1 queue=0 wait=0ms service=107ms status=302 bytes=0