python smtp mail code example
Example 1: send html smtp email python
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
def send_email():
sender = FROM_EMAIL
receiver = ["[email protected]","[email protected]"]
msg = MIMEMultipart()
msg['From'] = FROM_NAME
msg['To'] = TO_NAME
msg['Subject'] = SUBJECT
with open(HTML_TEMPLATE) as f:
html = f.read()
part = MIMEText(html, 'html')
msg.attach(part)
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(CONNECTION USERNAME/EMAIL, CONNECTION PASSWORD)
connection.sendmail(sender, receiver, msg.as_string())
Example 2: smtp in python
import smtplib
from email.message import EmailMessage
EmailAdd = "Email id"
Pass = "Email Password"
msg = EmailMessage()
msg['Subject'] = 'Subject of the Email'
msg['From'] = EmailAdd
msg['To'] = '[email protected]','[email protected]'
msg.set_content('Mail Body')
with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
smtp.login(EmailAdd,Pass)
smtp.send_message(msg)