Attaching file to an email in python leads to a blank file name? -
the following snipit of code works fine, except fact resulting attachment file name blank in email (the file opens 'noname' in gmail). doing wrong?
file_name = recordingurl.split("/")[-1] file_name=file_name+ ".wav" urlretrieve(recordingurl, file_name) # create container (outer) email message. msg = mimemultipart() msg['subject'] = 'new feedback %s (%a:%a)' % ( from, int(recordingduration) / 60, int(recordingduration) % 60) msg['from'] = "noreply@example.info" msg['to'] = 'user@gmail.com' msg.preamble = msg['subject'] file = open(file_name, 'rb') audio = mimeaudio(file.read()) file.close() msg.attach(audio) # send email via our own smtp server. s = smtplib.smtp() s.connect() s.sendmail(msg['from'], msg['to'], msg.as_string()) s.quit()
you need add content-disposition header audio part of message using add_header method:
file = open(file_name, 'rb') audio = mimeaudio(file.read()) file.close() audio.add_header('content-disposition', 'attachment', filename=file_name) msg.attach(audio)
Comments
Post a Comment