How to send email from gmail using python

Here you will learn Free send Email with gmail using python

send email using python

How can we send the free email using python ?

if you want to learn sending email using SMTP with python then You are in the right place.

It's very simple process of sending email by SMTP (Simple Mail Transfer Protocol) and there will be need of authorization details for sending email because when you send text message or files by email then some one need to be known about the sender details.

Authorization details will be required because Authorization means username and password. you know we are talking about sending email so username is your email address and password is your gmail app password which you will need to be generate from gmail .

If you are not aware about the generate App password from gmail then please click on below link and learn generate the Gmail App password.

How to setup free Google SMTP server

Hope you will have generated successfully app password and now please keep it for later use.

We are showing to you both function. first function will be a simple sending text message and second function will be a sending text message with attachment the file.

Sending Simple Text Message

There is simple text message in below function where you can send one message to multiple users at a time.

import smtplib from email.MIMEText import MIMEText def send_mail(subject, to_mail_ids, content=None): password = "APP PASSWORD" s1 = smtplib.SMTP('smtp.gmail.com: 587') s1.starttls() s1.login("YOUR EMAIL ID", password) for mail in to_mail_ids: msg = MIMEMultipart() msg['From'] = "YOUR EMAIL ID" msg['Subject'] = subject plain_test_part = MIMEText(content, 'plain') msg.attach(plain_test_part) msg['To']=mail s1.sendmail(msg['From'] , msg['To'], msg.as_string()) s1.quit() return True

Sending Text Message with attachment file

There is simple text message with attachment in below function where you can send one message to multiple users at a time.


import smtplib from email.MIMEText import MIMEText def send_attach_mail(subject, file_name, to_mail_ids, content=None): full_path = 'Your File Path'+file_name f = open(full_path, "r") file_content = f.read() f.close() attach = MIMEApplication(file_content, 'xlsx') attach.add_header('Content-Disposition', 'attachment', filename=file_name) password = "YOUR APP PASSWORD" s1 = smtplib.SMTP('smtp.gmail.com: 587') s1.starttls() s1.login("YOUR EMAIL ID", password) for mail in to_mail_ids: msg = MIMEMultipart() msg['From'] = "YOUR EMAIL ID" msg['Subject'] = subject text_with_attachment = MIMEText(content, 'plain') msg.attach(text_with_attachment) msg.attach(attach) msg['To']=mail s1.sendmail(msg['From'] , msg['To'], msg.as_string()) s1.quit() return True

Both function has been completed now you can call the function with details . I am taking one example in below .


subject = "Test Email" to_mail_ids = ["abc@gmail.com","abc2@gmail.com"] content = "Welcome to my First Test Email" send_mail(subject, to_mail_ids, content=None)

Once you call the function then you will get the successful response .

Hope you like it , please comment if you have any doubt .



How to setup free Google SMTP server
Setup django project in pythonanywhere
How to upload file in S3 bucket in python
Free live cricket score API for python

Post a Comment

0 Comments