mail.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. import smtplib
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. def sendmail(target, message, subject):
  7. noreply = 'consultas@especificosba.com.ar'
  8. #noreplypass = "contraseñaparacandelaria".encode("ascii","ignore")
  9. #noreplypass = noreplypass.decode("ascii")
  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. if k in line:
  32. line = line.replace("{{%s}}" % k , data[k])
  33. out += line + "\n"
  34. return out
  35. DATA = {
  36. "HASH": "254bdbda00d73f8645d5d4e6f1ef2c3e9525f2fda387148a33b451c88c25"
  37. }
  38. message = open("mailtemplate.html").read()
  39. subject = "Link pedididos ESPECIFICOS Buenos Aires"
  40. target = "adriana.garcia.cc@gmail.com"
  41. target = "davidventura27@gmail.com"
  42. sendmail(target, populate(message, DATA), subject)