mail.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import smtplib
  4. import pymysql
  5. import sys
  6. from email.mime.multipart import MIMEMultipart
  7. from email.mime.text import MIMEText
  8. def sendmail(target, message, subject):
  9. noreply = 'consultas@especificosba.com.ar'
  10. noreplypass = "contraseñaparacandelaria"
  11. msg = MIMEMultipart('alternative')
  12. msg['Subject'] = subject
  13. msg['From'] = noreply
  14. msg['to'] = target
  15. msg.attach(MIMEText(message,'html'))
  16. s = smtplib.SMTP('mail.especificosba.com.ar:587')
  17. s.ehlo()
  18. s.starttls()
  19. #print("tls'd", noreply, noreplypass)
  20. s.login(noreply, noreplypass)
  21. try:
  22. s.sendmail(noreply, target, msg.as_string())
  23. except Exception:
  24. print("Error al enviar la consulta")
  25. finally:
  26. s.quit()
  27. def populate(tpl, data):
  28. out = ""
  29. for line in tpl.split("\n"):
  30. #for k,v in data.iteritems():
  31. for k,v in data.items():
  32. if k in line:
  33. line = line.replace("{{%s}}" % k , data[k])
  34. out += line + "\n"
  35. return out
  36. _id = 1
  37. _id = 163
  38. DB = "EBA_STOCK"
  39. USER = "root"
  40. PASSWORD = "howdoiturnthison"
  41. conn = pymysql.connect(unix_socket='/var/run/mysqld/mysqld.sock', user=USER, passwd=PASSWORD, db=DB,cursorclass=pymysql.cursors.DictCursor)
  42. # print("pls no")
  43. # sys.exit(1)
  44. cur = conn.cursor()
  45. # cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where id=%s and mail like '%%@%%'", (_id,))
  46. # cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and mail like '%%@%%'")
  47. cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and mail like '%%pouso%%'")
  48. clientes = cur.fetchall()
  49. for cliente in clientes:
  50. print(cliente)
  51. DATA = { }
  52. DATA["HASH"] = cliente["hash"]
  53. target = cliente["mail"]
  54. #target = "mpetrigliano@leticiapepe.com.ar"
  55. message = open("mailtemplate.html").read()
  56. subject = "Link pedidos ESPECIFICOS Buenos Aires"
  57. # print(populate(message, DATA))
  58. sendmail(target, populate(message, DATA), subject)
  59. # sendmail("davidventura27@gmail.com", populate(message, DATA), subject)
  60. # sendmail("especificosba@gmail.com", populate(message, DATA), subject)