This project allows sending STMP email over TLS using SSL.
Project description
Secure Email
Table of Contents
Project Description
This project allows sending STMP email over TLS using SSL. Use the Emailer class to create an instance for an outgoing email account, and call the send
method to send an email. You can also use the EmailHandler class to add a custom email handler to your app's logger.
Installation
python -m pip install ssl-email
Setup
Keyring
The keyring library stores credentials in your machine's Windows Credential Locker. If you wish to make use of the Emailer class' use_keyring argument, make sure you have the keyring library installed:
pip install keyring
To use keyring to fetch the password in the Emailer class, use the email address associated with the credentials as the name:
keyring.set_password(
"username@domain.com",
"username@domain.com",
"supersecretpassword"
)
Usage
from secure_email import Emailer
Files in the demo folder demonstrate how to use the two classes in this project--Emailer and EmailHandler. send_email.py
demonstrates how to send an email from a python script, and the flask_app
demonstrates how to add the EmailHandler to you app's logger. The Emailer and EmailHandler classes are both located in sendmail.py
in the src folder.
Emailer
Class for sending emails. Instantiating a class merely stores the info to create an STMP connection, while calling the send method creates the STMP connection, sends an email, and then closes the connection.
-
Arguments:
- secure_host (str): default SSL-enabled mail host
- port (int): port number for mail host
- email_address (str): "from" email address
- password: (str | None): email password (default: None)
- user: (str | None): username for server, if different than email address
- unsecure_host: (str | None): backup non-SSL mail host
- use_keyring (bool): whether to fetch password from keyring (default: False)
- include_ssl_status (bool): whether to add line to body of sent emails indicating whether email was sent over SSL (default: False)
-
Methods:
- send
- Sends STMP email
- Arguments:
- to (str|list[str]): email recipient(s)
- subject (str): email subject line
- body (str): body of email as text or HTML string
- cc_list (list[str]): recipients to CC (default: [])
- bcc_list (list[str]): recipients to BCC (default: [])
- html (bool): whether to send body as HTML (default: False)
- secure (bool): whether to send over SSL (default: True)
- Returns:
- response (str): either "success" or error message
- send
-
Example:
# Supply password directly emailer = Emailer( "my.emailserver.com", # host 0, # port "email@example.com", # email_address password="supersecretpassword" ) # Use password stored in keyring emailer = Emailer( "my.emailserver.com", # host 0, # port "email@example.com", # email_address use_keyring=True ) response = emailer.send( "recipient@domain.org", # to "Open Immediately!!!", # subject "You may have won $1,000,000. Click the link below to find out", # body ) successful = response == "success"
EmailHandler
Extends logging.handlers.SMTPHandler to use the Emailer class to send emails (see the logging library documentation to learn more about the SMTPHandler). EmailHandler does not add any new methods to SMTPHandler, but it does take additional arguments.
- Additional arguments:
- send_html (bool): whether to send HTML email instead of plain text (default: False)
- backup_host (str | None): non-SSL host to send from if SSL host fails
- include_ssl_status (bool): whether to add line to body of sent emails indicating whether email was sent over SSL default: False)
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
Built Distribution
File details
Details for the file secure_email-1.0.0.tar.gz
.
File metadata
- Download URL: secure_email-1.0.0.tar.gz
- Upload date:
- Size: 214.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8250cfbb54384e0ae031acc9cb93e11e384e066cfd43a8452bbfa4743c18d505 |
|
MD5 | 5cb741ed0aaa82897e74918dfa4a65bd |
|
BLAKE2b-256 | 93da80451c6fea903ce0db3b1e44e310480daff42f49318535500ca85ebe6120 |
File details
Details for the file secure_email-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: secure_email-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a4761681c2d707a13ac476b8f379edba581f6415832c0bd990a4c202c73bb36 |
|
MD5 | 88c0279e42d89c13030f34a0a35f15f9 |
|
BLAKE2b-256 | 1176d3f20d88f25de50cf0c6a0cfd6260f2367c5c402b9edabb31338505caa05 |