2011-04-11 9 views
10

कोड का निम्नलिखित स्निपिट ठीक काम करता है, इस तथ्य को छोड़कर कि परिणामी अनुलग्नक फ़ाइल नाम ईमेल में खाली है (फ़ाइल जीमेल में 'noname' के रूप में खुलती है)। मैं क्या गलत कर रहा हूं?पायथन में किसी ईमेल को फ़ाइल संलग्न करने से रिक्त फ़ाइल नाम होता है?

file_name = RecordingUrl.split("/")[-1] 
      file_name=file_name+ ".wav" 
      urlretrieve(RecordingUrl, file_name) 

      # Create the container (outer) email message. 
      msg = MIMEMultipart() 
      msg['Subject'] = 'New feedback from %s (%a:%a)' % (
From, int(RecordingDuration)/60, int(RecordingDuration) % 60) 

      msg['From'] = "[email protected]" 
      msg['To'] = '[email protected]' 
      msg.preamble = msg['Subject']     
      file = open(file_name, 'rb') 
      audio = MIMEAudio(file.read()) 
      file.close() 
      msg.attach(audio) 

      # Send the email via our own SMTP server. 
      s = smtplib.SMTP() 
      s.connect() 
      s.sendmail(msg['From'], msg['To'], msg.as_string()) 
      s.quit() 

उत्तर

13

आप add_header method का उपयोग कर संदेश के audio हिस्सा करने के लिए एक Content-Disposition header जोड़ने की जरूरत:

file = open(file_name, 'rb') 
audio = MIMEAudio(file.read()) 
file.close() 
audio.add_header('Content-Disposition', 'attachment', filename=file_name) 
msg.attach(audio) 
+2

धन्यवाद। पाइथन ईमेल उदाहरणों को व्यावहारिक बनाने के लिए मुझे यह तीसरा अनुकूलन करना है, उन्हें वास्तव में फिर से लिखा जाना चाहिए। –

+2

@ सेन डब्ल्यू - इस उदाहरण में 'add_header' का उपयोग किया जाता है: http://docs.python.org/library/email-examples.html#id2 –