import smtplib
host = "localhost"
port = 1234
email_from = "correo.origen@gmail.com"
email_to = "mi.correo@gmail.com"
message = f"""\
Subject: Enviar correo con python
To: {email_from}
From: {email_to}
Envio de correo usando Python."""
try:
with smtplib.SMTP(host, port) as server:
server.sendmail(email_from, email_to, message)
except Exception as e:
print('Error enviando el correo')
print(e)
else:
print('Correo enviado')
Si queremos identificarnos con un usuario y contraseña, hacemos:
import smtplib
host = "localhost"
port = 1234
usuario = 'mi_usuario'
contrasenha = 'mi_contrasenha'
email_from = "correo.origen@gmail.com"
email_to = "mi.correo@gmail.com"
message = f"""\
Subject: Enviar correo con python
To: {email_from}
From: {email_to}
Envio de correo usando Python."""
try:
with smtplib.SMTP(host, port) as server:
server.login(usuario, contrasenha)
server.sendmail(email_from, email_to, message)
except Exception as e:
print('Error enviando el correo')
print(e)
else:
print('Correo enviado')
import smtplib
host = "localhost"
port = 1234
email_from = "correo.origen@gmail.com"
# luego email_to, se convierte a una lista de string
email_to = "mi.correo@gmail.com,correo2@gmailcom,correo3@gmail.com"
message = f"""\
Subject: Enviar correo con python
To: {email_from}
From: {email_to}
Envio de correo usando Python."""
try:
with smtplib.SMTP(host, port) as server:
# aqui usamos email_to para obtener una lista de string
server.sendmail(email_from, email_to.split(','), message)
except Exception as e:
print('Error enviando el correo')
print(e)
else:
print('Correo enviado')