python 发邮件

 书友73428983 发布于 2022-10-30 22:08

用python 发送带zip格式的邮件,邮件发送成功了,但是附件打不开是,代码如下

import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart

def send_file_zipped(the_file):

themsg = MIMEMultipart()
themsg['Subject'] = the_file
themsg['to'] = 'xxx'
themsg['from'] = 'xxx'
themsg.preamble = the_file
msg = MIMEBase('application', 'zip')
zf = open(the_file + '.zip', 'rb')
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()

try:
    server = smtplib.SMTP()
    server.timeout = 30
    server.connect('smtp.exmail.qq.com')

    server.login('xxx', 'xxx')
    server.sendmail('xxx', 'xxx', themsg)
    server.quit()
    print '发送成功'
except Exception, e:
    print str(e)

if name == "__main__":

file = '20170305'
send_file_zipped(file)

找了好多方法,都是这个结果,请教各位是哪里出了问题,邮件附件显示如下:

1 个回答
  • 你可以试一下我写的这个,用的是新浪邮箱发的,在我这儿是无论什么附件格式都可以发

    2022-10-31 22:22 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有