Email - HomeMadePy/messages GitHub Wiki
The email module provides an easy and intuitive interface to construct and send SMTP based email messages.
Email API
Email(from_=None, to=None, server=None, port=None, auth=None, cc=None, bcc=None, subject="", body="", attachments=None, verbose=False)
- from_: (str) The email address the message will originate from, i.e. '[email protected]'.
- to: (str or list) [required unless 'cc' or 'bcc' param set] A single or list of email addresses as the primary recipients, i.e. '[email protected]' or ['[email protected]', '[email protected]']
- server: (str) The email server url, i.e. 'smtp.gmail.com'. This may be left blank, as the get_server method does a guess at the server's name and port.
- port: (int) The email server url's listening port, i.e. 465. This may be left blank, as the get_server method does a guess at the server's name and port.
- auth: (str) The from_ email addresses password. There is currently no support for two factor authentication. May use environment variables to pull auth credentials or whatever means you feel necessary.
- cc: (str or list) [optional] A single or list of email addresses to carbon-copy. See to for examples.
- bcc: (str or list) [optional] A single or list of email addresses to blind carbon-copy. See to for examples.
- subject: (str) [optional] The email message subject line.
- body: (str) [optional] The body text of the message to send. Use body text from a file by setting body=file.read().
- attachments: (str or list) [optional] Single or list of absolute filepaths, relative filepaths if script is run in same directory as filepath, i.e. '/user/home/me/file1.txt', or ['./file1.txt', 'file2.txt']
- verbose: (bool) [optional] print verbose output.
Usage
After instantiating an Email object, using the API above, the following method can be used to send the message:
- send(): sends the message synchronously
Example
Script/REPL
>>> from messages import Email
>>> e = Email(
from_='[email protected]',
to='[email protected]',
auth='P@ssw0rd',
subject='Message 1',
body='This is an email message!',
attachments=['./file1.txt', '~/home/usr/file2.pdf']
)
>>> e.send()
Message sent...
Recommended Use
If you intend to use this package to automate personal or work-related tasks, it might be best to create a separate email account you can dedicate just to your program/script automation. At this moment, this package does not support security features like two-factor authentication (TFA). As such, if that is a feature you normally have enabled on your personal email, then it's best to create a separate email account with TFA disabled.
Email Service Compatibility
Some email services won't let python send emails right out of the box unless you first modify some settings.
Gmail
- Requires one to allow less secure apps to function.
- You may also have to occasionally click an unblock captcha to allow access through python.