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
from email.mime.audio import MIMEAudio
from email.mime.application import MIMEApplication
from ValidacionTelefonos import ValidacionTelefonosArgentinos, ValidacionCorreo
import requests, json, os, smtplib, config
import requests, json, os, smtplib, config, base64
class ServiceBase(ABC):
......@@ -121,11 +121,21 @@ class Mail(ServiceBase):
self.s.starttls()
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])
info = json.loads(data[Table.info])
self.TextoaEnviar = ""
#import ipdb; ipdb.set_trace()
msg = MIMEMultipart()
Cr = ValidacionCorreo()
msg['From'] = self.Usuario(info)
......@@ -139,11 +149,8 @@ class Mail(ServiceBase):
return(succ)
for file in types:
filepath = data[Table.path] + file
if file == 'texto':
self.TextoaEnviar = filepath#por si es envio masivo
MIME = self.MIMEmode(file, filepath, types[file])
msg.attach(MIME)
succ[file] = True
try:
if not msg['From'] in stadomail.MailRompedor:
......@@ -164,37 +171,54 @@ class Mail(ServiceBase):
return(u)
def Envio(self,msg,info,stadomail):
#import ipdb; ipdb.set_trace()
try:
smtp = info["smtp"]
except:
smtp = False
#import ipdb; ipdb.set_trace()
if smtp == False:
#print(info, type(info))
print("mail sale por default")
self.iniciarConn()
n = self.s.send_message(msg)
elif smtp["host"] == "MAILMASIVO":
n = self.sendGrid(msg,smtp)
else:
self.ControlMail(stadomail,smtp)
n = self.SMTPcliente(msg, smtp)
return(n)
def sendGrid(self, msg, smtp):
def sendGrid(self, info, data):
print("Saliendo por sendgrid")
texto = open(self.TextoaEnviar, "r").read()
smtp = info["smtp"]
msg = self.leerDatosparaApi(info, data)
Mail=smtp["username"]
KeyAPI=smtp["password"]
headers = {'Content-type': 'application/json', "Authorization": "Bearer {}".format(KeyAPI)}
data={"personalizations": [{"to": [{"email": msg['To']}]}],
"from": {"email": Mail},
"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 )
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):
est = stadomail.ChequearMailServer(User=smtp["username"],
Pass=smtp["password"],
......@@ -209,7 +233,6 @@ class Mail(ServiceBase):
dg = smtplib.SMTP(host = Serversmtp["host"], port = Serversmtp["port"])
dg.starttls()
dg.login(Serversmtp["username"],Serversmtp["password"])
#import ipdb; ipdb.set_trace()
n = dg.send_message(msg)#revisa esto no se q devuelve
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!