# _*_ coding:utf-8 _*_ # Author: nan # edit time: 2022/12/15 17:05 # name: tools.py # Product: PyCharm import smtplib import email from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.header import Header mail_host = "smtp.163.com" mail_sender = " @163.com" mail_license = " " # 授权码 mail_receivers = ["","",""] #接收人 mm = MIMEMultipart('related') subject_content = """邮件测试""" mm["From"] = "sender_name<*@163.com>" mm["To"] = "receiver_1_name<******@qq.com>,receiver_2_name<******@outlook.com>" mm["Subject"] = Header(subject_content,'utf-8') body_content = """你好,测试""" message_text = MIMEText(body_content,"plain","utf-8") mm.attach(message_text) # 构造附件 atta = MIMEText(open('sample.xlsx', 'rb').read(), 'base64', 'utf-8') # 设置附件信息 atta["Content-Disposition"] = 'attachment; filename="sample.xlsx"' # 添加附件到邮件信息当中去 mm.attach(atta) # 创建SMTP对象 stp = smtplib.SMTP() # 设置发件人邮箱的域名和端口,端口地址为25 stp.connect(mail_host, 25) # set_debuglevel(1)可以打印出和SMTP服务器交互的所有信息 stp.set_debuglevel(1) # 登录邮箱,传递参数1:邮箱地址,参数2:邮箱授权码 stp.login(mail_sender,mail_license) # 发送邮件,传递参数1:发件人邮箱地址,参数2:收件人邮箱地址,参数3:把邮件内容格式改为str stp.sendmail(mail_sender, mail_receivers, mm.as_string()) print("邮件发送成功") # 关闭SMTP对象 stp.quit()
标签:mm,发送,stp,测试,import,mail,email,邮件 From: https://www.cnblogs.com/chengfo/p/16988367.html