Django EmailMessage Attachments attribute -


i try send in 1 email more 1 attachments in pdf code

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

here django documentation says attachments attribute.

attachments: list of attachments put on message. these can either email.mimebase.mimebase instances, or (filename, content, mimetype) triples.

when try run code using attachments error

typeerror: 'list' object not callable 

maybe i'm misunderstanding passing list of files the doc says please can 1 have example. out everywhere , people use attach , attach_files both functions send 1 attachment in email.

you should construct list of attachments, , use when create emailmessage. if want send attachments single email, need create list of attachments first, send email outside of loop.

i've simplified code you'll have adjust it, started.

# loop through list of filenames , create list # of attachments, using (filename, content, mimetype)  # syntax.  attachments = []  # start empty list filename in filenames:     # create attachment triple filename     content = open(filename, 'rb').read()     attachment = (filename, content, 'application/pdf')     # add attachment list     attachments.append(attachment)  # send email attachments email = emailmessage('hello', 'body goes here', 'from@example.com',         ['to1@example.com', 'to2@example.com'], attachments=attachments) email.send() 

the email.attachments attribute actual list of attachments email instance. python list, trying call method raises error.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -