Error passing Tuple as 'To' param in Django EmailMessage
I am getting the following error when trying to send an email message where I give the 'To' parameter a tuple of email addresses.
> TypeError: sequence item 0: expected
> string, tuple found
I have looked at the Django documentation for the EmailMessage class and it indicates this should be fine. Anyone have any suggestions about what could be going wrong?
I construct the EmailMessage object like so:
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()
and
settings.MAIL_LIST = ["foo@bar.com", "foo2@bar.com", "foo3@bar.com"]
Partial Stack trace:
> 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
Send HTML email example
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()
This ended up being a non-question because my information above is actually incorrect. My error was pretty much what the error message describes. I was passing a list of string email addresses into code wanting a list of tuples.
精彩评论