from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart
host_server = 'smtp.qq.com'#使用qq邮箱smtp服务器
sender_qq = 'example'#sender_qq为发件人的qq号
pwd = '****'#pwd为qq邮箱的授权码
sender_qq_mail = 'example@qq.com' #发件人的邮箱
receiver = 'shouxinren@163.com' #收件人邮箱
mail_title = '这是标题' #邮件标题
smtp = SMTP_SSL(host_server) #ssl登录邮箱客户端
smtp.set_debuglevel(1)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)
##定义邮箱内容
msg = MIMEMultipart() ##生成一个邮箱对象
msg["Subject"] = Header(mail_title, 'utf-8') ##添加邮箱主题
msg["From"] = sender_qq_mail ##添加邮箱发送方
msg["To"] = receiver ##添加邮箱接收方
# 添加附件,这里以zip文件为例
file_path=r'test.zip'
att = MIMEText(open(file_path,"rb").read(), "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment;filename = {}'.format(file_path)
msg.attach(att)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
python使用qq邮箱发送带有附件的邮件
最新推荐文章于 2024-07-01 15:54:33 发布