import pyodbc as po import email import pymssql import pandas as pd import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import time import json from os.path import basename from email.mime.application import MIMEApplication from email.utils import COMMASPACE, formatdate import requests with open("./config/config.json") as json_config_file: config = json.load(json_config_file) ServerSIR = config['ServerSIR'] DatabaseSIR = config['DatabaseSIR'] UsernameSIR = config['DBUserSIR'] PasswordSIR = config['DBPasswordSIR'] EmailServer = config['EmailServer'] EmailPort = config['EmailPort'] EmailUser = config['EmailUser'] EmailPassword = config['EmailPassword'] def IsNewPedimentoPayed(): try: cnxn = po.connect( "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + ServerSIR + ";DATABASE=" + DatabaseSIR + ";UID=" + UsernameSIR + ";PWD=" + PasswordSIR ) cursor = cnxn.cursor() cursor.execute("""SELECT P.sReferencia    ,p.Archivo    ,P.Aduana    ,P.Patente    ,P.Pedimento    ,P.[Pedimento Fecha Pago]    ,Peds.nTipoCambio, P.Clave  FROM SIR.Admin.SIR_VT_PedimPagados P    INNER JOIN SIR.SIR.SIR_149_PEDIMENTO AS Peds ON Peds.nIdPedimento149=ID WHERE    P.ClienteClave=2096 and p.[Pedimento FechaPago]>=CAST( GETDATE() AS Date )""") for row in cursor: print(row) SendEmail( ["agarcia@gemcousa.com"], "Aviso de pago de pedimento del cliente Amazon", "Estimado colaborador se le comunica mediante este email, que se ha pagado un nuevo pedimento del cliente Amazon", [] ) requests.get( url='https://localhost:5001/api/Notificaciones/SendWhattsapp?IdUsuario=30&Concepto=Nuevo pedimento pagado de Amazon&IdGrupo=1', verify=False) except Exception as e: print("Error: %s" % e) finally: cnxn.close() return '' def SendEmail(emailList, header, content, files): for Receiver in emailList: msg = MIMEMultipart() msg['To'] = Receiver msg['From'] = email.utils.formataddr( ('GEMCO informa : '+header, EmailUser)) msg['Subject'] = content msg.attach(MIMEText(header)) sender = EmailUser smtpObj = smtplib.SMTP(EmailServer, EmailPort) ''' for f in files or []: with open(f, "rb") as fil: part = MIMEApplication(fil.read(), Name=basename(f)) part["Content-Disposition"] = 'attachment; filename="%s"' % basename( f) msg.attach(part) ''' smtpObj.ehlo() smtpObj.starttls() smtpObj.login(EmailUser, EmailPassword) try: smtpObj.sendmail(sender, Receiver, msg.as_string()) print("Successfully sent email") except: print("Error: unable to send email") IsNewPedimentoPayed()