You need to change the security of your Google account so that you can send emails from python.
From Google account security Login to Google-Turn on 2-step verification process, Set the app password.
You need to log in to Gmail's server address (smtp.gmail.com), port 587 with your Google account and password.
smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
smtpobj.starttls()
smtpobj.login(sendAddress, password)
Add the content of the email to send to MIMEText. You can add the subject to ['Subject'], the sender's email address to ['From'], and the destination email address to ['To'].
msg = MIMEText(bodyText)
msg['Subject'] = subject
msg['From'] = fromAddress
msg['To'] = toAddress
msg['Date'] = formatdate()
You can send the email created by send_message.
smtpobj.send_message(msg)
smtpobj.close()
import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
sendAddress = 'My email address'
password = 'password'
subject = 'subject'
bodyText = 'Text'
fromAddress = 'Sender's email address'
toAddress = 'Destination email address'
#Connect to SMTP server
smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
smtpobj.starttls()
smtpobj.login(sendAddress, password)
#Compose an email
msg = MIMEText(bodyText)
msg['Subject'] = subject
msg['From'] = fromAddress
msg['To'] = toAddress
msg['Date'] = formatdate()
#Send the created email
smtpobj.send_message(msg)
smtpobj.close()
Recommended Posts