Python is advertised as a "batteries included" language. So, I wonder why it's standard library doesn't include high level support for emails:
I found that you need to know lot about MIME to create an email message equivalent to what you can achieve in a typical email client, such as handling HTML content, embedded images and file attachments.
To achieve that you are required to do a low level assembly of the message, such as:
- handle
MIMEMultipart
sections, and know aboutrelated
,alternate
, etc. - know about file encodings, such as
base64
If you're just learning about MIME enough to assemble such an email, It's easy to fall into traps, such as bad section nesting, and create messages that may not be correctly viewed by some email clients.
I shouldn't need to know about MIME to be able to correctly send an e-mail. A high level library support should encapsulate all this MIME logic, allowing you to write something like this:
m = Email("mailserver.mydomain.com")
m.setFrom("Test User <test@mydomain.com>")
m.addRecipient("you@yourdomain.com")
m.setSubject("Hello there!")
m.setHtmlBody("The following should be <b>bold</b>")
m.addAttachment("/home/user/image.png")
m.send()
A non standard library solution is pyzmail
:
import pyzmail
sender=(u'Me', 'me@foo.com')
recipients=[(u'Him', 'him@bar.com'), 'just@me.com']
subject=u'the subject'
text_content=u'Bonjour aux Fran\xe7ais'
prefered_encoding='iso-8859-1'
text_encoding='iso-8859-1'
pyzmail.compose_mail(
sender, recipients,
subject, prefered_encoding, (text_content, text_encoding),
html=None,
attachments=[('attached content', 'text', 'plain', 'text.txt',
'us-ascii')])
Is there any reason for this not being in the "batteries included" standard library?