Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import smtplib
- from string import Template
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- NUM_ORGS = 17
- DEFINED_ORGS = []
- NAMES = []
- EMAIL = []
- ORG_DATA = []
- def read_template(filename):
- """
- Returns a Template object comprising the contents of the
- file specified by filename.
- """
- with open(filename, 'r', encoding='utf-8') as template_file:
- template_file_content = template_file.read()
- return Template(template_file_content)
- def send_email():
- with open('data2.csv') as csv_file:
- csv_reader = csv.reader(csv_file, delimiter=',')
- line_count = 0
- for row in csv_reader:
- if line_count == 0:
- DEFINED_ORGS = row[15:]
- line_count += 1
- else:
- name = row[1]
- email = row[3]
- orgs = []
- for i in range(1, NUM_ORGS):
- if row[i + 15] == 'X':
- orgs.append(DEFINED_ORGS[i])
- NAMES.append(name)
- EMAIL.append(email)
- ORG_DATA.append(orgs)
- line_count += 1
- with open("email_template.txt") as f:
- email_template = f.read()
- message_template = read_template('email_template.txt')
- s = smtplib.SMTP('smtp.gmail.com', 587)
- s.starttls()
- s.login('akashsrinagesh@gmail.com', 'ggmcpvphkxrfspdv')
- for name, email, org in zip(NAMES, EMAIL, ORG_DATA):
- msg = MIMEMultipart()
- # add in the actual person name to the message template
- message = message_template.substitute(name=name, orgs=', '.join(org))
- # Prints out the message body for our sake
- print(message)
- # setup the parameters of the message
- msg['From']='pittifcvpp@gmail.com'
- msg['To']=email
- msg['Subject']="This is TEST"
- # add in the message body
- msg.attach(MIMEText(message, 'plain'))
- # send the message via the server set up earlier.
- s.send_message(msg)
- del msg
- for name, email, org in zip(NAMES, EMAIL, ORG_DATA):
- print(name, email, org)
- print(f'Processed {line_count} lines.')
- send_email()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement