From a676017fe4632c931fccc5fa9539ed4c23624fd4 Mon Sep 17 00:00:00 2001 From: Nikita Nikishin Date: Tue, 18 Feb 2014 03:50:43 -0500 Subject: [PATCH] Fixed #441. --- docs/topics/email.rst | 5 ++++- scrapy/mail.py | 4 ++-- scrapy/tests/test_mail.py | 11 ++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/topics/email.rst b/docs/topics/email.rst index a487299f1..2fc23472b 100644 --- a/docs/topics/email.rst +++ b/docs/topics/email.rst @@ -76,7 +76,7 @@ uses `Twisted non-blocking IO`_, like the rest of the framework. :param settings: the e-mail recipients :type settings: :class:`scrapy.settings.Settings` object - .. method:: send(to, subject, body, cc=None, attachs=()) + .. method:: send(to, subject, body, cc=None, attachs=(), mimetype='text/plain') Send email to the given recipients. @@ -99,6 +99,9 @@ uses `Twisted non-blocking IO`_, like the rest of the framework. contents of the attachment :type attachs: iterable + :param mimetype: the MIME type of the e-mail + :type mimetype: str + .. _topics-email-settings: diff --git a/scrapy/mail.py b/scrapy/mail.py index bedf06895..83dbaf763 100644 --- a/scrapy/mail.py +++ b/scrapy/mail.py @@ -35,11 +35,11 @@ class MailSender(object): settings['MAIL_PASS'], settings.getint('MAIL_PORT'), settings.getbool('MAIL_TLS'), settings.getbool('MAIL_SSL')) - def send(self, to, subject, body, cc=None, attachs=(), _callback=None): + def send(self, to, subject, body, cc=None, attachs=(), mimetype='text/plain', _callback=None): if attachs: msg = MIMEMultipart() else: - msg = MIMENonMultipart('text', 'plain') + msg = MIMENonMultipart(*mimetype.split('/', 1)) msg['From'] = self.mailfrom msg['To'] = COMMASPACE.join(to) msg['Date'] = formatdate(localtime=True) diff --git a/scrapy/tests/test_mail.py b/scrapy/tests/test_mail.py index 2a2cddabc..250ae04ae 100644 --- a/scrapy/tests/test_mail.py +++ b/scrapy/tests/test_mail.py @@ -1,6 +1,6 @@ -from cStringIO import StringIO import unittest +from cStringIO import StringIO from scrapy.mail import MailSender class MailSenderTest(unittest.TestCase): @@ -19,6 +19,15 @@ class MailSenderTest(unittest.TestCase): self.assertEqual(msg['to'], 'test@scrapy.org') self.assertEqual(msg['subject'], 'subject') self.assertEqual(msg.get_payload(), 'body') + self.assertEqual(msg.get('Content-Type'), 'text/plain') + + 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) + + msg = self.catched_msg['msg'] + self.assertEqual(msg.get_payload(), '

body

') + self.assertEqual(msg.get('Content-Type'), 'text/html') def test_send_attach(self): attach = StringIO()