send_mail¶
e-mail sending module.
Code Example¶
PLAINTEXT_EMAIL = 'Yo...'
HTML_EMAIL = '<html>...'
send_mail(
'[AwesomeApp] very descriptive subject',
message=PLAINTEXT_EMAIL,
html_message=HTML_EMAIL,
to=[('To Example', 'to@example.com'), 'you@example.com'],
cc='him@example.com, her@example.com',
bcc=['them@example.com', ('You Know Who', 'youknowwho@example.com')],
sender=('AwesomeApp', 'notifications@example.com'),
reply_to='no-reply@example.com, do-not-reply@example.com',
attachments=['/full/path/to/attachment.ext'],
custom_headers={'X-Mailer': 'SendMail'}
)
Attention
In order to send the e-mail the function requires your server details.
You can put these details in
SMTP_HOST, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD, SMTP_USE_SSL & SMTP_USE_TLS
environment variables, or pass them as keyword arguments without the
smpt_ prefix.
Installation¶
You can install this module directly from github with
pip install -e git+https://github.com/dareenzo/send-mail@v1.0.1#egg=send_mail
Testing¶
Unit tests are provided with the code and you can run them by copying
the .env.example
file into a .env
file on the root of the
project.
The .env
file contains the required environment variables for the
module. So update it with your own settings.
These setting will be read by the cheap_dot_env
function into
environment variables before each test so that the send_mail
function can work properly
cp .env.example .env
# edit .env file with your mail settings
pip install -r requirements/test.txt
tox
Documentation Generation¶
# edit documentation in _docs
cd _docs
make singlehtml
cd ..
cp -fR _docs/_build/singlehtml/* docs/
Copyright & License¶
Code and documentation are available according to the MIT License.
See the LICENSE file for details.
API Reference¶
If you are looking for information on a specific function, class or method, this part of the documentation is for you
send_mail¶
Email sending module for scripts.
copyright: | Copyright 2017-2018 by Paulo Phagula. |
---|---|
license: | MIT, see LICENSE for details. |
-
send_mail.
send_mail
(subject, message=u'', html_message=u'', to=None, cc=None, bcc=None, sender=None, reply_to=None, attachments=None, custom_headers=None, logger=None, **kwargs)¶ Sends an email.
Single emails addresses can be provided as strings like ‘pphagula@gmail.com’ or name-address tuples like (‘Paulo Phagula’, ‘pphagula@gmail.com’‘)
Multiple email addresses can be provided as CSV strings like ‘email@example.com, email2@example.com’ or lists mixing singular address style [‘email@example.com’, (Example, email@example.com)]
Attachments can be passed as a CSV string with full paths to the desired files.
Parameters: - subject (
str
) – Subject line for this e-mail message. - message (
str
) – Plain-text message body. - html_message (
str
) – HTML message body. - to – Recipients address collection
- cc – Carbon Copy (CC) recipients address collection
- bcc – Blind Carbon Copy (BCC) recipients address collection
- sender – Sender email address as it appears in the ‘From’ email line.
- reply_to – List of addresses to reply to for the mail message.
- attachments – Attachments collection used to store data attached to this e-mail message.
- custom_headers (
dict
, optional) – Custom Headers to be added to the mail. - logger (
logging.Logger
,optional) – Logger instance for logging error and debug messages. - **kwargs –
Arbitrary keywords arguments
If any of them is not provided they will be taken from their environment variables equivalents (SMTP_<KEYWORD>) or use default values.
- host (
str
, optional): mail server host. If not given usesSMTP_HOST
. - port (
str
orint
, optional): mail server port. If not given usesSMTP_PORT
. - username (
str
, optional): mail server password. If not given usesSMTP_USERNAME
. - password (
str
, optional): mail server password. If not given usesSMTP_PASSWORD
. - use_tls (
bool
, optional): connect using TLS flag. If not given usesSMTP_USE_TLS
. Defaults to False - use_ssl (
bool
, optional): connect using SSL flag. If not given usesSMTP_USE_SSL
. Defaults to False - debug (
bool
, optional): debug mode enabling flag. If not given usesSMTP_DEBUG
.Defaults to False
- host (
Raises: ValueError
– if no recipient is given or no message is given.- subject (