flask-mail Documentation



Similar documents
gs. Documentation

Flask-SSO Documentation

smtplib SMTP protocol client

socketio Documentation

Network Services. SMTP, Internet Message Format. Johann Oberleitner SS 2006

Using Caché Internet Utilities

Result: Adds an X-header named "X-Company" with the value of "Your Company Name"

Interactive Reporting er Manual

Server-side Development using Python and SQL

Settings 1 September 2015

CS43: Computer Networks . Kevin Webb Swarthmore College September 24, 2015

Flask Documentation. Release

Pyak47 - Performance Test Framework. Release 1.2.1

Mail User Agent Project

s sent to the FaxFinder fax server must meet the following criteria to be processed for sending as a fax:

Management CSCU9B2 CSCU9B2 1

Sending MIME Messages in LISTSERV DISTRIBUTE Jobs

Internet Technology 2/13/2013

FTP and . Computer Networks. FTP: the file transfer protocol

Networking Applications

. MIME is the protocol that was devised to allow non-ascii encoded content in an and attached files to an .

SerialMailer Manual. For SerialMailer 7.2. Copyright Falko Axmann. All rights reserved.

SendMIME Pro Installation & Users Guide

Dove User Guide Copyright Virgil Trasca

Utility Classes - Overview. Version 14 service pack 9. Dalestech Ltd. Page 1 of 23

History. Originally written for Apache Turbine Extracted from Turbine in 2001 Release of version 1.0 in 2005 Currently working on version 1.

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

DaimlerChrysler EBMX HTTP/s Quick Start Guide

# Constructors $smtp = Net::SMTP->new('mailhost'); $smtp = Net::SMTP->new('mailhost', Timeout => 60);

Administration Guide. BlackBerry Resource Kit for BES12. Version 12.1

Bitrix Site Manager 4.0. Quick Start Guide to Newsletters and Subscriptions

latest Release 0.2.6

Protocolo FTP. FTP: Active Mode. FTP: Active Mode. FTP: Active Mode. FTP: the file transfer protocol. Separate control, data connections

Evolution of the WWW. Communication in the WWW. WWW, HTML, URL and HTTP. HTTP Abstract Message Format. The Client/Server model is used:

C HAPTER E IGHTEEN T HE PGP, MAIL, AND CGI LIBRARIES. PGP Interface Library

ing from The E2 Shop System address Server Name Server Port, Encryption Protocol, Encryption Type, SMTP User ID SMTP Password

CPSC Network Programming. , FTP, and NAT.

Administrator s and Developer s Guide

SpamPanel Reseller Level Manual 1 Last update: September 26, 2014 SpamPanel

Python DNS Failover Documentation

1. Seam Mail Introduction 2. Configuration 3. Core Usage 4. Templating 5. Advanced Features

Batch Processor INSTALL & USER GUIDELINES

DevKey Documentation. Release 0.1. Colm O Connor

Chapter 2 Application Layer. Lecture 5 FTP, Mail. Computer Networking: A Top Down Approach

Collax Mail Server. Howto. This howto describes the setup of a Collax server as mail server.

# Constructors $smtp = Net::SMTP->new('mailhost'); $smtp = Net::SMTP->new('mailhost', Timeout => 60);

Ipswitch Client Installation Guide

Installation and Administration Guide

Pulse Secure Client. Customization Developer Guide. Product Release 5.1. Document Revision 1.0. Published:

Open source framework for interactive data exploration in server based architecture

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Setting up and Automating a MS Dynamics AX Job in JAMS

Writing for Developers: The New Customers. Amruta Ranade

THE WEB. Messaging. In North America, over 76 million people own FEATURE

StreamServe Persuasion SP4 Connectors

MPP Manager Users Guide

Using Your New Webmail

128 CERT Exercises Toolset Document for students

Electronic Mail

versasrs HelpDesk quality of service

MARKETING AND TRACKING

Grinder Webtest Documentation

Oracle Java CAPS Binding Component User's Guide

CAPIX Job Scheduler User Guide

Once logged in you will have two options to access your e mails

Nokia for Business. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

Issue 2EN. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

Enterprise Vault.cloud. Microsoft Exchange Managed Folder Archiving Guide

2- Electronic Mail (SMTP), File Transfer (FTP), & Remote Logging (TELNET)

Bitrix Site Manager ASP.NET. Installation Guide

Send TLM. Table of contents

What browsers can I use to view my mail?

. Introduction. Set Up Sumac To Send

Slides from INF3331 lectures - web programming in Python

ETSI TS V2.1.1 ( ) Technical Specification

APACHE WEB SERVER. Andri Mirzal, PhD N

Sending an Message from a Process

requests_toolbelt Documentation

WS_FTP Professional 12

Listserv Mailing List Owner Quick Start Guide

SMTP Servers. Determine if an message should be sent to another machine and automatically send it to that machine using SMTP.

THE CHALLENGE OF ADMINISTERING WEBSITES OR APPLICATIONS THAT REQUIRE 24/7 ACCESSIBILITY

wce Outlook Contact Manager Documentation

citools Documentation

Virtual Fax API User Guide

SmarterMail User Guide

Configuration Manual. Version October 2012 File Transfer Daemon. Archive Digitization & Exploitation

1. Stem. Configuration and Use of Stem

Chapter 7: Configuring ScanMail emanager

POP3 Connector for Exchange - Configuration

Saruman Documentation

WESTERNACHER OUTLOOK -MANAGER OPERATING MANUAL

django-cron Documentation

APC by Schneider Electric Release Notes AP9537 Network Management Card. APC part number: Released: 26 October 2012

ALERT installation setup

Customization & Enhancement Guide. Table of Contents. Index Page. Using This Document

Outlook Express 6.0 Tutorial I

Administration Guide. BlackBerry Resource Kit for BES12. Version 12.3

Tuskar UI Documentation

Transcription:

flask-mail Documentation Release 0.9.1 Dan Jacob February 16, 2016

Contents 1 Links 3 2 Installing Flask-Mail 5 3 Configuring Flask-Mail 7 4 Sending messages 9 5 Bulk emails 11 6 Attachments 13 7 Unit tests and suppressing emails 15 8 Header injection 17 9 Signalling support 19 10 API 21 Python Module Index 23 i

ii

flask-mail Documentation, Release 0.9.1 One of the most basic functions in a web application is the ability to send emails to your users. The Flask-Mail extension provides a simple interface to set up SMTP with your Flask application and to send messages from your views and scripts. Contents 1

flask-mail Documentation, Release 0.9.1 2 Contents

CHAPTER 1 Links documentation source changelog 3

flask-mail Documentation, Release 0.9.1 4 Chapter 1. Links

CHAPTER 2 Installing Flask-Mail Install with pip and easy_install: pip install Flask-Mail or download the latest version from version control: git clone https://github.com/mattupstate/flask-mail.git cd flask-mail python setup.py install If you are using virtualenv, it is assumed that you are installing flask-mail in the same virtualenv as your Flask application(s). 5

flask-mail Documentation, Release 0.9.1 6 Chapter 2. Installing Flask-Mail

CHAPTER 3 Configuring Flask-Mail Flask-Mail is configured through the standard Flask config API. These are the available options (each is explained later in the documentation): MAIL_SERVER : default localhost MAIL_PORT : default 25 MAIL_USE_TLS : default False MAIL_USE_SSL : default False MAIL_DEBUG : default app.debug MAIL_USERNAME : default None MAIL_PASSWORD : default None MAIL_DEFAULT_SENDER : default None MAIL_MAX_EMAILS : default None MAIL_SUPPRESS_SEND : default app.testing MAIL_ASCII_ATTACHMENTS : default False In addition the standard Flask TESTING configuration option is used by Flask-Mail in unit tests (see below). Emails are managed through a Mail instance: from flask import Flask from flask_mail import Mail app = Flask( name ) mail = Mail(app) In this case all emails are sent using the configuration values of the application that was passed to the Mail class constructor. Alternatively you can set up your Mail instance later at configuration time, using the init_app method: mail = Mail() app = Flask( name ) mail.init_app(app) In this case emails will be sent using the configuration values from Flask s current_app context global. This is useful if you have multiple applications running in the same process but with different configuration options. 7

flask-mail Documentation, Release 0.9.1 8 Chapter 3. Configuring Flask-Mail

CHAPTER 4 Sending messages To send a message first create a Message instance: from flask_mail import Message @app.route("/") def index(): msg = Message("Hello", sender="from@example.com", recipients=["to@example.com"]) You can set the recipient emails immediately, or individually: msg.recipients = ["you@example.com"] msg.add_recipient("somebodyelse@example.com") If you have set MAIL_DEFAULT_SENDER you don t need to set the message sender explicity, as it will use this configuration value by default: msg = Message("Hello", recipients=["to@example.com"]) If the sender is a two-element tuple, this will be split into name and address: msg = Message("Hello", sender=("me", "me@example.com")) assert msg.sender == "Me <me@example.com>" The message can contain a body and/or HTML: msg.body = "testing" msg.html = "<b>testing</b>" Finally, to send the message, you use the Mail instance configured with your Flask application: mail.send(msg) 9

flask-mail Documentation, Release 0.9.1 10 Chapter 4. Sending messages

CHAPTER 5 Bulk emails Usually in a web application you will be sending one or two emails per request. In certain situations you might want to be able to send perhaps dozens or hundreds of emails in a single batch - probably in an external process such as a command-line script or cronjob. In that case you do things slightly differently: with mail.connect() as conn: for user in users: message = '...' subject = "hello, %s" % user.name msg = Message(recipients=[user.email], body=message, subject=subject) conn.send(msg) The connection to your email host is kept alive and closed automatically once all the messages have been sent. Some mail servers set a limit on the number of emails sent in a single connection. You can set the max amount of emails to send before reconnecting by specifying the MAIL_MAX_EMAILS setting. 11

flask-mail Documentation, Release 0.9.1 12 Chapter 5. Bulk emails

CHAPTER 6 Attachments Adding attachments is straightforward: with app.open_resource("image.png") as fp: msg.attach("image.png", "image/png", fp.read()) See the API for details. If MAIL_ASCII_ATTACHMENTS is set to True, filenames will be converted to an ASCII equivalent. This can be useful when using a mail relay that modify mail content and mess up Content-Disposition specification when filenames are UTF-8 encoded. The conversion to ASCII is a basic removal of non-ascii characters. It should be fine for any unicode character that can be decomposed by NFKD into one or more ASCII characters. If you need romanization/transliteration (i.e ß ss) then your application should do it and pass a proper ASCII string. 13

flask-mail Documentation, Release 0.9.1 14 Chapter 6. Attachments

CHAPTER 7 Unit tests and suppressing emails When you are sending messages inside of unit tests, or in a development environment, it s useful to be able to suppress email sending. If the setting TESTING is set to True, emails will be suppressed. Calling send() on your messages will not result in any messages being actually sent. Alternatively outside a testing environment you can set MAIL_SUPPRESS_SEND to True. This will have the same effect. However, it s still useful to keep track of emails that would have been sent when you are writing unit tests. In order to keep track of dispatched emails, use the record_messages method: with mail.record_messages() as outbox: mail.send_message(subject='testing', body='test', recipients=emails) assert len(outbox) == 1 assert outbox[0].subject == "testing" The outbox is a list of Message instances sent. The blinker package must be installed for this method to work. Note that the older way of doing things, appending the outbox to the g object, is now deprecated. 15

flask-mail Documentation, Release 0.9.1 16 Chapter 7. Unit tests and suppressing emails

CHAPTER 8 Header injection To prevent header injection attempts to send a message with newlines in the subject, sender or recipient addresses will result in a BadHeaderError. 17

flask-mail Documentation, Release 0.9.1 18 Chapter 8. Header injection

CHAPTER 9 Signalling support New in version 0.4. Flask-Mail now provides signalling support through a email_dispatched signal. This is sent whenever an email is dispatched (even if the email is not actually sent, i.e. in a testing environment). A function connecting to the email_dispatched signal takes a Message instance as a first argument, and the Flask app instance as an optional argument: def log_message(message, app): app.logger.debug(message.subject) email_dispatched.connect(log_message) 19

flask-mail Documentation, Release 0.9.1 20 Chapter 9. Signalling support

CHAPTER 10 API class flask_mail.mail(app=none) Manages email messaging Parameters app Flask instance connect() Opens a connection to the mail host. send(message) Sends a single message instance. If TESTING is True the message will not actually be sent. Parameters message a Message instance. send_message(*args, **kwargs) Shortcut for send(msg). Takes same arguments as Message constructor. Versionadded 0.3.5 class flask_mail.attachment(filename=none, content_type=none, data=none, disposition=none, headers=none) Encapsulates file attachment information. Versionadded 0.3.5 Parameters filename filename of attachment content_type file mimetype data the raw file data disposition content-disposition (if any) class flask_mail.connection(mail) Handles connection to host. send(message, envelope_from=none) Verifies and sends message. Parameters message Message instance. envelope_from Email address to be used in MAIL FROM command. 21

flask-mail Documentation, Release 0.9.1 send_message(*args, **kwargs) Shortcut for send(msg). Takes same arguments as Message constructor. Versionadded 0.3.5 class flask_mail.message(subject=, recipients=none, body=none, html=none, alts=none, sender=none, cc=none, bcc=none, attachments=none, reply_to=none, date=none, charset=none, extra_headers=none, mail_options=none, rcpt_options=none) Encapsulates an email message. Parameters subject email subject header recipients list of email addresses body plain text message html HTML message alts A dict or an iterable to go through dict() that contains multipart alternatives sender email sender address, or MAIL_DEFAULT_SENDER by default cc CC list bcc BCC list attachments list of Attachment instances reply_to reply-to address date send date charset message character set extra_headers A dictionary of additional headers for the message mail_options A list of ESMTP options to be used in MAIL FROM command rcpt_options A list of ESMTP options to be used in RCPT commands add_recipient(recipient) Adds another recipient to the message. Parameters recipient email address of recipient. attach(filename=none, content_type=none, data=none, disposition=none, headers=none) Adds an attachment to the message. Parameters filename filename of attachment content_type file mimetype data the raw file data disposition content-disposition (if any) 22 Chapter 10. API

Python Module Index f flask-mail, 3 flask_mail, 21 23

flask-mail Documentation, Release 0.9.1 24 Python Module Index

Index A add_recipient() (flask_mail.message method), 22 attach() (flask_mail.message method), 22 Attachment (class in flask_mail), 21 C connect() (flask_mail.mail method), 21 Connection (class in flask_mail), 21 F flask-mail (module), 1 flask_mail (module), 21 M Mail (class in flask_mail), 21 Message (class in flask_mail), 22 S send() (flask_mail.connection method), 21 send() (flask_mail.mail method), 21 send_message() (flask_mail.connection method), 21 send_message() (flask_mail.mail method), 21 25