47. sendmail
— sendmail.py: a simple program to send an email message¶
47.1. Functions defined in module sendmail¶
- sendmail.message(sender='', to='', cc='', subject='', text='', files=None)[source]¶
Create an email message
‘to’ and ‘cc’ can be lists of email addresses (python lists or strings with comma-separated email adresses.
- Returns:
EmailMessage – The Email message to be sent.
- sendmail.send_message(message, sender=None, to=None, smtpdata=None)[source]¶
Send an email message
- Parameters:
message (
EmailMessage
) – The Email message to be sent.message()
can help in creating it.sender (str) – The email address of the sender. If None, it is taken from the From: field in the message.
to (str or list) – The adressee(s): either a single email address or a list thereof. If None, it is taken from the To: field in the message.
A dict with the data needed to connect to the smtp server, or the name of a file containing this data. If None, the following config files are tried in order:
'$HOME/.config/smtp_credentials', '$HOME/.config/pyformex/smtp_credentials'
The data can (and usually should) contain the following fields:
server: the smtp server (like ‘smtp.gmail.com’) port: the port number (like 587 for using TLS) username: the full user name (like ‘john.doe@gmail.com’) password: the password needed for authentication
Leave out username and password if no authentication is required.
Notes
When using smtp.gmail.com as your SMTP server and your account has 2FA activated (it likely has), you need to set up an App Password and use that password for authentication. To create an App Password, go to your Google account settings, select the Security section and 2FA authentication, then create an App Password. By prefernce store it on a config file like mentioned above. Make the file read-only for everyone except yourself (chmod 0600).