#!/usr/bin/python3 # -*- coding: utf-8 -*- import smtplib import pymysql import sys from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText DB = "EBA_STOCK" USER = "root" PASSWORD = "howdoiturnthison" def sendmail(target, message, subject): noreply = 'consultas@especificosba.com.ar' noreplypass = "contraseñaparacandelaria" msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = noreply msg['to'] = target msg.attach(MIMEText(message,'html')) s = smtplib.SMTP('mail.especificosba.com.ar:587') s.ehlo() s.starttls() #print("tls'd", noreply, noreplypass) s.login(noreply, noreplypass) try: s.sendmail(noreply, target, msg.as_string()) ok = True except Exception: ok = False print("Error al enviar la consulta") finally: s.quit() return ok def populate(tpl, data): out = "" for line in tpl.split("\n"): #for k,v in data.iteritems(): for k,v in data.items(): if k in line: line = line.replace("{{%s}}" % k , data[k]) out += line + "\n" return out def reset(identity): conn = pymysql.connect(unix_socket='/var/run/mysqld/mysqld.sock', user=USER, passwd=PASSWORD, db=DB,cursorclass=pymysql.cursors.DictCursor) cur = conn.cursor() if '@' in identity: cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and mail = %s", identity) else: # hash cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and hash = %s", identity) cliente = cur.fetchone() if cliente is None: return False DATA = {} DATA["HASH"] = cliente["hash"] target = cliente["mail"] message = open("/var/www/pedido/back/mailtemplate.html").read() subject = "Link pedidos ESPECIFICOS Buenos Aires" return sendmail(target, populate(message, DATA), subject) if __name__ == '__main__': if len(sys.argv) != 2: print('usage %s mail' % sys.argv[0]) sys.exit(1) reset(sys.argv[1])