将 zip 文件添加到电子邮件中

Posted

技术标签:

【中文标题】将 zip 文件添加到电子邮件中【英文标题】:adding to an email a zip file 【发布时间】:2018-01-18 23:15:40 【问题描述】:

下面的代码在我使用它来发送 txt 文件、图像或音频时运行良好。但是,当我尝试发送 zip 文件、rar 文件或任何其他没有自己的 MIME(与 MIMEText、MIMEImage 或 MIMEAudio 无关)的文件时,它不起作用。

总之,每当我到达 else 部分(MIMEBase 命令)时,我都会做错事并得到错误:

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

代码:

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

我见过类似的代码,但我不明白我的代码有什么问题。

你能解释一下我在搞砸什么吗?

谢谢!

【问题讨论】:

@tzadok 没有人反对你...你有 2 票赞成 我们在谈论我问的其他问题 你们都很好;) 【参考方案1】:

如果它对这里的任何人有帮助,那就是答案: 主要问题是我更改了 msg 有效负载而不是 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()

【讨论】:

以上是关于将 zip 文件添加到电子邮件中的主要内容,如果未能解决你的问题,请参考以下文章

python邮件脚本-添加zip

Boto3没有将zip文件上传到S3 python

删除 zip 存档中“早于”的文件

PHPMailer 不发送带有附加 zip 文件的邮件

如何在 zip 文件中生成 xls 文件并将其发送到电子邮件以在那里下载 - groovy [关闭]

无法使用烧瓶邮件python将附件添加到邮件