Merge pull request #2331 from moisesguimaraes/fixes-2272

[MRG+1] Fixes issue #2272 using arg_to_iter() to wrap single values and list() to…
This commit is contained in:
Mikhail Korobov 2016-12-07 20:08:18 +05:00 committed by GitHub
commit ff3aec6613
3 changed files with 22 additions and 12 deletions

View File

@ -35,12 +35,6 @@ And here is how to use it to send an e-mail (without attachments)::
mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@example.com"])
.. note::
As shown in the example above, ``to`` and ``cc`` need to be lists
of email addresses, not single addresses, and even for one recipient,
i.e. ``to="someone@example.com"`` will not work.
MailSender class reference
==========================
@ -87,13 +81,13 @@ uses `Twisted non-blocking IO`_, like the rest of the framework.
Send email to the given recipients.
:param to: the e-mail recipients
:type to: list
:type to: str or list of str
:param subject: the subject of the e-mail
:type subject: str
:param cc: the e-mails to CC
:type cc: list
:type cc: str or list of str
:param body: the e-mail body
:type body: str

View File

@ -21,6 +21,8 @@ else:
from twisted.internet import defer, reactor, ssl
from .utils.misc import arg_to_iter
logger = logging.getLogger(__name__)
@ -48,6 +50,10 @@ class MailSender(object):
msg = MIMEMultipart()
else:
msg = MIMENonMultipart(*mimetype.split('/', 1))
to = list(arg_to_iter(to))
cc = list(arg_to_iter(cc))
msg['From'] = self.mailfrom
msg['To'] = COMMASPACE.join(to)
msg['Date'] = formatdate(localtime=True)

View File

@ -10,7 +10,8 @@ class MailSenderTest(unittest.TestCase):
def test_send(self):
mailsender = MailSender(debug=True)
mailsender.send(to=['test@scrapy.org'], subject='subject', body='body', _callback=self._catch_mail_sent)
mailsender.send(to=['test@scrapy.org'], subject='subject', body='body',
_callback=self._catch_mail_sent)
assert self.catched_msg
@ -24,9 +25,16 @@ class MailSenderTest(unittest.TestCase):
self.assertEqual(msg.get_payload(), 'body')
self.assertEqual(msg.get('Content-Type'), 'text/plain')
def test_send_single_values_to_and_cc(self):
mailsender = MailSender(debug=True)
mailsender.send(to='test@scrapy.org', subject='subject', body='body',
cc='test@scrapy.org', _callback=self._catch_mail_sent)
def test_send_html(self):
mailsender = MailSender(debug=True)
mailsender.send(to=['test@scrapy.org'], subject='subject', body='<p>body</p>', mimetype='text/html', _callback=self._catch_mail_sent)
mailsender.send(to=['test@scrapy.org'], subject='subject',
body='<p>body</p>', mimetype='text/html',
_callback=self._catch_mail_sent)
msg = self.catched_msg['msg']
self.assertEqual(msg.get_payload(), '<p>body</p>')
@ -90,7 +98,8 @@ class MailSenderTest(unittest.TestCase):
mailsender = MailSender(debug=True)
mailsender.send(to=['test@scrapy.org'], subject=subject, body=body,
attachs=attachs, charset='utf-8', _callback=self._catch_mail_sent)
attachs=attachs, charset='utf-8',
_callback=self._catch_mail_sent)
assert self.catched_msg
self.assertEqual(self.catched_msg['subject'], subject)
@ -99,7 +108,8 @@ class MailSenderTest(unittest.TestCase):
msg = self.catched_msg['msg']
self.assertEqual(msg['subject'], subject)
self.assertEqual(msg.get_charset(), Charset('utf-8'))
self.assertEqual(msg.get('Content-Type'), 'multipart/mixed; charset="utf-8"')
self.assertEqual(msg.get('Content-Type'),
'multipart/mixed; charset="utf-8"')
payload = msg.get_payload()
assert isinstance(payload, list)