mail.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. DB = "EBA_STOCK"
  9. USER = "root"
  10. PASSWORD = "howdoiturnthison"
  11. def sendmail(target, message, subject):
  12. noreply = 'consultas@especificosba.com.ar'
  13. noreplypass = "contraseñaparacandelaria"
  14. msg = MIMEMultipart('alternative')
  15. msg['Subject'] = subject
  16. msg['From'] = noreply
  17. msg['to'] = target
  18. msg.attach(MIMEText(message,'html'))
  19. s = smtplib.SMTP('mail.especificosba.com.ar:587')
  20. s.ehlo()
  21. s.starttls()
  22. #print("tls'd", noreply, noreplypass)
  23. s.login(noreply, noreplypass)
  24. try:
  25. s.sendmail(noreply, target, msg.as_string())
  26. ok = True
  27. except Exception:
  28. ok = False
  29. print("Error al enviar la consulta")
  30. finally:
  31. s.quit()
  32. return ok
  33. def populate(tpl, data):
  34. out = ""
  35. for line in tpl.split("\n"):
  36. #for k,v in data.iteritems():
  37. for k,v in data.items():
  38. if k in line:
  39. line = line.replace("{{%s}}" % k , data[k])
  40. out += line + "\n"
  41. return out
  42. def reset(identity):
  43. conn = pymysql.connect(unix_socket='/var/run/mysqld/mysqld.sock', user=USER, passwd=PASSWORD, db=DB,cursorclass=pymysql.cursors.DictCursor)
  44. cur = conn.cursor()
  45. if '@' in identity:
  46. cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and mail = %s", identity)
  47. else: # hash
  48. cur.execute("SELECT nombre,apellido,mail,hash FROM clientes where tipo=1 and hash = %s", identity)
  49. cliente = cur.fetchone()
  50. if cliente is None:
  51. return False
  52. DATA = {}
  53. DATA["HASH"] = cliente["hash"]
  54. target = cliente["mail"]
  55. message = open("/var/www/pedido/back/mailtemplate.html").read()
  56. subject = "Link pedidos ESPECIFICOS Buenos Aires"
  57. return sendmail(target, populate(message, DATA), subject)
  58. if __name__ == '__main__':
  59. if len(sys.argv) != 2:
  60. print('usage %s mail' % sys.argv[0])
  61. sys.exit(1)
  62. reset(sys.argv[1])