Skip to main content

Elegant and simple email library for python 2/3

Project description

Modern email handling in python.

m = emails.Message(html=T("<html><p>Build passed: {{ project_name }} <img src='cid:icon.png'> ..."),
                   text=T("Build passed: {{ project_name }} ..."),
                   subject=T("Passed: {{ project_name }}#{{ build_id }}"),
                   mail_from=("CI", "ci@mycompany.com"))
m.attach(filename="icon.png", content_disposition="inline", data=open("icon.png"))
response = m.send(render={"project_name": "user/project1", "build_id": 121},
                  to='somebody@mycompany.com',
                  smtp={"host":"mx.mycompany.com", "port": 25})

if response.status_code not in [250, ]:
    # message is not sent, retry later
    ...

See the same code, without Emails.

Emails code is not much simpler than the same code in django, but it is still more elegant, can be used in django environment and has html transformation methods (see HTML Transformer section).

Features

  • HTML-email message abstraction

  • Method to transform html body:

    • css inlining (using peterbe’s premailer)

    • image inlining

  • DKIM signature

  • Message loaders

  • Send directly or via django email backend

Examples

Create message:

import emails
message = emails.html(html=open('letter.html'),
                      subject='Friday party',
                      mail_from=('Company Team', 'contact@mycompany.com'))

Attach files or inline images:

message.attach(data=open('event.ics'), filename='Event.ics')
message.attach(data=open('image.png'), filename='image.png',
               content_disposition='inline')

Use templates:

from emails.template import JinjaTemplate as T

message = emails.html(subject=T('Payment Receipt No.{{ billno }}'),
                      html=T('<p>Dear {{ name }}! This is a receipt...'),
                      mail_from=('ABC', 'robot@mycompany.com'))

message.send(to=('John Brown', 'jbrown@gmail.com'),
             render={'name': 'John Brown', 'billno': '141051906163'})

Add DKIM signature:

message.dkim(key=open('my.key'), domain='mycompany.com', selector='newsletter')

Generate email.message or rfc822 string:

m = message.as_message()
s = message.as_string()

Send and get response from smtp server:

r = message.send(to=('John Brown', 'jbrown@gmail.com'),
                 render={'field1': 'X'},
                 smtp={'host':'smtp.mycompany.com', 'port': 465, 'ssl': True})
assert r.status_code == 250

Django

DjangoMessage helper sends via django configured email backend:

from emails.django_ import DjangoMessage as Message
message = Message(...)
message.send(mail_to=('John Brown', 'jbrown@gmail.com'),
             context={'field1': 'X'})

Flask

For flask integration take a look at flask-emails

HTML transformer

Message HTML body can be modified with ‘transformer’ object:

>>> message = emails.Message(html="<img src='promo.png'>")
>>> message.transformer.apply_to_images(func=lambda src, **kw: 'http://mycompany.tld/images/'+src)
>>> message.transformer.save()
>>> message.html
u'<html><body><img src="http://mycompany.tld/images/promo.png"></body></html>'

Code example to make images inline:

>>> message = emails.Message(html="<img src='promo.png'>")
>>> message.attach(filename='promo.png', data=open('promo.png'))
>>> message.attachments['promo.png'].is_inline = True
>>> message.transformer.synchronize_inline_images()
>>> message.transformer.save()
>>> message.html
u'<html><body><img src="cid:promo.png"></body></html>'

Loaders

python-emails ships with couple of loaders.

Load message from url:

import emails.loader
message = emails.loader.from_url(url="http://xxx.github.io/newsletter/2015-08-14/index.html")

Load from zipfile or directory:

message = emails.loader.from_zipfile(open('design_pack.zip'))
message = emails.loader.from_directory('/home/user/design_pack')

Zipfile and directory loaders require at least one html file (with “html” extension).

Install

Install from pypi:

$ [sudo] pip install emails

Install on Ubuntu from PPA:

$ [sudo] add-apt-repository ppa:lavrme/python-emails-ppa
$ [sudo] apt-get update
$ [sudo] apt-get install python-emails

TODO

  • Documentation

  • Increase test coverage

  • Feature: load message from rfc2822

  • Feature: export message to directory or zipfile

  • Performance: Patch pydkim for performance: i.e. preload key only once

  • Distribution: deb package (debianization example)

  • Distribution: rpm package

  • Other: Flask extension

  • Feature: ESP integration - Amazon SES, SendGrid, …

How to Help

Library is under development and contributions are welcome.

  1. Open an issue to start a discussion around a bug or a feature.

  2. Fork the repository on GitHub and start making your changes to a new branch.

  3. Write a test which shows that the bug was fixed.

  4. Send a pull request. Make sure to add yourself to AUTHORS.

See also

There are plenty other python email-around libraries that may fit your needs:

https://travis-ci.org/lavr/python-emails.png?branch=master https://img.shields.io/pypi/v/emails.svg http://allmychanges.com/p/python/emails/badge/ https://coveralls.io/repos/lavr/python-emails/badge.svg?branch=master

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

emails-0.4.1.tar.gz (42.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

emails-0.4.1-py2.py3-none-any.whl (57.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file emails-0.4.1.tar.gz.

File metadata

  • Download URL: emails-0.4.1.tar.gz
  • Upload date:
  • Size: 42.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for emails-0.4.1.tar.gz
Algorithm Hash digest
SHA256 c9c2fb267e71813ea6b62ae527a1eea55cb8c184c24511f41d62289fa767cf1f
MD5 b57c77fea418b0979fbf17579e9ab463
BLAKE2b-256 08d1a7b4bd16b1493ae3483b1836951e6e856a978e9db7b68144e7941b29f921

See more details on using hashes here.

File details

Details for the file emails-0.4.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for emails-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b7916e3df0b69b50d791bc04f3088fbd8982bba12aec96f8393ae210f85f9139
MD5 6c39b2b4d9166fe4a1ad934aea9a5ede
BLAKE2b-256 731ea9cbcd5215e080799e12638fe1a3f349a50dfd21ac962d41201737d40708

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page