How to send a zip file as an attachment in python?

后端 未结 2 2043
春和景丽
春和景丽 2020-12-10 14:37

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

2条回答
  •  無奈伤痛
    2020-12-10 15:10

    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.

提交回复
热议问题