#!/usr/bin/python3 # -*- coding: utf-8 -*- import smtplib import pymysql import sys from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText 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()) except Exception: print("Error al enviar la consulta") finally: s.quit() 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 _id = 1 _id = 163 DB = "EBA_STOCK" USER = "root" PASSWORD = "howdoiturnthison" conn = pymysql.connect(unix_socket='/var/run/mysqld/mysqld.sock', user=USER, passwd=PASSWORD, db=DB,cursorclass=pymysql.cursors.DictCursor) # print("pls no") # sys.exit(1) cur = conn.cursor() # cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where id=%s and mail like '%%@%%'", (_id,)) # cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and mail like '%%@%%'") cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and mail like '%%pouso%%'") clientes = cur.fetchall() for cliente in clientes: print(cliente) DATA = { } DATA["HASH"] = cliente["hash"] target = cliente["mail"] #target = "mpetrigliano@leticiapepe.com.ar" message = open("mailtemplate.html").read() subject = "Link pedidos ESPECIFICOS Buenos Aires" # print(populate(message, DATA)) sendmail(target, populate(message, DATA), subject) # sendmail("davidventura27@gmail.com", populate(message, DATA), subject) # sendmail("especificosba@gmail.com", populate(message, DATA), subject)