· 4 years ago · Nov 05, 2020, 10:50 PM
1######################## BIBILOTECAS ########################
2import os
3from openpyxl import Workbook
4
5import smtplib
6from email.mime.multipart import MIMEMultipart
7from email.mime.base import MIMEBase
8from email import encoders
9
10######################## FUNCIONES ########################
11
12def validar(dato):
13 while True:
14 if dato:
15 return dato
16 else:
17 print("Error, no ingresó nada!")
18 dato = input("Ingrese nuevamente el dato: ")
19
20def convertir(dato):
21 while True:
22 try:
23 dato = int(dato)
24 return dato
25 except ValueError:
26 try:
27 dato = float(dato)
28 return dato
29 except ValueError:
30 print("Error, ese dato no es un numero entero")
31 dato = input("Ingrese dato nuevamente: ")
32
33def validarEmail(email):
34 while True:
35 contador=0
36 punto=False
37
38 cantidad_caracteres_email=len(email)
39 cantidad_caracteres_email=cantidad_caracteres_email-1
40
41
42 for i in range(cantidad_caracteres_email):
43 if email[i]=="@" and email[0]!="@" and email[cantidad_caracteres_email]!="@":
44 contador=contador+1
45 elif email[i]=="." and email[0]!="." and email[cantidad_caracteres_email]!=".":
46 punto=True
47 if contador==1 and punto:
48 return email
49 break
50 else:
51 print("Email incorrecto.")
52 email = input("Ingrese su correo nuevamente: ")
53
54def registro(presentes):
55 nombre = input("Ingrese nombre y apellido del invitado: ")
56 nombre = validar(nombre)
57 telefono = input("\nIngrese telefono del invitado: ")
58 telefono = validar(telefono)
59 telefono = convertir(telefono)
60 email = input("\nIngrese email del invitado: ")
61 email = validar(email)
62 email = validarEmail(email)
63 guardar(nombre, telefono, email)
64 presentes.append([nombre,telefono,email])
65 return presentes
66
67def guardar(nombre, telefono, email):
68 ws = wb.active
69 ws.append([nombre, telefono, email])
70 wb.save("Presentes.xlsx")
71
72def conteo(presentes):
73 cant = len(presentes)
74 print(cant)
75
76def enviar():
77
78 #Instanciar objeto de msj
79 mensaje = MIMEMultipart("plain")
80
81 #Configuracion de los paramentros del msj
82 password = "##"
83 mensaje["From"] = "####@gmail.com"
84 mensaje["To"] = "####@gmail.com"
85 mensaje["Subject"] = "Lista de Invitados"
86
87 #Adjuntar archivo al cuerpo del msj
88
89 adjunto = MIMEBase("application", "octect-stream")
90 adjunto.set_payload(open("Presentes.xlsx", "rb").read())
91 encoders.encode_base64(adjunto)
92 adjunto.add_header("content-Disposition", 'attachment; filename="Lista-de-Invitados.xlsx"')
93
94 mensaje.attach(adjunto)
95
96 #Crear servidor - Host y puerto
97 server = smtplib.SMTP("smtp.gmail.com", 587)
98
99 #protocolo de cifrado de datos utilizado por gmail
100 server.starttls()
101
102 #Cargar Credencials para el envio del mail
103 server.login(mensaje["From"], password)
104
105 #Muestra la depuracion de la operación de envio 1=true
106 #server.set_debuglevel(1)
107
108 #Enviar el msj a través del sevidor
109 server.sendmail(mensaje["From"], mensaje["To"], mensaje.as_string())
110
111 #Cerrar conexion SMTP
112 server.quit()
113
114 print("\nInforme Enviado a casilla de correo")
115
116
117
118######################## MAIN ########################
119
120wb = Workbook()
121wb.save(filename = "Presentes.xlsx")
122
123presentes = []
124
125while True:
126 print("1 - Ingresar datos del Invitado")
127 print("\n2 - Ver Cantidad de Presentes")
128 print("\n3 - Salir y Enviar Informe")
129 opcion = input("\nIngrese una opcion: ")
130 if opcion == "1":
131 print("\nREGISTRO")
132 registro(presentes)
133 print("\nGuardado!")
134 elif opcion == "2":
135 print("\nCantidad de Presentes: ")
136 conteo(presentes)
137 #print(presentes)
138 elif opcion == "3":
139 print("\nFinalizando y enviando informe...")
140 enviar()
141 break
142 else:
143 print("Error, no se registró ingreso o ingreso incorrecto")
144
145 input("\nPresione ENTER para seguir")
146 os.system("cls")
147