Created
November 21, 2015 19:59
-
-
Save 2624789/082fe60dd643cc624e74 to your computer and use it in GitHub Desktop.
Enviar por correo un objeto mensaje utilizando python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
# Iniciamos los parámetros del script | |
remitente = '[email protected]' | |
destinatario = '[email protected]' | |
# Creamos el objeto mensaje | |
mensaje = MIMEMultipart() | |
# Establecemos los atributos del mensaje | |
mensaje['From'] = remitente | |
mensaje['To'] = destinatario | |
mensaje['Subject'] = '[RPI] Correo de prueba' | |
# Creamos el cuerpo del mensaje | |
cuerpo = 'Este es el contenido del mensaje' | |
# Y lo agregamos al objeto mensaje como objeto MIME de tipo texto | |
mensaje.attach(MIMEText(cuerpo, 'plain')) | |
# Creamos la conexión con el servidor | |
sesion_smtp = smtplib.SMTP('smtp.gmail.com', 587) | |
# Ciframos la conexión | |
sesion_smtp.starttls() | |
# Iniciamos sesión en el servidor | |
sesion_smtp.login('[email protected]','una contraseña segura') | |
# Convertimos el objeto mensaje a texto | |
texto = mensaje.as_string() | |
# Enviamos el mensaje | |
sesion_smtp.sendmail(remitente, destinatario, texto) | |
# Cerramos la conexión | |
sesion_smtp.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment