mirror of
https://github.com/scrapy/scrapy.git
synced 2025-02-25 05:24:31 +00:00
Merge pull request #602 from Blender3D/email
Added a mimetype parameter to `MailSender.send`
This commit is contained in:
commit
822adb1d5f
@ -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:
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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='<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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user