python3.8发送邮件带附件

import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


class emailSend:

def emailSendfun(self,paths):
path = os.path.dirname(os.path.realpath(__file__))#附件路径
path = path + "\\"+paths+".zip"#附件为.zip文件
    
     # path = os.path.realpath(__file__)
     # path = str(path)
     # path = path.replace("\\", "/")
     # path =path.split("/emailSend")
     # path=path[0]+"/seven.zip"


# 发送邮箱服务器
smtpserver='smtp.163.com'
# 发送邮箱用户名和密码
user='gu88888888@163.com'
password='**********'
# 发送邮箱和接收邮箱
sender="gu********@163.com"
receive=['gu*********@163.com','27880570@qq.com']
# 发送邮件的主题和内容
subject="测试报告"
content='<html><h1 style="color:red">你好,发送邮件的内容</h1></html>'
# 构造附件内容
send_file=open(path,'rb').read()

att =MIMEText(send_file,'base64','utf-8')
att['Content-Type']='application/octet-stream'
att['Content-Disposition']='attachment;filename=paths+".zip"'


# 构建发送与接收信息
msgRoot=MIMEMultipart()
msgRoot.attach(MIMEText(content,'html','utf-8'))
msgRoot['subject']=subject
msgRoot['From']=sender
msgRoot['To']=','.join(receive)
msgRoot.attach(att)
# SSL协议端口号要使用465
smtp=smtplib.SMTP_SSL(smtpserver,465)
# HELO向服务器标识用户身份
smtp.helo(smtpserver)
# EHLO服务器返回结果确认
smtp.ehlo(smtpserver)
# 登录邮箱服务器用户名和密码
smtp.login(user,password)
print ('Start send email...')
smtp.sendmail(sender,receive,msgRoot.as_string())
smtp.quit()
print('Send End')

if __name__ == '__main__':
email1= emailSend();
email1.emailSendfun("seven")
posted @ 2020-11-30 16:48  有风有雨  阅读(375)  评论(0)    收藏  举报