.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "FLASK-MAIL" "1" "May 16, 2024" "0.9.1" "Flask-Mail" .SH NAME flask-mail \- Flask-Mail 0.9.1 .sp One of the most basic functions in a web application is the ability to send emails to your users. .sp The \fBFlask\-Mail\fP extension provides a simple interface to set up SMTP with your \X'tty: link http://flask.pocoo.org'\fI\%Flask\fP\X'tty: link' application and to send messages from your views and scripts. .SH LINKS .INDENT 0.0 .IP \(bu 2 \X'tty: link http://packages.python.org/Flask-Mail/'\fI\%documentation\fP\X'tty: link' .IP \(bu 2 \X'tty: link http://github.com/mattupstate/flask-mail'\fI\%source\fP\X'tty: link' .IP \(bu 2 \fI\%changelog\fP .UNINDENT .SH INSTALLING FLASK-MAIL .sp Install with \fBpip\fP and \fBeasy_install\fP: .INDENT 0.0 .INDENT 3.5 .sp .EX pip install Flask\-Mail .EE .UNINDENT .UNINDENT .sp or download the latest version from version control: .INDENT 0.0 .INDENT 3.5 .sp .EX git clone https://github.com/mattupstate/flask\-mail.git cd flask\-mail python setup.py install .EE .UNINDENT .UNINDENT .sp If you are using \fBvirtualenv\fP, it is assumed that you are installing flask\-mail in the same virtualenv as your Flask application(s). .SH CONFIGURING FLASK-MAIL .sp \fBFlask\-Mail\fP is configured through the standard Flask config API. These are the available options (each is explained later in the documentation): .INDENT 0.0 .IP \(bu 2 \fBMAIL_SERVER\fP : default \fB\(aqlocalhost\(aq\fP .IP \(bu 2 \fBMAIL_PORT\fP : default \fB25\fP .IP \(bu 2 \fBMAIL_USE_TLS\fP : default \fBFalse\fP .IP \(bu 2 \fBMAIL_USE_SSL\fP : default \fBFalse\fP .IP \(bu 2 \fBMAIL_DEBUG\fP : default \fBapp.debug\fP .IP \(bu 2 \fBMAIL_USERNAME\fP : default \fBNone\fP .IP \(bu 2 \fBMAIL_PASSWORD\fP : default \fBNone\fP .IP \(bu 2 \fBMAIL_DEFAULT_SENDER\fP : default \fBNone\fP .IP \(bu 2 \fBMAIL_MAX_EMAILS\fP : default \fBNone\fP .IP \(bu 2 \fBMAIL_SUPPRESS_SEND\fP : default \fBapp.testing\fP .IP \(bu 2 \fBMAIL_ASCII_ATTACHMENTS\fP : default \fBFalse\fP .UNINDENT .sp In addition the standard Flask \fBTESTING\fP configuration option is used by \fBFlask\-Mail\fP in unit tests (see below). .sp Emails are managed through a \fBMail\fP instance: .INDENT 0.0 .INDENT 3.5 .sp .EX from flask import Flask from flask_mail import Mail app = Flask(__name__) mail = Mail(app) .EE .UNINDENT .UNINDENT .sp In this case all emails are sent using the configuration values of the application that was passed to the \fBMail\fP class constructor. .sp Alternatively you can set up your \fBMail\fP instance later at configuration time, using the \fBinit_app\fP method: .INDENT 0.0 .INDENT 3.5 .sp .EX mail = Mail() app = Flask(__name__) mail.init_app(app) .EE .UNINDENT .UNINDENT .sp In this case emails will be sent using the configuration values from Flask\(aqs \fBcurrent_app\fP context global. This is useful if you have multiple applications running in the same process but with different configuration options. .SH SENDING MESSAGES .sp To send a message first create a \fBMessage\fP instance: .INDENT 0.0 .INDENT 3.5 .sp .EX from flask_mail import Message @app.route(\(dq/\(dq) def index(): msg = Message(\(dqHello\(dq, sender=\(dqfrom@example.com\(dq, recipients=[\(dqto@example.com\(dq]) .EE .UNINDENT .UNINDENT .sp You can set the recipient emails immediately, or individually: .INDENT 0.0 .INDENT 3.5 .sp .EX msg.recipients = [\(dqyou@example.com\(dq] msg.add_recipient(\(dqsomebodyelse@example.com\(dq) .EE .UNINDENT .UNINDENT .sp If you have set \fBMAIL_DEFAULT_SENDER\fP you don\(aqt need to set the message sender explicity, as it will use this configuration value by default: .INDENT 0.0 .INDENT 3.5 .sp .EX msg = Message(\(dqHello\(dq, recipients=[\(dqto@example.com\(dq]) .EE .UNINDENT .UNINDENT .sp If the \fBsender\fP is a two\-element tuple, this will be split into name and address: .INDENT 0.0 .INDENT 3.5 .sp .EX msg = Message(\(dqHello\(dq, sender=(\(dqMe\(dq, \(dqme@example.com\(dq)) assert msg.sender == \(dqMe \(dq .EE .UNINDENT .UNINDENT .sp The message can contain a body and/or HTML: .INDENT 0.0 .INDENT 3.5 .sp .EX msg.body = \(dqtesting\(dq msg.html = \(dqtesting\(dq .EE .UNINDENT .UNINDENT .sp Finally, to send the message, you use the \fBMail\fP instance configured with your Flask application: .INDENT 0.0 .INDENT 3.5 .sp .EX mail.send(msg) .EE .UNINDENT .UNINDENT .SH BULK EMAILS .sp 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. .sp In that case you do things slightly differently: .INDENT 0.0 .INDENT 3.5 .sp .EX with mail.connect() as conn: for user in users: message = \(aq...\(aq subject = \(dqhello, %s\(dq % user.name msg = Message(recipients=[user.email], body=message, subject=subject) conn.send(msg) .EE .UNINDENT .UNINDENT .sp The connection to your email host is kept alive and closed automatically once all the messages have been sent. .sp 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 \fBMAIL_MAX_EMAILS\fP setting. .SH ATTACHMENTS .sp Adding attachments is straightforward: .INDENT 0.0 .INDENT 3.5 .sp .EX with app.open_resource(\(dqimage.png\(dq) as fp: msg.attach(\(dqimage.png\(dq, \(dqimage/png\(dq, fp.read()) .EE .UNINDENT .UNINDENT .sp See the \fI\%API\fP for details. .sp If \fBMAIL_ASCII_ATTACHMENTS\fP is set to \fBTrue\fP, 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 \fIß\fP → \fIss\fP) then your application should do it and pass a proper ASCII string. .SH UNIT TESTS AND SUPPRESSING EMAILS .sp When you are sending messages inside of unit tests, or in a development environment, it\(aqs useful to be able to suppress email sending. .sp If the setting \fBTESTING\fP is set to \fBTrue\fP, emails will be suppressed. Calling \fBsend()\fP on your messages will not result in any messages being actually sent. .sp Alternatively outside a testing environment you can set \fBMAIL_SUPPRESS_SEND\fP to \fBFalse\fP\&. This will have the same effect. .sp However, it\(aqs still useful to keep track of emails that would have been sent when you are writing unit tests. .sp In order to keep track of dispatched emails, use the \fBrecord_messages\fP method: .INDENT 0.0 .INDENT 3.5 .sp .EX with mail.record_messages() as outbox: mail.send_message(subject=\(aqtesting\(aq, body=\(aqtest\(aq, recipients=emails) assert len(outbox) == 1 assert outbox[0].subject == \(dqtesting\(dq .EE .UNINDENT .UNINDENT .sp The \fBoutbox\fP is a list of \fBMessage\fP instances sent. .sp The blinker package must be installed for this method to work. .sp Note that the older way of doing things, appending the \fBoutbox\fP to the \fBg\fP object, is now deprecated. .SH HEADER INJECTION .sp To prevent \X'tty: link http://www.nyphp.org/PHundamentals/8_Preventing-Email-Header-Injection'\fI\%header injection\fP\X'tty: link' attempts to send a message with newlines in the subject, sender or recipient addresses will result in a \fBBadHeaderError\fP\&. .SH SIGNALLING SUPPORT .sp Added in version 0.4. .sp \fBFlask\-Mail\fP now provides signalling support through a \fBemail_dispatched\fP signal. This is sent whenever an email is dispatched (even if the email is not actually sent, i.e. in a testing environment). .sp A function connecting to the \fBemail_dispatched\fP signal takes a \fBMessage\fP instance as a first argument, and the Flask app instance as an optional argument: .INDENT 0.0 .INDENT 3.5 .sp .EX def log_message(message, app): app.logger.debug(message.subject) email_dispatched.connect(log_message) .EE .UNINDENT .UNINDENT .SH API .INDENT 0.0 .TP .B class flask_mail.Mail(app=None) Manages email messaging .INDENT 7.0 .TP .B Parameters \fBapp\fP \-\- Flask instance .UNINDENT .INDENT 7.0 .TP .B connect() Opens a connection to the mail host. .UNINDENT .INDENT 7.0 .TP .B send(message) Sends a single message instance. If TESTING is True the message will not actually be sent. .INDENT 7.0 .TP .B Parameters \fBmessage\fP \-\- a Message instance. .UNINDENT .UNINDENT .INDENT 7.0 .TP .B send_message(*args, **kwargs) Shortcut for send(msg). .sp Takes same arguments as Message constructor. .INDENT 7.0 .TP .B Versionadded 0.3.5 .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B class flask_mail.Attachment(filename=None, content_type=None, data=None, disposition=None, headers=None) Encapsulates file attachment information. .INDENT 7.0 .TP .B Versionadded 0.3.5 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBfilename\fP \-\- filename of attachment .IP \(bu 2 \fBcontent_type\fP \-\- file mimetype .IP \(bu 2 \fBdata\fP \-\- the raw file data .IP \(bu 2 \fBdisposition\fP \-\- content\-disposition (if any) .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B class flask_mail.Connection(mail) Handles connection to host. .INDENT 7.0 .TP .B send(message, envelope_from=None) Verifies and sends message. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBmessage\fP \-\- Message instance. .IP \(bu 2 \fBenvelope_from\fP \-\- Email address to be used in MAIL FROM command. .UNINDENT .UNINDENT .UNINDENT .INDENT 7.0 .TP .B send_message(*args, **kwargs) Shortcut for send(msg). .sp Takes same arguments as Message constructor. .INDENT 7.0 .TP .B Versionadded 0.3.5 .UNINDENT .UNINDENT .UNINDENT .INDENT 0.0 .TP .B class flask_mail.Message(subject=\(aq\(aq, recipients=None, body=None, html=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. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBsubject\fP \-\- email subject header .IP \(bu 2 \fBrecipients\fP \-\- list of email addresses .IP \(bu 2 \fBbody\fP \-\- plain text message .IP \(bu 2 \fBhtml\fP \-\- HTML message .IP \(bu 2 \fBsender\fP \-\- email sender address, or \fBMAIL_DEFAULT_SENDER\fP by default .IP \(bu 2 \fBcc\fP \-\- CC list .IP \(bu 2 \fBbcc\fP \-\- BCC list .IP \(bu 2 \fBattachments\fP \-\- list of Attachment instances .IP \(bu 2 \fBreply_to\fP \-\- reply\-to address .IP \(bu 2 \fBdate\fP \-\- send date .IP \(bu 2 \fBcharset\fP \-\- message character set .IP \(bu 2 \fBextra_headers\fP \-\- A dictionary of additional headers for the message .IP \(bu 2 \fBmail_options\fP \-\- A list of ESMTP options to be used in MAIL FROM command .IP \(bu 2 \fBrcpt_options\fP \-\- A list of ESMTP options to be used in RCPT commands .UNINDENT .UNINDENT .INDENT 7.0 .TP .B add_recipient(recipient) Adds another recipient to the message. .INDENT 7.0 .TP .B Parameters \fBrecipient\fP \-\- email address of recipient. .UNINDENT .UNINDENT .INDENT 7.0 .TP .B attach(filename=None, content_type=None, data=None, disposition=None, headers=None) Adds an attachment to the message. .INDENT 7.0 .TP .B Parameters .INDENT 7.0 .IP \(bu 2 \fBfilename\fP \-\- filename of attachment .IP \(bu 2 \fBcontent_type\fP \-\- file mimetype .IP \(bu 2 \fBdata\fP \-\- the raw file data .IP \(bu 2 \fBdisposition\fP \-\- content\-disposition (if any) .UNINDENT .UNINDENT .UNINDENT .UNINDENT .SH AUTHOR unknown .SH COPYRIGHT 2024, Dan Jacob .\" Generated by docutils manpage writer. .