0

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 about related, 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 <[email protected]>")
m.addRecipient("[email protected]")
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', '[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'
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?

4
  • So to send 'a rather complicated message' you had to do rather complicated things. That isn't really suprising. Commented Jan 30, 2013 at 9:46
  • Yes, the wording used was not the best. I improved the explanation to try to better illustrate the point. Commented Jan 30, 2013 at 10:12
  • 1
    Stack Overflow is not a good place for this question. If you actually want to know why there's no high-level e-mail support in the standard library (and are not just complaining about it) then you're best off asking the developers on python-dev. Or if you would like pyzmail to be added to the standard library, raise an issue. Complaining here is unlikely to get you anywhere. Commented Jan 30, 2013 at 11:45
  • @gareth-rees Actually, that's a good answer, with actionable advice. Commented Jan 30, 2013 at 13:13

2 Answers 2

2

I think that the question is more about the complexity of the structure of an email message rather than the weakness of the smpt lib in Python.

This example in PHP doesn't seem more simple than your Python example.

Sign up to request clarification or add additional context in comments.

Comments

1
import smtplib
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail("[email protected]", ["[email protected]"],
    """Subject: This is a message sent with very little configuration.

    It will assume that the mailserver is on localhost on the default port
    (25) and also assume a message type of text/plain.

    Of course, if you want more complex message types and/or server
    configuration, it kind of stands to reason that you'd need to do
    more complex message assembly. Email (especially with embedded content)
    is actually a rather complex area with many possible options.
    """
smtp.quit()

5 Comments

Your demonstration would not be complete without a simple file attachment.
An example would need to also add an HTML version for the message, and include a couple of attached files or images.
@DReispt Maybe you should change your question then to be more specific.
@DReispt I think you missed the point this answer was making, which is that the definition of "high level support" is by no means objective.
Comments taken. I improved the question to better explain what I actually intended to ask.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.