Skip to content

luigifilippochiara/emails-with-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emails with Python

License: MIT

Learning how to send emails with Python.

Quick solution: if you have a Gmail account, the easiest way is to use yagmail.

Some theory

In this project, automatic emails are sent with SMTP (Simple Mail Transfer Protocol), the most common communication protocol for electronic mail transmission.

Transport Layer Security (TLS), and its now-deprecated predecessor, Secure Sockets Layer (SSL), are cryptographic protocols designed to provide communications security over a computer network. SSL creates a secure connection from the beginning, while TLS starts with an unsecure connection and encryptes it later.

Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email messages to support text in character sets other than ASCII, as well as attachments of audio, video, images, and application programs. Nowadays this is the most common email type. It usually contains both a plain text and HTML versions of the same message.

Project description

The scripts of this project allow you to send automatic emails from/to your localhost or connecting to a Gmail account.

  1. Sending email from and to your localhost is easier, you don't need to create a real email address and your computer is sending a message to itself. Just remember to run
$ python -m smtpd -c DebuggingServer -n localhost:1025

in a terminal window for your localhost to receive these emails. The received emails will be displayed in your terminal. Since everything is happening locally, there is no need to encrypt any connection. To use your localhost to need the server name ('localhost') and a port number (1025 in this case).

  1. To start sending real emails, it is suggested to use a Gmail account explicitly created for sending automatic emails, with modified security restrictions (see Sending Emails With Python for more detail). The scripts will then ask for your email address and password to automatically login to your email account and send emails from there. In this case, the connection must be encrypted for security reasons, either with SSL or TLS. To connect to your Gmail account you need the server name ('smtp.gmail.com') and the port number (465 for SSL and 587 for TLS).

  2. Finally, yagmail is a GMAIL/SMTP client that aims to make it as simple as possible to send automatic emails with Gmail, and it greatly simplifies the process through a friendly API. yagmail takes care of security and passwords for you.

Getting started

First of all, fork this repo and then clone your fork to have a local copy. All the data comes with the repo.

To use yagmail you first need to install it. You can create this project's environment with

$ conda env create -f emails-with-python.yml

and then activate it with

$ conda activate emails-with-python

Alternatively, you can install yagmail directly by running

$ pip install yagmail[all]

in a terminal. It is recommended to install it with the [all] specification, in order to install the keyring library too.

Scripts description

The main program of this project is send_email.py, which is an interactive script that allows you to send a MIME email.
This email contains both an HTML and a plain text version of a message, and an optional pdf attachment. You can choose to send the email with your localhost or with a Gmail account (with SSL or TLS encryption). Moreover, a RegEx check of the emails addresses is performed during execution. Run the script with

$ python send_email.py

and enter the required information.

Finally, the folder single_scripts contains some useful scripts:

  1. localhost_send_email.py sends a single plain text email from/to your localhost.
  2. ssl_send_email.py sends a single plain text email from your Gmail account with SSL encryption.
  3. tls_send_email.py sends a single plain text email from your Gmail account with TLS encryption.
  4. yagmail_send_email.py sends a plain text single email with a pdf attachment from your Gmail account, using yagmail.
  5. send_many_emails.py sends personalised automatic plain text emails in a for loop, taking the contact details from data/contact_details.csv. The connection is encrypted with SSL.

Acknowledgements

Most of this content was inspired by Sending Emails With Python from realpython.com.

License

This project is licensed under the MIT License. Use it as you wish.

About

Learning how to send emails with Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages