Send Email Without a Server

Sending email alerts from a Python programme can be incredibly useful. For example, a home security system could email you when an intruder is detected. One way to do this would be to set up an email server, but it is possible to do this using a web based service. Gmail provides an API to allow you to use a python script to instruct an email account you have to send an email. I have a separate Gmail acount registed to use for alerting from scripts and that's all I use it for. It will send an email to another account I have which i look at one my phone. The code below contains a function that takes as input a message and a list of email accounts to send the message to. It is descibed in these steps:

  1. import the smtplib library which can be used to send email using the Simple Mail Transfer Protocol.
  2. Define variables for the email address to send from and the username and password for this account
  3. link to the Gmail smtp api and assign this to the server variable
  4. Login to the api
  5. loop over all the email addreses in the list and send the message to each of them.

Comments