-
Notifications
You must be signed in to change notification settings - Fork 1
T. Email notification
Last update: 23/04/2021
Sending email from python is an easy task but with a twist because most of us do not have an mail server that we can use, so we need to be smart about it and find a simple yet effective work around.
The easiest way I could think about this was to create a new Gmail account that would be used to automatically send emails to my own account, and here is where the twist comes into effect. In order to use your newly created Gmail account you must allow "Less secure app access", this means that anyone - or even a spambot with your account and password can just log in, no extra verification is needed.
I'm not going to talk about creating a new email account.
In order to activate this setting that is default as "No", you must:
* Log into Gmail
* Go to Manage your Google Account (top right corner)
* Go to Security (left side of the screen)
* Scroll down till you find "Less secure app access"
* Turn it on
In order to access our newly created email address we must user our credentials to log in but it's definitely not a good practice to leave them in plain text, thus we will create a credentials.py file in which we will fill in the mail_address and mail_password.
The credentials.py file must have restricted access thus we will set it with "Owner Only" permissions.
$ chmod 700 credentials.py
I will not go into depth with this as it is a pretty simple function, I will write a small code break down but you can find the full script in the email_notification.py file.
Code Breakdown
def send_mail(body, to,cc,bcc, todays_date, sender, password ):
# this part of the code is where we create the message as a MIME - Multipurpose Internet Mail Extensions
message = MIMEMultipart()
message['Subject'] = 'Automatic email notification | '+todays_date
message['From'] = sender
message['To'] = ','.join(to)
message['Cc'] = ','.join(cc)
message['Bcc']= ','.join(bcc)
# this part is where we create the body of the email, everything is written in HTML
body_content ="Hey, <br><br> Please note that a tiny Rapsberry PI webserver says:<b> "+body +"</b><br><br>Best regards,<br>Bogdan"
message.attach(MIMEText(body_content, "html"))
# the message body must be a string
msg_body = message.as_string()
# this part is where we use the Secure Socket Layer to connect to the Simple Mail Transfer Protocol
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
# this part is where we log into our Gmail account
server.login(sender, password)
# this is where we actually send the email
server.sendmail(sender, (to+cc+bcc), msg_body)
# this is where we close the connection to the server
server.quit()
Result:
**Congrats, you're done!**
We have learned about the prerequisite steps we need to take in order to be able to send server side email in a secure fashion.
We have learned about the script we can use to send automatic emails.
If you hit a problem or have feedback (which is highly welcomed) please feel free to get in touch, more details in the footer.