python3基于email模块发送邮件
2021-02-21 00:20
标签:表示 mat python == ext 邮箱 标题 发送邮件 return python3基于email模块发送邮件 标签:表示 mat python == ext 邮箱 标题 发送邮件 return 原文地址:https://blog.51cto.com/liuxiaolan/2486456#! /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()
文章标题:python3基于email模块发送邮件
文章链接:http://soscw.com/index.php/essay/58216.html