Docsity

Python-emails Documentation: Modern Email Handling in Python, Study Guides, Projects, Research of Advanced Computer Programming

Partial preview of the text

Download Python-emails Documentation: Modern Email Handling in Python and more Study Guides, Projects, Research Advanced Computer Programming in PDF only on Docsity!

python-emails Documentation

Release 0.5.

Sergey Lavrinenko

Oct 08, 2018

ii

python-emails Documentation, Release 0.5.

Modern email handling in python.

m = emails.Message(html=T("

Build passed: {{ project_name }} ..."), text=T("Build passed: {{ project_name }} ..."), subject=T("Passed: {{ project_name }}#{{ build_id }}"), mail_from=("CI", "[email protected]")) m.attach(filename="icon.png", content_disposition="inline", data=open("icon.png", "rb ˓→")) response = m.send(render={"project_name": "user/project1", "build_id": 121}, to='[email protected]', 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).

Contents 1

## CHAPTER 1

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

python-emails Documentation, Release 0.5.

4 Chapter 1. Features

python-emails Documentation, Release 0.5.

r = message.send(to=('John Brown', '[email protected]'), render={'name': 'John'}, smtp={'host':'smtp.mycompany.com', 'port': 465, 'ssl': True, 'user': ˓→'john', 'password': '***'}) assert r.status_code == 250

6 Chapter 2. Examples

CHAPTER 3

Django

DjangoMessage helper sends via django configured email backend:

from emails.django import DjangoMessage as Message message = Message(...) message.send(mail_to=('John Brown', '[email protected]'), context={'name': 'John'})

CHAPTER 4

Flask

For flask integration take a look at flask-emails

python-emails Documentation, Release 0.5.

10 Chapter 4. Flask

python-emails Documentation, Release 0.5.

12 Chapter 5. HTML transformer

CHAPTER 6

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', 'rb')) message = emails.loader.from_directory('/home/user/design_pack')

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

Load message from .eml file (experimental):

message = emails.loader.from_rfc822(open('message.eml').read())

CHAPTER 7

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

python-emails Documentation, Release 0.5.

16 Chapter 7. Install