Guest User

Untitled

a guest
May 12th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # coding=utf-8
  2. import smtplib
  3. import csv
  4. from email.mime.text import MIMEText
  5.  
  6. # Prepare a template as mail body.
  7. def get_template():
  8. with open("Mail\\Text\\Template\\Path", "r", encoding="utf-8") as template_txt:
  9. template = template_txt.read()
  10. return template
  11.  
  12.  
  13. def send_gmail(name, email):
  14. gmail_user = 'you@gmail.com'
  15. gmail_password = 'pwd'
  16. mail_server = 'smtp.gmail.com'
  17.  
  18. sender = gmail_user
  19. receiver = email
  20. subject = 'I am subject!'
  21.  
  22. template = get_template()
  23.  
  24. body = template.format(name)
  25. ## template: ##
  26. # Hi {},
  27. # This is your mail.
  28.  
  29. msg = MIMEText(body.encode('utf-8'), _charset='utf-8') # Use utf-8 instead ascii.
  30. msg['Subject'] = subject
  31. msg['From'] = sender
  32. msg['To'] = receiver
  33.  
  34. s = smtplib.SMTP_SSL(mail_server, 465)
  35. s.ehlo()
  36. s.login(gmail_user, gmail_password)
  37. s.sendmail(sender, receiver, msg.as_string())
  38. s.close()
  39.  
  40.  
  41. with open('ContactFile\\receivers.csv', newline='', encoding="utf-8") as f:
  42. ## receivers.csv:
  43. # Alice,alice@gmail.com
  44. # Bob,bob@gmail.com
  45. reader = csv.reader(f)
  46. for row in reader:
  47. print(row)
  48. send_gmail(*row) # What are in *row depends on your function. In this case, *row is [name, email].
Add Comment
Please, Sign In to add comment