def send_mail():
'''
使用第三方模块发邮件
see also: https://www.cnblogs.com/Neeo/articles/11199127.html
'''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
mail_host = "smtp.qq.com"
mail_user = "xxx@qq.com"
mail_pass = "xxx"
sender = 'xxx@qq.com'
receivers = ['xxx@qq.com']
message = MIMEMultipart()
message['From'] = Header("我是发件人", 'utf-8')
message['To'] = Header("我是收件人", 'utf-8')
subject = 'Python发送带附件的邮件示例'
message['Subject'] = Header(subject, 'utf-8')
send_content = 'hi man,你收到附件了吗?'
content_obj = MIMEText(send_content, 'plain', 'utf-8')
message.attach(content_obj)
att1 = MIMEText(open(r'/jetBrains/pycharm/python-exercises/pytest/demo.py', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1.add_header("Content-Disposition", "attachment", filename=("gbk", "", "卧槽"))
message.attach(att1)
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
send_mail()