Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自文件目录的Python电子邮件PDF_Python_Loops_If Statement_Exchangelib - Fatal编程技术网

来自文件目录的Python电子邮件PDF

来自文件目录的Python电子邮件PDF,python,loops,if-statement,exchangelib,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 = 'Co

我需要通过电子邮件将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()
#======================== 

你的问题是如何发送电子邮件?如何更改循环?在您提供的实现中,什么不起作用?我想您是在问为什么发布的代码不起作用,但忘了解释什么不起作用。首先,您将
i
定义为循环变量,然后在全局范围内使用它,其中
i
现在只是数据帧的最后一个元素。也许你在什么地方搞砸了一个缩进?另外,请在发布代码时查看:-)