Scrapy, a fast high-level web crawling & scraping framework for Python.
Encode message using supplied charset
Paul Tremberth12c7628fcbcf
diff --git a/scrapy/mail.py b/scrapy/mail.pyindex 7365f25b7..7f237820f 100644--- a/scrapy/mail.py+++ b/scrapy/mail.py@@ -5,10 +5,13 @@ See documentation in docs/topics/email.rst"""import logging+try:+ from cStringIO import StringIO as BytesIO+except ImportError:+ from io import BytesIOimport sixfrom email.utils import COMMASPACE, formatdate-from io import BytesIOfrom six.moves.email_mime_multipart import MIMEMultipartfrom six.moves.email_mime_text import MIMETextfrom six.moves.email_mime_base import MIMEBase@@ -21,14 +24,6 @@ else:from twisted.internet import defer, reactor, ssl-try:- from twisted.mail.smtp import ESMTPSenderFactory-except ImportError:- """- twisted.mail.smtp was not available in- older versions of Twisted on Python 3- """-from .utils.misc import arg_to_iterlogger = logging.getLogger(__name__)@@ -96,7 +91,7 @@ class MailSender(object):'mailattachs': len(attachs)})return- dfd = self._sendmail(rcpts, msg.as_string())+ dfd = self._sendmail(rcpts, msg.as_string().encode(charset or 'utf-8'))dfd.addCallbacks(self._sent_ok, self._sent_failed,callbackArgs=[to, cc, subject, len(attachs)],errbackArgs=[to, cc, subject, len(attachs)])@@ -118,7 +113,9 @@ class MailSender(object):'mailattachs': nattachs, 'mailerr': errstr})def _sendmail(self, to_addrs, msg):- msg = BytesIO(msg.encode('utf-8'))+ # Import twisted.mail here because it is not available in python3+ from twisted.mail.smtp import ESMTPSenderFactory+ msg = BytesIO(msg)d = defer.Deferred()factory = ESMTPSenderFactory(self.smtpuser, self.smtppass, self.mailfrom, \to_addrs, msg, d, heloFallback=True, requireAuthentication=False, \