-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail.py
44 lines (32 loc) · 1.05 KB
/
gmail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import smtplib, getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
print("*** Enviar email con GMail ***")
user = input("Cuenta de correo (gmail): ")
password = input("Contraseña: ")
# Para las cabeceras
remitente = input("From (ex: administrador <[email protected]>: ")
destinatario = input("To, (ex: tecnico <[email protected]>: ")
asunto = input("Subject (ex: Asunto): ")
mensaje = input("Mensaje HTML: ")
# Host y puerto SMTP de Gmail
gmail = smtplib.SMTP('smtp.gmail.com', 587)
# Cifrado de datos requerido TLS
gmail.starttls()
# Credenciales del usuario
gmail.login(user, password)
# Despuración del envío 1 = true
gmail.set_debuglevel(1)
# Creamos cabeceras
header = MIMEMultipart()
header['Subject'] = asunto
header['From'] = remitente
header['To'] = destinatario
# Conversión a HTML del mensaje
mensaje = MIMEText(mensaje, 'html') #Content-type:text/html
# Añadimos mensaje a cabecera
header.attach(mensaje)
# Enviar
gmail.sendmail(remitente, destinatario, header.as_string())
# Cerramos la conexión SMTP
gmail.quit()