Ticker

6/recent/ticker-posts

How to send emails with Python3 + gmail or others smtp servers.

 

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(FROMTOmsg.as_string())
smtp.close()
Reacciones:

Post a Comment

0 Comments