您当前的位置: 首页 >  Python

每日出拳老爷子

暂无认证

  • 3浏览

    0关注

    135博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Python】如何用Python发送SMTP邮件

每日出拳老爷子 发布时间:2022-06-22 14:12:22 ,浏览量:3

【背景】

自动发送邮件通知是流成自动化系统必备的功能,今天介绍如何用Python实现。

【代码】

这里以163邮箱为例

def send_smtp_mail(host="smtp.163.com",content="",cont_type="plain",encode="utf-8",sender_name="My_Robot",reciver_name="user",subject="unkonwn subject",mail_user="myname@163.com",mail_pass="Your Mail Pass" ):
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header

    with open("param.csv", encoding="utf8") as file:
        mail_addrs = file.readlines()
    sender = 'myname@163.com'
    receivers = [ele.strip() for ele in mail_addrs]

    message = MIMEText(content,cont_type,encode)
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = subject

    try:
        smtpObj = smtplib.SMTP_SSL(host,465)
        smtpObj.login(mail_user,mail_pass)
        smtpObj.sendmail(sender,receivers,message.as_string())
        print("mail sent success!")
    except smtplib.SMTPException as e:
        print(e)
关注
打赏
1657016083
查看更多评论
立即登录/注册

微信扫码登录

0.0372s