文件目录中的Python电子邮件PDF

时间:2018-08-29 15:54:05

标签: python loops if-statement exchangelib

我需要通过电子邮件将文件目录中的pdf和通用求职信通过电子邮件发送给与5位代码匹配的电子邮件地址。可以在pdf名称的前5个中找到该代码,然后在包含5位代码和电子邮件地址的相应数据框中找到该代码。有没有简单的方法可以做到这一点?谢谢

 # 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()
#======================== 

0 个答案:

没有答案