Monday, December 3, 2012

How to Send Mail in Python

There are several use cases how you can send an email message using Python:
  1. Plain Mail: an email message with plain text or html content.
  2. Mail with Attachment: an email message with attached document.
  3. Alternative Mail Views: you provide a convenient way to email recipients to view your message in plain text or html with optional rich content including images, etc.
Here is an example of alternative email with HTML and image:
Let see how this can be accomplished. But first setup virtual environment:
$ virtualenv env
$ env/bin/pip install wheezy.core

Plain Mail

An email message with plain text or html content. Place the following into file plain.py:
from wheezy.core.mail import MailMessage
from wheezy.core.mail import SMTPClient


mail = MailMessage(
    subject='Welcome to Python',
    content='Hello World!',
    from_addr='someone@dev.local',
    to_addrs=['you@dev.local'])

client = SMTPClient()
client.send(mail)
You can send this out with the following command:
env/bin/python plain.py
If you need to send a HTML message just replace content with HTML, set content_type to 'text/html' and charset per encoding of your message.
from wheezy.core.mail import MailMessage
from wheezy.core.mail import SMTPClient


content = """\
<html><body>
    <h1>Hello World!</h1>
</body></html>"""
mail = MailMessage(
    subject='Welcome to Python',
    content=content,
    content_type='text/html',
    charset='utf-8',
    from_addr='someone@dev.local',
    to_addrs=['you@dev.local'])

client = SMTPClient()
client.send(mail)
Optional parameters to SMTPClient let you set host, port, use of tls, credentials, etc.

Mail with Attachment

An email message with attached document. Place the following into file attachment.py:
from wheezy.core.mail import Attachment
from wheezy.core.mail import MailMessage
from wheezy.core.mail import SMTPClient


mail = MailMessage(
    subject='Welcome to Python',
    content='Hello World!',
    from_addr='someone@dev.local',
    to_addrs=['you@dev.local'])
mail.attachments.append(Attachment(
    name='welcome.txt',
    content='Hello World!'))

client = SMTPClient()
client.send(mail)
You can use factory method Attachment.from_file to create an instance of attachment for a file from filesystem.

Alternative Mail Views

You provide a convenient way to email recipients to view your message in plain text or html with optional rich content including images, etc. Let download python logo as an image for our HTML mail view:
wget http://www.python.org/images/python-logo.gif
Place the following into file alternative.py:
import os.path

from wheezy.core.mail import Alternative
from wheezy.core.mail import MailMessage
from wheezy.core.mail import Related
from wheezy.core.mail import SMTPClient


mail = MailMessage(
    subject='Welcome to Python',
    content='Hello World!',
    from_addr='someone@dev.local',
    to_addrs=['you@dev.local'])

alt = Alternative("""\
<html><body>
    <h1>Hello World!</h1>
    <p><img src="cid:python-logo.gif" /></p>
</body></html>""", content_type='text/html')

curdir = os.path.dirname(__file__)
path = os.path.join(curdir, 'python-logo.gif')
alt.related.append(Related.from_file(path))

mail.alternatives.append(alt)

client = SMTPClient()
client.send(mail)
Recipients of your email message who prefer plain text will be able to see plain text version, while users who has rich mail client capable to display HTML will see your message formatted and with an image.
The source code is here.

1 comment :

  1. If you want to avoid importing and installing a 3rd-party package (say if you're not using wheezy): https://code.activestate.com/recipes/578472-sending-email-in-python/?in=user-4167757

    ReplyDelete