2017-08-10 12 views
2

私はtxtファイル、画像、またはオーディオを送信するために使用すると完全に正常に動作する以下のコードを持っています。しかし、zipファイル、rarファイル、またはMIMEText、MIMEImageまたはMIMEAudioに関係のない独自のMIMEを持たないファイルを送信しようとすると、動作しません。電子メールにzipファイルを追加する

e.send_mail(TARGET, SUBJECT, "file.zip")  
msg.attach(part)   //two lines after the else's end 
AttributeError: 'str' object has no attribute 'append' 

コード:結論、私は他の部分(MIMEBaseコマンド)に達するたび、私は何か間違ったことをして、エラーを取得するには

私は類似したコードを見てきました

def send_mail(self, target, subject, *file_names): 
    """ 
    send a mail with files to the target 
    @param target: send the mail to the target 
    @param subject: mail's subject 
    @param file_names= list of files to send 
    """ 
    msg = email.MIMEMultipart.MIMEMultipart() 
    msg['From'] = self.mail 
    msg['To'] = email.Utils.COMMASPACE.join(target) 
    msg['Subject'] = subject 
    for file_name in file_names: 
     f = open(file_name, 'rb') 
     ctype, encoding = mimetypes.guess_type(file_name) 
     if ctype is None or encoding is not None: 
      ctype = 'application/octet-stream' 
     maintype, subtype = ctype.split('/', 1) 
     # in case of a text file 
     if maintype == 'text': 
      part = MIMEText(f.read(), _subtype=subtype) 
     # in case of an image file 
     elif maintype == 'image': 
      part = MIMEImage(f.read(), _subtype=subtype) 
     # in case of an audio file 
     elif maintype == 'audio': 
      part = MIMEAudio(f.read(), _subtype=subtype) 
     # any other file 
     else: 
      part = MIMEBase(maintype, subtype) 
      msg.set_payload(f.read()) 
     part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name)) 
     msg.attach(part) 
     f.close() 
    # ssl server doesn't support or need tls, so don't call server_ssl.starttls() 
    self.server_ssl.sendmail(self.mail, target, msg.as_string()) 
    #server_ssl.quit() 
    self.server_ssl.close() 

が、私は何が間違っているのか理解していません。

私は何をしゃべっているのか教えていただけますか?

ありがとうございました!

+0

TNXのMSGのペイロードを変更したことでした。しかし、他の人は私の質問を見ていませんでした。誰かがそれを読んでいなくても私の質問に投票するまで5秒もかからなかったと私は誓っています。 – tzadok

+0

@tzadok誰もあなたを落としてくれません...あなたは2つのアップノートを持っていました – paper1111

+0

私は他の質問について話していました。 – tzadok

答えて

0

それはここに誰が答えである場合に役立ちます: 主な問題は、私は私を助けるためにあなたの試みのために代わりにzipファイルの

def send_mail(self, target, subject, body, *file_names): 
     """ 
     send a mail with files to the target 
     @param target: send the mail to the target 
     @param subject: mail's subject 
     @param file_names= list of files to send 
     """ 
     msg = MIMEMultipart() 
     msg['From'] = self.mail 
     msg['To'] = target 
     msg['Subject'] = subject 
     body_part = MIMEText(body, 'plain') 
     msg.attach(body_part) 
     for file_name in file_names: 
      f = open(file_name, 'rb') 
      ctype, encoding = mimetypes.guess_type(file_name) 
      if ctype is None or encoding is not None: 
       ctype = 'application/octet-stream' 
      maintype, subtype = ctype.split('/', 1) 
      # in case of a text file 
      if maintype == 'text': 
       part = MIMEText(f.read(), _subtype=subtype) 
      # in case of an image file 
      elif maintype == 'image': 
       part = MIMEImage(f.read(), _subtype=subtype) 
      # in case of an audio file 
      elif maintype == 'audio': 
       part = MIMEAudio(f.read(), _subtype=subtype) 
      # any other file 
      else: 
       part = MIMEBase(maintype, subtype) 
       part.set_payload(f.read()) 
      encoders.encode_base64(part) 
      part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name)) 
      msg.attach(part) 
      f.close() 
     # ssl server doesn't support or need tls, so don't call server_ssl.starttls() 
     self.server_ssl.sendmail(self.mail, target, msg.as_string()) 
     self.server_ssl.quit() 
関連する問題