diff --git a/docs/topics/email.rst b/docs/topics/email.rst index 18d2f8084..aac93a91a 100644 --- a/docs/topics/email.rst +++ b/docs/topics/email.rst @@ -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 diff --git a/scrapy/mail.py b/scrapy/mail.py index c6339f25b..0bb395521 100644 --- a/scrapy/mail.py +++ b/scrapy/mail.py @@ -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) diff --git a/tests/test_mail.py b/tests/test_mail.py index bd7e49621..b139e98d8 100644 --- a/tests/test_mail.py +++ b/tests/test_mail.py @@ -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='
body
', mimetype='text/html', _callback=self._catch_mail_sent) + mailsender.send(to=['test@scrapy.org'], subject='subject', + body='body
', mimetype='text/html', + _callback=self._catch_mail_sent) msg = self.catched_msg['msg'] self.assertEqual(msg.get_payload(), 'body
') @@ -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)