0

I need to email a pdf and a generic cover letter from a file directory to a an email address that matches the 5 digit code. the code can be found in the first 5 of the pdf name and then the corresonding dataframe that contains the 5 digit code and email address. Is there an easy way to accomplish this? Thanks

 # loop through the email list 
for i in email_list.itertuples():
    PDF_name = i.AGCODE + '_2017 Net_Initial.pdf'
    cover_letter_name = 'CoverLetter.pdf' 
    print(PDF_name)
 #attach an excel file and PDF file: 
with open(dir_path + PDF_name, 'rb') as f, open(dir_path + cover_letter_name, 'rb') as g:
 # Read the binary file contents
     PDF_content = f.read()  
     cl_content = g.read()
     PDF_att = FileAttachment(name=PDF_name, content=PDF_content) 
     cl_att = FileAttachment(name=cover_letter_name, content=cl_content) 

 # if you want a copy in the 'Sent' folder
m = Message(
      account=a 
     ,folder=a.sent
     ,subject=('Award Letter for ' + i.FACILITY_NAME + ' -- Agency Code: ' + i.AGCODE)
     ,body = body_of_email 
     ,to_recipients=[Mailbox(email_address=i.FAC_EMAIL_ADDR)])

#attach files
m.attach(cl_att)
m.attach(PDF_att)
# send email each time          
m.send_and_save()
#======================== 
Tinkinc
  • 449
  • 2
  • 8
  • 21
  • 1
    Is your question how to send an e-mail? How to change your loop? What doesn't work in the implementation you've provided? – J. Blackadar Aug 29 '18 at 17:37
  • I think you're asking why the posted code doesn't work, but forgot to explain what doesn't work. For starters, you are defining `i` as a loop variable, but then using it in global scope, where `i` is now just the last element of the dataframe. Maybe you botched an indent somewhere? – Erik Cederstrand Aug 30 '18 at 11:05
  • Also, please have a look at https://stackoverflow.com/help/mcve when you are posting code :-) – Erik Cederstrand Sep 18 '18 at 12:01

0 Answers0