Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. import csv
  2. import smtplib
  3. from string import Template
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6.  
  7. NUM_ORGS = 17
  8. DEFINED_ORGS = []
  9.  
  10. NAMES = []
  11. EMAIL = []
  12. ORG_DATA = []
  13.  
  14. def read_template(filename):
  15.     """
  16.    Returns a Template object comprising the contents of the
  17.    file specified by filename.
  18.    """
  19.     with open(filename, 'r', encoding='utf-8') as template_file:
  20.         template_file_content = template_file.read()
  21.     return Template(template_file_content)
  22.  
  23. def send_email():
  24.     with open('data2.csv') as csv_file:
  25.         csv_reader = csv.reader(csv_file, delimiter=',')
  26.         line_count = 0
  27.         for row in csv_reader:
  28.             if line_count == 0:
  29.                 DEFINED_ORGS = row[15:]
  30.                 line_count += 1
  31.             else:
  32.                 name = row[1]
  33.                 email = row[3]
  34.                 orgs = []
  35.                 for i in range(1, NUM_ORGS):
  36.                     if row[i + 15] == 'X':
  37.                         orgs.append(DEFINED_ORGS[i])
  38.  
  39.                 NAMES.append(name)
  40.                 EMAIL.append(email)
  41.                 ORG_DATA.append(orgs)
  42.                 line_count += 1
  43.  
  44.     with open("email_template.txt") as f:
  45.         email_template = f.read()
  46.  
  47.     message_template = read_template('email_template.txt')
  48.  
  49.     s = smtplib.SMTP('smtp.gmail.com', 587)
  50.     s.starttls()
  51.     s.login('akashsrinagesh@gmail.com', 'ggmcpvphkxrfspdv')
  52.     for name, email, org in zip(NAMES, EMAIL, ORG_DATA):
  53.         msg = MIMEMultipart()
  54.         # add in the actual person name to the message template
  55.         message = message_template.substitute(name=name, orgs=', '.join(org))
  56.  
  57.         # Prints out the message body for our sake
  58.         print(message)
  59.  
  60.         # setup the parameters of the message
  61.         msg['From']='pittifcvpp@gmail.com'
  62.         msg['To']=email
  63.         msg['Subject']="This is TEST"
  64.  
  65.         # add in the message body
  66.         msg.attach(MIMEText(message, 'plain'))
  67.  
  68.         # send the message via the server set up earlier.
  69.         s.send_message(msg)
  70.         del msg
  71.  
  72.  
  73.     for name, email, org in zip(NAMES, EMAIL, ORG_DATA):
  74.         print(name, email, org)
  75.  
  76.     print(f'Processed {line_count} lines.')
  77.  
  78. send_email()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement