This example code for send email using server gmail.
"""Module for send mail"""
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib
TO = "to@domain.com"
FROM = "username@gmail.com"
SERVER = "smtp.gmail.com"
PORT = '587'
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "Test email"
msg.attach(MIMEText("this body email", 'plain', 'utf-8'))
SERVER_PORT = f"{SERVER}:{PORT}"
smtp = smtplib.SMTP(SERVER_PORT)
smtp.starttls()
smtp.login("username@gmail.com", "mypassword")
smtp.sendmail(FROM, TO, msg.as_string())
smtp.close()
0 Comments