我试着发送一封电子邮件,多个附件都是PDF格式的,这是我的代码
for pdf_files in glob.glob(path+str(customer)+'*.*'):
get_filename = os.path.basename(pdf_files)
list_files = [get_filename]
attachment = open(path+get_filename, 'rb')
email = EmailMessage('Report for'+' '+customer,
'Report Date'+' '+cust+', '+cust, to=['asd@asd.com'])
email.attachments(filename=list_files, content=attachment.read(), mimetype='application/pdf')
email.send()以下是Django文档对附件属性的说明。
附件:要放在消息上的附件列表。这些可以是
email.MIMEBase.MIMEBase实例,也可以是(filename, content, mimetype)三元组。
当我试图使用附件运行这段代码时,我总是会得到这个错误。
TypeError: 'list' object is not callable也许我误解了,但我传递了一个文件列表,像医生说的,请有人举个例子。我到处寻找,所有的人使用附加和attach_files,但这两个功能只发送一个附件在一封电子邮件。
发布于 2016-01-07 17:52:03
您应该构造一个附件列表,并在创建EmailMessage时使用它。如果您想用一封电子邮件发送所有附件,那么您需要首先创建所有附件的列表,然后在循环之外发送电子邮件。
我已经稍微简化了代码,所以您将不得不调整它,但希望这将使您开始。
# Loop through the list of filenames and create a list
# of attachments, using the (filename, content, mimetype)
# syntax.
attachments = [] # start with an empty list
for filename in filenames:
# create the attachment triple for this filename
content = open(filename, 'rb').read()
attachment = (filename, content, 'application/pdf')
# add the attachment to the list
attachments.append(attachment)
# Send the email with all attachments
email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
['to1@example.com', 'to2@example.com'], attachments=attachments)
email.send()email.attachments属性是email实例的实际附件列表。它是Python列表,因此试图像调用方法一样调用它会引发错误。
https://stackoverflow.com/questions/34661771
复制相似问题