4

Is there an alternative email module in Python?

Like requests is a "HTTP library, written in Python, for human beings"

Handling unicode mails with the email module from the standard library is no fun.

5
  • 2
    @guetti That sounds like "for human beings" is your opinion about the ease of use of the standard email module. But we cannot give you advice based on opinions as input. So please edit your question and describe the requirements for the alternative module in measurable terms.
    – user416
    Oct 30, 2014 at 15:51
  • Seconding Jan, and giving you further hints: Please take a look at What is required for a question to contain "enough information"? for what else might either be missing, or otherwise improve your chances for good answers. Thanks!
    – Izzy
    Oct 30, 2014 at 15:53
  • 1
    @JanDoggen: I'm not sure how you interpreted "for human beings" as OP's description of the standard email module. He's using the catchphrase from requests, which is an improvement over standard Python modules. I'm totally new here, so I don't quite know what level of subjectiveness is acceptable, but it sounds to me as though OP is after something that is of a similar level of abstraction and ease of use as requests, but for e-mail instead of HTTP.
    – John Y
    Oct 30, 2014 at 22:16
  • I wrote "Handling unicode mails with the email module from the standard library is no fun.". I am not a native speaker. For me this is a requirement. Have you ever handled unicode with the python email module? If not, please comment on different questions.
    – guettli
    Oct 31, 2014 at 10:16
  • @john I should've said the NOT easy of use
    – user416
    Oct 31, 2014 at 11:05

2 Answers 2

5

I think you might be looking for pyzmail - from the examples:

import pyzmail

sender=(u'Me', '[email protected]')
recipients=[(u'Him', '[email protected]'), '[email protected]']
subject=u'the subject'
text_content=u'Bonjour aux Fran\xe7ais'
prefered_encoding='iso-8859-1'
text_encoding='iso-8859-1'

payload, mail_from, rcpt_to, msg_id=pyzmail.compose_mail(\
        sender, \
        recipients, \
        subject, \
        prefered_encoding, \
        (text_content, text_encoding), \
        html=None, \
        attachments=[('attached content', 'text', 'plain', 'text.txt', \
                      'us-ascii')])
smtp_host='smtp.gmail.com'
smtp_port=587
smtp_mode='tls'
smtp_login='[email protected]'
smtp_password='my.gmail.password'

ret=pyzmail.send_mail(payload, mail_from, rcpt_to, smtp_host, \
        smtp_port=smtp_port, smtp_mode=smtp_mode, \
        smtp_login=smtp_login, smtp_password=smtp_password)

if isinstance(ret, dict):
    if ret:
        print 'failed recipients:', ', '.join(ret.keys())
    else:
        print 'success'
else:
    print 'error:', ret

Not quite SMTP for humans but maybe getting there!

1
  • 2
    Thank you for this answer. I searched the web before asking here. Maybe I was blind. Again, thank you.
    – guettli
    Nov 2, 2014 at 20:38
1

Be sure to check envelopes as well!

from envelopes import Envelope, GMailSMTP

envelope = Envelope(
    from_addr=(u'[email protected]', u'From Example'),
    to_addr=(u'[email protected]', u'To Example'),
    subject=u'Envelopes demo',
    text_body=u"I'm a helicopter!"
)
envelope.add_attachment('/Users/bilbo/Pictures/helicopter.jpg')

# Send the envelope using an ad-hoc connection...
envelope.send('smtp.googlemail.com', login='[email protected]',
              password='password', tls=True)

# Or send the envelope using a shared GMail connection...
gmail = GMailSMTP('[email protected]', 'password')
gmail.send(envelope)

Note: The documentation says that it should be considered as beta. Use with caution.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.