Note that there are some explanatory texts on larger screens.

plurals
  1. POPython and Twisted: making utf-8 html mail with message-id
    primarykey
    data
    text
    <p>I am trying to send emails to Gmail with Twisted, but having some strange problems.</p> <p>Firstly, <a href="http://findingscience.com/python/twisted/2011/01/22/sending-email-with-python-twisted.html" rel="nofollow" title="this">this</a> method works fine for simple text-only messages. But when I am tried to use this snippet with cStringIO data from <a href="http://www.askthepony.com/blog/2011/06/how-to-send-a-proper-unicode-encoded-email-using-python-2-7/" rel="nofollow" title="this article">this article</a>, I've got empty message without title and my e-mail as recipient.</p> <p>Now, I think, that I forgot something simple, but I've got nothing, trying to find answer today. So, final source with my problem is:</p> <pre><code>from twisted.internet import defer from twisted.mail import smtp, relaymanager from twisted.internet import reactor from cStringIO import StringIO from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header from email import Charset from email.generator import Generator from email.Utils import make_msgid MXCALCULATOR = relaymanager.MXCalculator() def getMailExchange(host): def cbMX(mxRecord): return str(mxRecord.name) return MXCALCULATOR.getMX(host).addCallback(cbMX) def sendEmail(mailFrom, mailTo, msg, subject=""): def dosend(host): print "emailing %s (using host %s) from %s" % (mailTo, host, mailFrom) html = u'&lt;html&gt;&lt;body&gt;You\'re registered. Now you should use site.&lt;/body&gt;&lt;/html&gt;' text = u'You\'re registered. Now you should use site.' # Override python's weird assumption that utf-8 text should be encoded with # base64, and instead use quoted-printable (for both subject and body). I # can't figure out a way to specify QP (quoted-printable) instead of base64 in # a way that doesn't modify global state. :-( Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') # This example is of an email with text and html alternatives. multipart = MIMEMultipart('alternative') # We need to use Header objects here instead of just assigning the strings in # order to get our headers properly encoded (with QP). # You may want to avoid this if your headers are already ASCII, just so people # can read the raw message without getting a headache. multipart['Subject'] = Header(subject.encode('utf-8'), 'UTF-8').encode() multipart['To'] = Header(mailTo.encode('utf-8'), 'UTF-8').encode() multipart['From'] = Header(mailFrom.encode('utf-8'), 'UTF-8').encode() multipart['Message-Id'] = Header(make_msgid('e_shop').encode('utf-8'), 'UTF-8').encode() # Attach the parts with the given encodings. htmlpart = MIMEText(html.encode('utf-8'), 'html', 'UTF-8') multipart.attach(htmlpart) textpart = MIMEText(text.encode('utf-8'), 'plain', 'UTF-8') multipart.attach(textpart) # And here we have to instantiate a Generator object to convert the multipart # object to a string (can't use multipart.as_string, because that escapes # "From" lines). io = StringIO() g = Generator(io, False) # second argument means "should I mangle From?" g.flatten(multipart) d = defer.Deferred() factory = smtp.ESMTPSenderFactory(None, None, mailFrom, mailTo, io, d, requireAuthentication=False, requireTransportSecurity=False) reactor.connectTCP(host, 25, factory) return d return getMailExchange(mailTo.split("@")[1]).addCallback(dosend) d = sendEmail('admin@mysite.me', 'myemail@gmail.com', 'template filename', 'this is a test subject') d.addCallback(lambda _: reactor.stop()) reactor.run() </code></pre> <p>What should I change to make it work?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload