python3基于email模块发送邮件
#! /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()
相关推荐
zhangpan 2020-01-10
codeAB 2019-10-31
Rain 2020-06-25
88570299 2020-06-18
JasonYeung 2020-06-11
sunnyJam 2020-04-03
llwang0 2020-05-06
vanturman 2020-04-08
keepdoingit 2020-02-23
mmyCSDN 2020-02-10
stulen 2020-01-21
zhouyuqi 2020-01-20
zhiyuan0 2020-01-18
nanbiebao 2020-01-10
疯狂紫萧 2020-01-08
水痕 2019-12-05
peterlee 2011-07-26
wordmhg 2019-10-27