使用python自动发邮件(163、QQ、outlook邮箱)

使用python自动发邮件(163、QQ、outlook邮箱)[Python常见问题]

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from datetime import datetime
 
# 发件人邮箱账号
my_sender = "l1525943131@163.com"
# my_sender = "1525943131@qq.com"
# 收件人邮箱账号
my_user = ["1525943131@qq.com"]
# my_user = ["l1525943131@163.com"]
# 授权码,不是账号的密码
authorization_code = "********"
# authorization_code = "****************"
# 发送人姓名
sender_name = "After__today"
# 接收人姓名
user_name = ["天晴的时候会下雨"]
# 发件服务器
smtp = "smtp.163.com"   #网易163
# smtp="smtp.qq.com"    #qq邮箱
 
def mail(text):
    now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    ret = True
    for i in range(len(my_user)):
        text1 = "您好 "+user_name[i]+"
现在时间为"+str(now_time)+"
很抱歉打扰您!
"+text
        try:
            msg = MIMEText(text1,"plain","utf-8")
            # 括号里的对应发件人邮箱昵称、发件人邮箱账号
            msg["From"] = formataddr([sender_name,my_sender])
            # 括号里的对应收件人邮箱昵称、收件人邮箱账号
            msg["To"] = formataddr([user_name[i],my_user[i]])
            # 邮件的主题,也可以说是标题
            msg["Subject"] = "系统提示信息"
            # 发件人邮箱中的SMTP服务器,端口是25
            server = smtplib.SMTP(smtp,25)
            # 发件人邮箱账号、邮箱密码
            server.login(my_sender, authorization_code)
            # 发件人邮箱账号、收件人邮箱账号、发送邮件
            server.sendmail(my_sender, [my_user[i],], msg.as_string())
            # 关闭连接
            server.quit()
        except Exception:
            ret = False
        finally:
            next
    return ret
 
text = "发送成功"
mail(text)
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 使用python自动发邮件(163、QQ、outlook邮箱)