Python 如何向电子邮件添加多个附件

Python 如何向电子邮件添加多个附件,python,Python,我需要添加多个附件,以便通过python脚本自动发送电子邮件 我是python脚本编写新手。所以请帮忙。提前谢谢 下面是代码片段 请让我知道我要对下面的代码做什么更改 themsg = MIMEMultipart() themsg['Subject'] = Subject themsg['To'] =','.join(Email_ID) themsg['From'] =email_from themsg.preamble = 'I am not using a MIME-aware mail r

我需要添加多个附件,以便通过python脚本自动发送电子邮件

我是python脚本编写新手。所以请帮忙。提前谢谢

下面是代码片段

请让我知道我要对下面的代码做什么更改

themsg = MIMEMultipart()
themsg['Subject'] = Subject
themsg['To'] =','.join(Email_ID)
themsg['From'] =email_from
themsg.preamble = 'I am not using a MIME-aware mail reader.\n'
msg = MIMEBase('application', 'zip')
msg.set_payload(zf_csv.read())
zf_csv.close()
msg.set_payload(zf_pdf.read())
zf_pdf.close()
Encoders.encode_base64(msg)
print 'csv attachment is'+str(os.path.basename(current_dirs+csv_to_mail))
print 'pdf attachment is'+str(os.path.basename(current_dirs+pdf_to_mail))
msg.add_header('Content-Disposition', 'attachment; filename="(%s,%s)"' %(os.path.basename(current_dirs+csv_to_mail),os.path.basename(current_dirs+pdf_to_mail))

我不知道有什么比使用lamsonproject(lamsonproject.org)更好的方式使用python发送邮件


他们的API还包括向邮件添加附件,没有什么可以阻止您向邮件添加多个附件。只需查找有关MailResponse及其附加方法的API。

这是一个示例:

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = COMMASPACE.join(to_addrs_list) 
msg['Date'] = formatdate(localtime = True)
msg['Cc'] = COMMASPACE.join(cc_addrs_list)
msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)
msgAlternative.attach(MIMEText(content, 'plain'))

#add mutiple attachments to an Email
#attachment_paths is a list, like this:['/home/x/a.pdf', '/home/x/b.txt']
for file_path in attachment_paths:
    ctype, encoding = mimetypes.guess_type(file_path)
    if ctype is None or encoding is not None:
        ctype = dctype
    maintype, subtype = ctype.split('/', 1)
    try:
        with open(file_path, 'rb') as f:
            part = MIMEBase(maintype, subtype)
            part.set_payload(f.read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file_path))
            print os.path.basename(file_path)
            msg.attach(part)
    except IOError:
         print "error: Can't open the file %s"%file_path

这里有一个问题,很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或带有修辞色彩,无法以目前的形式得到合理的回答。有关澄清此问题以便重新打开的帮助,请参阅.Hi jamylak,我需要编写一个python脚本,将多个附件(多个附件)添加到电子邮件中,并将其自动发送到一些emai ID。我已将代码段添加到我的question@user1240863好了,这个问题没问题。