代码
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
def sendMail(title, msg):
mail_host = "smtp.qq.com" # qq邮箱服务器地址
mail_port = 465 # 端口
mail_user = "xxx@qq.com" # qq邮箱地址
mail_pass = "xxx" # qq邮箱
sender = "xxx@qq.com" # 邮件发送者,这里用的qq邮箱
receivers = ['xxx@xx.com'] # 邮件接收者
code = "utf-8"
message = MIMEMultipart()
message.attach(MIMEText(msg, 'plain', code)) #文本
message['From'] = sender
message['To'] = ';'.join(receivers)
message['Subject'] = Header(title, code)
try:
smtpobj = smtplib.SMTP_SSL(mail_host, mail_port)
smtpobj.login(mail_user, mail_pass)
smtpobj.sendmail(sender, receivers, message.as_string())
except smtplib.SMTPException as e:
print(e)
if __name__ == "__main__":
msg = "" # 这里写发送消息
title = "" # 这里写邮件标题
sendMail(title=title, msg=msg)
需要的东西
mail_host: smtp.{domain}
mail_port: 使用ssl默认是465,不然就是25
mail_user:登陆的qq邮箱
mail_pass:下方点击生成授权码