From 45e95b79ce17eef0b3b4d324daa8b0d220fdb2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Tue, 18 Oct 2016 11:06:55 -0300 Subject: [PATCH 1/5] (fixes #2272) using arg_to_iter() to wrap single values and list() to avoid consuming from generators. --- scrapy/mail.py | 6 ++++++ tests/test_mail.py | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/scrapy/mail.py b/scrapy/mail.py index c6339f25b..283c3b8e1 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) From 3fb6e52457a7f24d580ed32c07aa12d9547d2970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Tue, 18 Oct 2016 12:24:11 -0300 Subject: [PATCH 2/5] fixes import for py35 env. --- scrapy/mail.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrapy/mail.py b/scrapy/mail.py index 283c3b8e1..0bb395521 100644 --- a/scrapy/mail.py +++ b/scrapy/mail.py @@ -21,7 +21,7 @@ else: from twisted.internet import defer, reactor, ssl -from utils.misc import arg_to_iter +from .utils.misc import arg_to_iter logger = logging.getLogger(__name__) From c58ea021b816f20fda1b522f80d8b6a1c22f2454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Sun, 4 Dec 2016 11:56:14 -0300 Subject: [PATCH 3/5] fixes docs --- docs/topics/email.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/topics/email.rst b/docs/topics/email.rst index 18d2f8084..2380a340c 100644 --- a/docs/topics/email.rst +++ b/docs/topics/email.rst @@ -87,13 +87,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 iterable :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 iterable :param body: the e-mail body :type body: str From a4178f99daf69e930fcc635421cc3a9ba519e36c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Mon, 5 Dec 2016 15:24:26 -0300 Subject: [PATCH 4/5] fixes params types in docs. --- docs/topics/email.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/topics/email.rst b/docs/topics/email.rst index 2380a340c..f68448276 100644 --- a/docs/topics/email.rst +++ b/docs/topics/email.rst @@ -87,13 +87,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: str or iterable + :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: str or iterable + :type cc: str or list of str :param body: the e-mail body :type body: str From c08d278c0c7549b3377df995cdf724e3fbb8c02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Mon, 5 Dec 2016 16:47:24 -0300 Subject: [PATCH 5/5] removes note from docs. --- docs/topics/email.rst | 6 ------ 1 file changed, 6 deletions(-) diff --git a/docs/topics/email.rst b/docs/topics/email.rst index f68448276..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 ==========================