我正在通过SMTP API安排电子邮件。这就是我现在尝试过的:
from smtpapi import SMTPAPIHeader
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.template import Template, Context
def campaign_email(lg_user, template):
user = lg_user.id
email = user.email
fname = lg_user.id.first_name
lname = lg_user.id.last_name
mobile = lg_user.contact_no
purchase = lg_user.active_user_subjects.values_list('subject', flat=True)
expiry = str(lg_user.active_user_subjects.values_list('at_expiry', flat=True))
filename = '/tmp/campaign_mailer.html'
opened_file = open(filename, "r").read()
temp = Template(opened_file)
c = Context({'fname': fname, 'lname': lname, 'subject': subject, 'email': email,
'mobile': mobile, 'purchase': purchase, 'expiry': expiry})
header = SMTPAPIHeader()
html_content = temp.render(c)
send_at = {"send_at": 1472058300}
header.set_send_at(send_at)
msg = EmailMultiAlternatives(subject, html_content, sender, [email],
headers={'X-SMTPAPI': header.json_string()})
msg.attach_alternative(html_content, "text/html")
msg.send(fail_silently=True)
为了检查,我的标题(打印header.json_string()上的标题解析为:
{
"send_at": {
"send_at": 1472051700
}
}
)是否有效,我检查了https://sendgrid.com/docs/Utilities/smtpapi_validator.html,结果证明它完全有效。
但是我从sendgrid的支持部门收到的失败邮件说明了失败的原因:send_at必须是时间戳。我相信,在documentation中,明确指出时间戳应该是UNIX格式 - 这是我作为send_at键的值提供的。
那么,我该如何解决这个错误?
答案 0 :(得分:1)
set_send_at()
采用整数参数,但您传递的是字典({"send_at": 1472058300}
)。这是无效的并导致错误。
将其更改为:
header.set_send_at(1472058300)