2011-04-11 16 views
10

El siguiente snipit de código funciona bien, excepto por el hecho de que el nombre del archivo adjunto resultante está en blanco en el correo electrónico (el archivo se abre como 'noname' en gmail). ¿Qué estoy haciendo mal?¿Adjuntar archivo a un correo electrónico en python conduce a un nombre de archivo en blanco?

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

Respuesta

13

es necesario agregar un Content-Disposition header a la parte audio del mensaje usando el 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) 
+2

Gracias. Esta es la tercera adaptación que he tenido que hacer para que los ejemplos de correo electrónico de python sean viables, realmente necesitan ser reescritos. –

+2

@Sean W. - 'add_header' se usa en este ejemplo: http://docs.python.org/library/email-examples.html#id2 –

Cuestiones relacionadas