Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- import smtplib
- import csv
- from email.mime.text import MIMEText
- # Prepare a template as mail body.
- def get_template():
- with open("Mail\\Text\\Template\\Path", "r", encoding="utf-8") as template_txt:
- template = template_txt.read()
- return template
- def send_gmail(name, email):
- gmail_user = 'you@gmail.com'
- gmail_password = 'pwd'
- mail_server = 'smtp.gmail.com'
- sender = gmail_user
- receiver = email
- subject = 'I am subject!'
- template = get_template()
- body = template.format(name)
- ## template: ##
- # Hi {},
- # This is your mail.
- msg = MIMEText(body.encode('utf-8'), _charset='utf-8') # Use utf-8 instead ascii.
- msg['Subject'] = subject
- msg['From'] = sender
- msg['To'] = receiver
- s = smtplib.SMTP_SSL(mail_server, 465)
- s.ehlo()
- s.login(gmail_user, gmail_password)
- s.sendmail(sender, receiver, msg.as_string())
- s.close()
- with open('ContactFile\\receivers.csv', newline='', encoding="utf-8") as f:
- ## receivers.csv:
- # Alice,alice@gmail.com
- # Bob,bob@gmail.com
- reader = csv.reader(f)
- for row in reader:
- print(row)
- 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