Sending Email Using Python Smtplib Automate Email Sending Alerts
When you have a script running and you have logs set , you might also want to send yourself or colleagues, email alerts in case things go wrong or simply to let you know that your scripts ran to completion without any problems.
In this short notes, we take a look at sending email using Python. We will send our emails via Gmail.
Credits to sentdex. You can check him out on Youtube.
1 |
import smtplib |
1 2 3 4 |
#let's create our message as the email content content = """ Hello World, this is a an email we are sending automatically as soon as something happens to our programe. When you get this email please take time to check the program's script again. Thank you for your time. """ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#now we need to specify the smtp mail servers and the port number of our email hosting. #we will be using gmail in our case mail = smtplib.SMTP('smtp.gmail.com',465) #identify yourself with the server . you can either use 'helo' or ehlo mail.ehlo() #use tls so that all commands the follow after that will be encrypted mail.starttls() #login into your email with your email address and password mail.login('YOURemailaddress','YOURpassword') mail.send('YOURemailaddress','recipient_email', content) #now let's close the mail mail.close() |