Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Fixed #441.

Nikita Nikishina676017fe463
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'], '[email protected]')
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=['[email protected]'], 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>')
+ self.assertEqual(msg.get('Content-Type'), 'text/html')
def test_send_attach(self):
attach = StringIO()