在Django EmailMessage中将Tuple传递为'To'参数时出错

时间:2011-01-31 17:56:09

标签: python django email typeerror

我在尝试发送电子邮件时收到以下错误,其中我给'To'参数一个电子邮件地址元组。

> TypeError: sequence item 0: expected
> string, tuple found

我查看了EmailMessage class的Django文档,它表明这应该没问题。任何人都有什么可能出错的建议吗?

我像这样构建EmailMessage对象:

spam = EmailMessage("Some title - %s \"%s\"" % (subject, task.name), message,
                                "%s <%s>" % (user.get_full_name(), user.email), settings.MAIL_LIST)
spam.content_subtype = "html"
spam.send()

settings.MAIL_LIST = ["foo@bar.com", "foo2@bar.com", "foo3@bar.com"]

部分堆栈跟踪:

> File "/myClass/Mail.py", line 217, in
> contact_owner
>     spam.send()
> 
>   File
> "/port/python-environments/port_web/lib/python2.6/site-packages/django/core/mail.py",
> line 281, in send
>     return self.get_connection(fail_silently).send_messages([self])
> 
>   File
> "/port/python-environments/port_web/lib/python2.6/site-packages/django/core/mail.py",
> line 185, in send_messages
>     sent = self._send(message)
> 
>   File
> "/port/python-environments/port_web/lib/python2.6/site-packages/django/core/mail.py",
> line 199, in _send
>     email_message.message().as_string())
> 
>   File
> "/port/python-environments/port_web/lib/python2.6/site-packages/django/core/mail.py",
> line 253, in message
>     msg['To'] = ', '.join(self.to)
> 
> TypeError: sequence item 0: expected
> string, tuple found

2 个答案:

答案 0 :(得分:0)

发送HTML电子邮件示例

from django.core.mail import EmailMultiAlternatives
from django.utils.html import strip_tags

subject = render_to_string('cart/email/order_subject.txt',c)
html_body = t.render(Context(c))
text_body = strip_tags(html_body)

msg = EmailMultiAlternatives(subject, text_body, None, sc.settings['email_order'].split(','))
msg.attach_alternative(html_body, "text/html")
msg.send()

答案 1 :(得分:0)

这最终成为一个非问题因为我上面的信息实际上是不正确的。我的错误几乎是错误消息描述的内容。我将一个字符串电子邮件地址列表传递给需要元组列表的代码。