设置“cc”从 Django 发送电子邮件时
Django 1.3 将向 EmailMessage
,这非常好。如何使用 Django 1.2 来模拟这一点?
首先,我尝试了以下操作:
headers = None
if form.cleaned_data['cc_sender']:
headers = {'Cc': sender} # `cc` argument added in Django 1.3
msg = EmailMultiAlternatives(subject, message, sender, recipients, headers=headers)
msg.attach_alternative(replace(convert(message)), 'text/html')
msg.send(fail_silently=False)
这正确设置了“抄送”标头,但实际上并未发送抄送副本。我查看了 SMTP.sendmail 寻找线索,它似乎将所有收件人作为单个参数(它没有单独的 to
、cc
和 bcc
参数)。
接下来我尝试了这个:
headers = None
if form.cleaned_data['cc_sender']:
headers = {'Cc': sender} # `cc` argument added in Django 1.3
recipients.append(sender) # <-- added this line
msg = EmailMultiAlternatives(subject, message, sender, recipients, headers=headers)
msg.attach_alternative(replace(convert(message)), 'text/html')
msg.send(fail_silently=False)
这有效,但意味着当我点击“回复”(至少在 Gmail 中)时,两个地址都会出现在“收件人”字段中。我还尝试设置“Reply-To”标头(发送者),但这没有什么区别。
必须可以“抄送”地址,而无需将该地址包含在直接收件人中。我该怎么做呢?
Django 1.3 will add a "cc" argument to EmailMessage
, which is excellent. How would one mimic this using Django 1.2?
First, I tried this:
headers = None
if form.cleaned_data['cc_sender']:
headers = {'Cc': sender} # `cc` argument added in Django 1.3
msg = EmailMultiAlternatives(subject, message, sender, recipients, headers=headers)
msg.attach_alternative(replace(convert(message)), 'text/html')
msg.send(fail_silently=False)
This correctly set the "Cc" header but did not actually send the carbon copy. I looked at SMTP.sendmail for clues, and it appears to take all the recipients as a single argument (it doesn't have separate to
, cc
, and bcc
arguments).
Next I tried this:
headers = None
if form.cleaned_data['cc_sender']:
headers = {'Cc': sender} # `cc` argument added in Django 1.3
recipients.append(sender) # <-- added this line
msg = EmailMultiAlternatives(subject, message, sender, recipients, headers=headers)
msg.attach_alternative(replace(convert(message)), 'text/html')
msg.send(fail_silently=False)
This worked, but meant that when I hit "reply" (in Gmail, at any rate) both addresses appeared in the "To" field. I tried also setting the "Reply-To" header (to sender
), but this made no difference.
It must be possible to "cc" an address without also including the address among the direct recipients. How would I do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(3)
EmailMultiAlternatives 有一个 BCC kwarg,我在包装函数中使用它来自动密件抄送所有出站通信的记录电子邮件帐户。
from django.core.mail import EmailMultiAlternatives
def _send(to, subject='', text_content='', html_content='', reply_to=None):
if not isinstance(to, (list, tuple)):
to = (to,)
kwargs = dict(
to=to,
from_email='%s <%s>' % ('Treatful', settings.EMAIL_HOST_USER),
subject=subject,
body=text_content,
alternatives=((html_content, 'text/html'),)
)
if reply_to:
kwargs['headers'] = {'Reply-To': reply_to}
if not settings.DEBUG:
kwargs['bcc'] = (settings.RECORDS_EMAIL,)
message = EmailMultiAlternatives(**kwargs)
message.send(fail_silently=True)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
像您一样添加抄送:标头,另外将“bcc”关键字参数中的抄送地址列表传递给 EmailMessage 构造函数。这看起来有点违反直觉,但这样做的真正效果只是将抄送地址添加到收件人列表中,这正是您想要做的。 (如果您想了解有关标头和收件人列表之间差异的更多信息,有关 SMTP 的 Wikipedia 文章提供了一些很好的背景知识)。
Add the Cc: header just like you did, and additionally pass the list of CC addresses in the "bcc" keyword arg to the EmailMessage constructor. It seems a little counterintuitive but the real effect of this is simply to add the CC addresses to the recipients list, which is exactly what you want to do. (If you want to learn more about the difference between headers and the recipient list, the Wikipedia article on SMTP gives some nice background).