python 之邮件发送

  • 发送邮件脚本
import smtplib
from email.mime.text import MIMEText


def SendMail():
    mail_host = "smtp.163.com" #邮箱服务
    mail_user = "xxx@163.com" #发件人邮箱
    mail_pass = "xxx" # 开启的密码,smtp

    sender = "xxx@163.com" #发件人邮箱
    receuvers = ['xxxx@163.com'] #收件人邮箱

    content = "Python Send Mail!"
    title = "Python SMTP Mail Test"

    data = {
        "title": title,
        "content": content
    }

    #信息拼接
    message = MIMEText(data['content'],'Plain', "utf-8")
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receuvers)
    message['Subject'] = data['title']
   
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465) #加密
        smtpObj.login(mail_user,mail_pass)  # 登录
        smtpObj.sendmail(sender,receuvers,message.as_string()) # 发送邮件
        print("mail has been send successfully")
    except smtplib.SMTPException as e:
        print(e)

if __name__ == '__main__':
    SendMail()
posted @ 2021-01-18 15:18  Mr.zou  阅读(108)  评论(0编辑  收藏  举报