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 <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?

Community
  • 1
  • 1
Daniel Reis
  • 12,944
  • 6
  • 43
  • 71
  • So to send 'a rather complicated message' you had to do rather complicated things. That isn't really suprising. – Duncan Jan 30 '13 at 09:46
  • Yes, the wording used was not the best. I improved the explanation to try to better illustrate the point. – Daniel Reis Jan 30 '13 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](http://mail.python.org/mailman/listinfo/python-dev). Or if you would like `pyzmail` to be added to the standard library, [raise an issue](http://bugs.python.org/). Complaining here is unlikely to get you anywhere. – Gareth Rees Jan 30 '13 at 11:45
  • @gareth-rees Actually, that's a good answer, with actionable advice. – Daniel Reis Jan 30 '13 at 13:13

2 Answers2

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.

Community
  • 1
  • 1
luc
  • 41,928
  • 25
  • 127
  • 172
1
import smtplib
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail("me@somewhere.com", ["you@elsewhere.com"],
    """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()
Amber
  • 507,862
  • 82
  • 626
  • 550