Commit 2bd38b73 by Tu Nombre

prueba adjuntos masivo

1 parent c6d8f7ff
Showing 1 changed file with 38 additions and 15 deletions
...@@ -6,7 +6,7 @@ from email.mime.image import MIMEImage ...@@ -6,7 +6,7 @@ from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio from email.mime.audio import MIMEAudio
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
from ValidacionTelefonos import ValidacionTelefonosArgentinos, ValidacionCorreo from ValidacionTelefonos import ValidacionTelefonosArgentinos, ValidacionCorreo
import requests, json, os, smtplib, config import requests, json, os, smtplib, config, base64
class ServiceBase(ABC): class ServiceBase(ABC):
...@@ -121,11 +121,21 @@ class Mail(ServiceBase): ...@@ -121,11 +121,21 @@ class Mail(ServiceBase):
self.s.starttls() self.s.starttls()
self.s.login(self.__username,self.__password) self.s.login(self.__username,self.__password)
def send(self,data,stadotel,stadomail): def send(self,data,stadotel,stadomail):#separo si es smtp o si es sendgrid api
info = json.loads(data[Table.info])
try:
if info["smtp"]["host"] == "MAILMASIVO":
res = n = self.sendGrid(info["smtp"], data)
return(res)
except:
pass
res = self._sendClasico(data,stadomail)
return(res)
def _sendClasico(self,data,stadomail):
types = json.loads(data[Table.type]) types = json.loads(data[Table.type])
info = json.loads(data[Table.info]) info = json.loads(data[Table.info])
self.TextoaEnviar = "" self.TextoaEnviar = ""
#import ipdb; ipdb.set_trace()
msg = MIMEMultipart() msg = MIMEMultipart()
Cr = ValidacionCorreo() Cr = ValidacionCorreo()
msg['From'] = self.Usuario(info) msg['From'] = self.Usuario(info)
...@@ -139,11 +149,8 @@ class Mail(ServiceBase): ...@@ -139,11 +149,8 @@ class Mail(ServiceBase):
return(succ) return(succ)
for file in types: for file in types:
filepath = data[Table.path] + file filepath = data[Table.path] + file
if file == 'texto':
self.TextoaEnviar = filepath#por si es envio masivo
MIME = self.MIMEmode(file, filepath, types[file]) MIME = self.MIMEmode(file, filepath, types[file])
msg.attach(MIME) msg.attach(MIME)
succ[file] = True succ[file] = True
try: try:
if not msg['From'] in stadomail.MailRompedor: if not msg['From'] in stadomail.MailRompedor:
...@@ -164,37 +171,54 @@ class Mail(ServiceBase): ...@@ -164,37 +171,54 @@ class Mail(ServiceBase):
return(u) return(u)
def Envio(self,msg,info,stadomail): def Envio(self,msg,info,stadomail):
#import ipdb; ipdb.set_trace()
try: try:
smtp = info["smtp"] smtp = info["smtp"]
except: except:
smtp = False smtp = False
#import ipdb; ipdb.set_trace()
if smtp == False: if smtp == False:
#print(info, type(info))
print("mail sale por default") print("mail sale por default")
self.iniciarConn() self.iniciarConn()
n = self.s.send_message(msg) n = self.s.send_message(msg)
elif smtp["host"] == "MAILMASIVO":
n = self.sendGrid(msg,smtp)
else: else:
self.ControlMail(stadomail,smtp) self.ControlMail(stadomail,smtp)
n = self.SMTPcliente(msg, smtp) n = self.SMTPcliente(msg, smtp)
return(n) return(n)
def sendGrid(self, msg, smtp): def sendGrid(self, info, data):
print("Saliendo por sendgrid") print("Saliendo por sendgrid")
texto = open(self.TextoaEnviar, "r").read() smtp = info["smtp"]
msg = self.leerDatosparaApi(info, data)
Mail=smtp["username"] Mail=smtp["username"]
KeyAPI=smtp["password"] KeyAPI=smtp["password"]
headers = {'Content-type': 'application/json', "Authorization": "Bearer {}".format(KeyAPI)} headers = {'Content-type': 'application/json', "Authorization": "Bearer {}".format(KeyAPI)}
data={"personalizations": [{"to": [{"email": msg['To']}]}], data={"personalizations": [{"to": [{"email": msg['To']}]}],
"from": {"email": Mail}, "from": {"email": Mail},
"subject": msg['Subject'], "subject": msg['Subject'],
"content": [{"type": "text/plain", "value": str(texto)}]} "content": [{"type": "text/plain", "value": str(msg["Body"])}],
"attachments": msg["Adjuntos"]}
requests.post(url="https://api.sendgrid.com/v3/mail/send",data=json.dumps(data), headers=headers ) requests.post(url="https://api.sendgrid.com/v3/mail/send",data=json.dumps(data), headers=headers )
return(True) return(True)
def leerDatosparaApi(self, info, data):
Cr = ValidacionCorreo()
types = json.loads(data[Table.type])
msg = {}
msg['To'] = Cr.ValidarCorreo(data[Table.dest])
msg['Subject'] = info['subject']
msg['Body'] = ""
msg["Adjuntos"] = []
for file in types:
filepath = data[Table.path] + file
if file == 'texto':
objeto = open(filepath, "r")
msg["Body"] = objeto.read()
else:
objeto = open(filepath, "rb")
adj = {"content":base64.b64encode(objeto.read()),"filename": file}
msg["Adjuntos"].append(adj)
objeto.close()
return(msg)
def ControlMail(self,stadomail,smtp): def ControlMail(self,stadomail,smtp):
est = stadomail.ChequearMailServer(User=smtp["username"], est = stadomail.ChequearMailServer(User=smtp["username"],
Pass=smtp["password"], Pass=smtp["password"],
...@@ -209,7 +233,6 @@ class Mail(ServiceBase): ...@@ -209,7 +233,6 @@ class Mail(ServiceBase):
dg = smtplib.SMTP(host = Serversmtp["host"], port = Serversmtp["port"]) dg = smtplib.SMTP(host = Serversmtp["host"], port = Serversmtp["port"])
dg.starttls() dg.starttls()
dg.login(Serversmtp["username"],Serversmtp["password"]) dg.login(Serversmtp["username"],Serversmtp["password"])
#import ipdb; ipdb.set_trace()
n = dg.send_message(msg)#revisa esto no se q devuelve n = dg.send_message(msg)#revisa esto no se q devuelve
return(n) return(n)
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!