python - 将 zip 文件添加到电子邮件

标签 python email mime

我有下面的代码,当我用它来发送 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()

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

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

谢谢!

最佳答案

如果对任何人有帮助,这里就是答案: 主要问题是我更改了 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()

关于python - 将 zip 文件添加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45618222/

相关文章:

Html 电子邮件表格单元格在 Outlook 中垂直居中

linux - 如何在 Linux 终端中创建电子邮件帐户/地址?

python - Mesos/src/examples/python/test_framework.py 第 25 行,找不到 mesos.native

python - 用于小特征值的 Scipy 稀疏 eigsh()

python - 用python计算lmdb数据库中的记录数

excel - 设置Excel文档的mime类型

python - 为什么 Python 的 MIMEMultipart 生成带有换行符的附件文件名?

python - 脚本完成后 python 垃圾收集时出现奇怪的线程错误

c# - 以编程方式将文本文件的内容发送到电子邮件地址

c - 检测消息字符串是否是 QP 编码的