python压缩文件夹然后在发送邮件_如何在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.

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

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

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)

解决方案

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.

Essentially, this part here

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)

creates the attachment. The

msg.set_payload(zf.read())

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值