python3基于email模块发送邮件

winmeanyoung 2020-04-11

#! /usr/bin/env python #coding=utf-8 """ example: python3 send_mail.py """ from email.mime.text import MIMEText from email.header import Header from smtplib import SMTP_SSL from email import encoders from email.utils import parseaddr, formataddr def __format_addr(k): name, addr = parseaddr(k) return formataddr((Header(name, ‘utf-8‘).encode(), addr)) def send_mail(): # 163邮箱smtp服务器 wy_server = ‘smtp.163.com‘ # sender_163为发件人的163号码 sender_163 = ‘*********‘ # pwd为163邮箱的授权码 pwd = ‘*********‘ # 发件人的邮箱 sender_163_mail = ‘*********@163.com‘ # 收件人邮箱 receivers = [‘*********@126.com‘,‘*********@163.com‘] # 邮件的正文内容 mail_content = ‘你好,这是使用python登录163邮箱发邮件的测试‘ # 邮件标题 mail_title = ‘mike的邮件‘ # ssl登录 smtp = SMTP_SSL(wy_server) # set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 smtp.set_debuglevel(0) smtp.ehlo(wy_server) smtp.login(sender_163, pwd) msg = MIMEText(mail_content, "plain", ‘utf-8‘) msg["Subject"] = Header(mail_title, ‘utf-8‘).encode() msg["From"] = __format_addr(sender_163_mail) msg["To"] = __format_addr(receivers) smtp.sendmail(sender_163_mail, receivers, msg.as_string()) smtp.quit() if __name__ == "__main__": send_mail()

相关推荐

李高峰 / 0评论 2020-05-25
Junzizhiai / 0评论 2013-05-13