如何在python中将zip文件作为附件发送? [英] How to send a zip file as an attachment in python?

查看:556
本文介绍了如何在python中将zip文件作为附件发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了许多教程,以及有关堆栈溢出的其他问题,并且文档和说明最少,只是无法解释的代码。我想发送一个已经压缩的文件,并作为附件发送。我已经尝试复制并粘贴提供的代码,但是它不起作用,因此无法解决问题。

I have looked through many tutorials, as well as other question here on stack overflow, and the documentation and explanation are at minimum, just unexplained code. I would like to send a file that I already have zipped, and send it as an attachment. I have tried copy and pasting the code provided, but its not working, hence I cannot fix the problem.

所以我要问的是,是否有人知道谁来解释smtplib以及电子邮件和MIME库如何共同发送文件,更具体地说,该如何做与一个zip文件。任何帮助将不胜感激。

So what I am asking is if anyone knows who to explain how smtplib as well as email and MIME libraries work together to send a file, more specifically, how to do it with a zip file. Any help would be appreciated.

这是每个人都引用的代码:

This is the code that everyone refers to:

import smtplib
import zipfile
import tempfile
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart    

def send_file_zipped(the_file, recipients, sender='you@you.com'):
    myzip = zipfile.ZipFile('file.zip', 'w')

    # Create the message
    themsg = MIMEMultipart()
    themsg['Subject'] = 'File %s' % the_file
    themsg['To'] = ', '.join(recipients)
    themsg['From'] = sender
    themsg.preamble = 'I am not using a MIME-aware mail reader.\n'
    msg = MIMEBase('application', 'zip')
    msg.set_payload(zf.read())
    encoders.encode_base64(msg)
    msg.add_header('Content-Disposition', 'attachment', 
               filename=the_file + '.zip')
    themsg.attach(msg)
    themsg = themsg.as_string()

    # send the message
    smtp = smtplib.SMTP()
    smtp.connect()
    smtp.sendmail(sender, recipients, themsg)
    smtp.close()

我怀疑这是问题代码也压缩文件。我不想压缩任何内容,因为我已经有一个要发送的压缩文件。无论哪种情况,该代码以及python库本身的文档都很少,因为它们无法提供img文件和文本文件以外的任何内容。

I suspect the issue is this code zips a file as well. I don't want to zip anything as I already have a zipped file I would like to send. In either case, this code is poorly documented as well as the python libraries themselves as they provide no insight on anything past img file and text files.

更新:错误,我是现在开始。我还使用上面的代码更新了文件中的内容

UPDATE: Error I am getting now. I have also updated what is in my file with the code above

Traceback (most recent call last):
File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 100, in <module>
send_file_zipped('hw5.zip', 'avaldez@oswego.edu')
File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 32, in send_file_zipped
msg.set_payload(myzip.read())
TypeError: read() takes at least 2 arguments (1 given)


推荐答案

我真的看不到问题所在。只需省略创建zip文件的部分,而是加载您拥有的zip文件。

I don't really see the problem. Just omit the part which creates the zip file and, instead, just load the zip file you have.

本质上,这部分在这里

msg = MIMEBase('application', 'zip')
msg.set_payload(zf.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', 
               filename=the_file + '.zip')
themsg.attach(msg)

创建附件。

msg.set_payload(zf.read())

设置好从文件 zf 中读取的附件的有效负载(可能意味着zip文件)

sets, well, the payload of the attachment to what you read from the file zf (probably meaning zip file).

只需事先打开您的zip文件,然后从此行读取。

Just open your zip file beforehand and let this line read from it.

这篇关于如何在python中将zip文件作为附件发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆