Python Mini Projects for Everyone-4
How to send email with attachment.
Many things you can do with Python also you can send email. This is what i use often.
When creating a project, not possible to wait to much so i need a notice from Python also i want to see output but i couldn’t find a code for sending email with attachment. This is why i wrote this blog. With few steps and less code you can implement this code block to your project easily.
Before coding you need to activate your two factor authentication from https://myaccount.google.com/security. You can see the screen below. There is a one app password, it is Python :)
When you click “App passwords” , you need to verify your account.
Just one step left, from the “Choose application” select other and just write”Python”.
When you enter you will see like me.Click to Python and see your app password. I will blure this password part but copy this password to your notebook. Not try to copy my password :)
That’s it. We can pass to coding part. As always i will continue with Google Colab. You can write wherever you want.
We will use SMTP library and email package.
import smtplib #import necessary libraries
from email.message import EmailMessage
I will send email my own.You can change email_reciever part.
PAY ATTENTION, EMAIL PASSWORD IS NOT YOUR GMAIL PASSWORD.
email_sender='kaancukurx@gmail.com' #Sender email
email_password='aksjsszneeahaia'#password whichone take from google account.
email_reciever='kaancukurx@gmail.com' # email reciever
subject='Send mail with Python!' # your mail subject
content='Please look at attached file' # email content(you can add .txt file in here.)
the following function is almost standard.I will add comments, you can check from there.
def send_mail_with_attachment(email_reciever, subject, content, excel_file):
msg = EmailMessage()
msg['Subject'] = subject # definitaion of subject
msg['From'] = email_sender # definitaion of sender
msg['To'] = email_reciever # definitaion of reciever
msg.set_content(content) # setting content
with open(excel_file, 'rb') as f:
file_data = f.read() # reading of excel file, you can change this to csv,txt,png..
msg.add_attachment(file_data, maintype="application", subtype="xlsx", filename=excel_file)# add attachment part.need to optimize for your file.
with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:# our port is 465 for mail.
smtp.login(email_sender, email_password) # login to gmail.
smtp.send_message(msg)#sending to email
Last step is call function. I added excel file to Google Colab before.From option copy the file path.
send_mail_with_attachment(email_reciever,subject,content,'/content/sample_data/try.xlsx')
Let’s check it out. By the way this email sending is ultimate fast. Email coming in almost 1 second.
Last words
Thanks for reading this blog. I am trying to find useful and easy projects. Your comments and likes will help my growth.
You can find all source code in my github profile. You can keep in touch me from my LinkedIn profile.
If i have any mistake,please warn me.