Sending Email Using Python Script (Gmail )
About this Post
In this post I will be sharing a simple python script to send email using Python SMTP module.
I am taking Gmail example.
Prerequisite
- Python 2 / Python 3
- Enable Less Secure App Login (Gmail)- https://myaccount.google.com/lesssecureapps
Python Code
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def mail_using_gmail():
from_email = '-------@gmail.com'
to_email = "-------@gmail.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Testing Email Using Python"
msg['From'] = from_email
msg['To'] = to_email
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test email."
part1 = MIMEText(text, 'plain')
msg.attach(part1)
# SMTP client session
mail = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
# Login using email and password
mail.login(from_email, 'password of from_email')
# Sending mail
mail.sendmail(from_email, to_email, msg.as_string())
# Terminate SMTP session
mail.quit()
return "Success"
print(mail_using_gmail())
Very good bhai
ReplyDeleteCool
ReplyDelete