python 实现发送邮件功能
<h2> 一、前言</h2><p> 在开始正题之前,我们先理一下常见的电子邮件协议: <span class="bold">SMTP、POP、IMAP 都遵循TCP/IP协议规范。至于Exchange是邮件服务器,不是收邮件和发邮件的协议,不要混淆概念,一般情况下不用,因为它是微软的产品。</span></p>
<ul>
<li><span class="bold"><strong>SMTP:</strong>即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。简单来说是 <strong>发送协议</strong>。</span></li>
<li><span class="bold"><strong>POP:</strong>协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、标记已读等),不会反馈到服务器上。属于 <strong>收件协议。</strong></span></li>
<li><span class="bold"><strong>IMAP:</strong>提供 邮件服务器 与电子邮件客户端之间的双向通信,客户端的操作都会反馈到服务器上,对邮件进行的操作,服务器上的邮件也会做相应的动作(这是和POP协议主要不同点)。属于 <strong>收件协议。</strong></span></li>
</ul>
<p> </p>
<h2><span class="bold"><strong>二、SMTP发送邮件</strong></span></h2>
<p><span class="bold"> Python发送邮件比较简单,掌握两个自带库 smtplib、email 即可,使用的时候直接 import。smtplib 负责发送邮件,email 负责构造邮件(邮件内容,主题,收件人,抄送人等)。</span></p>
<h3> </h3>
<h3><span class="bold">1、发送普通邮件</span></h3>
<p><span class="bold">1)先找到你的发送邮箱,比如我这里是企业邮箱,点击邮箱“设置”—>“客户端设置”,开启 SMTP 服务器。发送服务器配置使用:smtp.exmail.qq.com(使用SSL,端口号465)</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1236854/202101/1236854-20210112173702643-1349373264.png"></p>
<p> </p>
<p> 2)smtplib、email 使用如下。其中 MIMEText 构造对象时,第一个参数是邮件正文;第二个参数是subType,可以设置两种格式 'plain' 和 'html';然后一定要指定字符编码‘utf-8’。‘plain’ 表示发送纯文本消息。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> smtplib <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> SMTP_SSL
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.text <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEText
</span><span style="color: rgba(0, 0, 255, 1)">def</span> sendMail(message,Subject,sender_show,recipient_show,to_addrs,cc_show=<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">):
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
:param message: str 邮件内容
:param Subject: str 邮件主题描述
:param sender_show: str 发件人显示,不起实际作用如:"xxx"
:param recipient_show: str 收件人显示,不起实际作用 多个收件人用','隔开如:"xxx,xxxx"
:param to_addrs: str 实际收件人
:param cc_show: str 抄送人显示,不起实际作用,多个抄送人用','隔开如:"xxx,xxxx"
</span><span style="color: rgba(128, 0, 0, 1)">'''</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 填写真实的发邮件服务器用户名、密码</span>
user = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
password </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件内容</span>
msg = MIMEText(message, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">plain</span><span style="color: rgba(128, 0, 0, 1)">'</span>, _charset=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件主题描述</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Subject</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> Subject
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 发件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">from</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> sender_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 收件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">to</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> recipient_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 抄送人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Cc</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> cc_show
with SMTP_SSL(host</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">smtp.exmail.qq.com</span><span style="color: rgba(128, 0, 0, 1)">"</span>,port=465<span style="color: rgba(0, 0, 0, 1)">) as smtp:
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 登录发邮件服务器</span>
smtp.login(user = user, password =<span style="color: rgba(0, 0, 0, 1)"> password)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发送、接收邮件配置</span>
smtp.sendmail(from_addr = user, to_addrs=to_addrs.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">,</span><span style="color: rgba(128, 0, 0, 1)">'</span>), msg=<span style="color: rgba(0, 0, 0, 1)">msg.as_string())
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> ==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
message </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Python 测试邮件...</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
Subject </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">主题测试</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示发送人</span>
sender_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示收件人</span>
recipient_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发给的收件人</span>
to_addrs = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx@qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
sendMail(message,Subject,sender_show,recipient_show,to_addrs)</span></pre>
</div>
<p> 邮件效果:</p>
<p><img src="https://img2020.cnblogs.com/blog/1236854/202101/1236854-20210112182157652-297970907.png"></p>
<p> </p>
<h3>2、发送HTML格式邮件</h3>
<p> 将 MIMEText 第二个参数设置成 'html',如:MIMEText(message, 'html', _charset="utf-8") </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> smtplib <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> SMTP_SSL
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.text <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEText
</span><span style="color: rgba(0, 0, 255, 1)">def</span> sendMail(message,Subject,sender_show,recipient_show,to_addrs,cc_show=<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">):
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
:param message: str 邮件内容
:param Subject: str 邮件主题描述
:param sender_show: str 发件人显示,不起实际作用如:"xxx"
:param recipient_show: str 收件人显示,不起实际作用 多个收件人用','隔开如:"xxx,xxxx"
:param to_addrs: str 实际收件人
:param cc_show: str 抄送人显示,不起实际作用,多个抄送人用','隔开如:"xxx,xxxx"
</span><span style="color: rgba(128, 0, 0, 1)">'''</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 填写真实的发邮件服务器用户名、密码</span>
user = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
password </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件内容</span>
msg = MIMEText(message, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(128, 0, 0, 1)">'</span>, _charset=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件主题描述</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Subject</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> Subject
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 发件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">from</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> sender_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 收件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">to</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> recipient_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 抄送人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Cc</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> cc_show
with SMTP_SSL(host</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">smtp.exmail.qq.com</span><span style="color: rgba(128, 0, 0, 1)">"</span>,port=465<span style="color: rgba(0, 0, 0, 1)">) as smtp:
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 登录发送邮件服务器</span>
smtp.login(user = user, password =<span style="color: rgba(0, 0, 0, 1)"> password)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发送、接收邮件配置</span>
smtp.sendmail(from_addr = user, to_addrs=to_addrs.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">,</span><span style="color: rgba(128, 0, 0, 1)">'</span>), msg=<span style="color: rgba(0, 0, 0, 1)">msg.as_string())
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> ==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
message </span>= <span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
<p>Python 邮件发送测试...</p>
<p><a href="https://www.baidu.com">纵里寻她千百度</a></p>
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
Subject </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">主题测试</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示发送人</span>
sender_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示收件人</span>
recipient_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发给的收件人</span>
to_addrs = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx@qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
sendMail(message,Subject,sender_show,recipient_show,to_addrs)</span></pre>
</div>
<p> 邮件效果:</p>
<p><img src="https://img2020.cnblogs.com/blog/1236854/202101/1236854-20210113091721585-23792556.png"></p>
<p> </p>
<h3>3、发送带附件邮件</h3>
<p> 发送带附件的邮件需借助类 MIMEMultipart(),然后构造邮件内容及附件,多个附件 attach 多次。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> smtplib <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> SMTP_SSL
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.text <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEText
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.multipart <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEMultipart
</span><span style="color: rgba(0, 0, 255, 1)">def</span> sendMail(message,Subject,sender_show,recipient_show,to_addrs,cc_show=<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">):
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
:param message: str 邮件内容
:param Subject: str 邮件主题描述
:param sender_show: str 发件人显示,不起实际作用如:"xxx"
:param recipient_show: str 收件人显示,不起实际作用 多个收件人用','隔开如:"xxx,xxxx"
:param to_addrs: str 实际收件人
:param cc_show: str 抄送人显示,不起实际作用,多个抄送人用','隔开如:"xxx,xxxx"
</span><span style="color: rgba(128, 0, 0, 1)">'''</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 填写真实的发邮件服务器用户名、密码</span>
user = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
password </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件内容</span>
msg =<span style="color: rgba(0, 0, 0, 1)"> MIMEMultipart()
msg.attach(MIMEText(message, </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(128, 0, 0, 1)">'</span>, _charset=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 构造附件1,传送当前目录下的 test.txt 文件</span>
att = MIMEText(open(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test.txt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rb</span><span style="color: rgba(128, 0, 0, 1)">'</span>).read(), <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">base64</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
att[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Content-Type</span><span style="color: rgba(128, 0, 0, 1)">"</span>] = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">application/octet-stream</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 附件名称为中文时的写法</span>
att.add_header(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Content-Disposition</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">attachment</span><span style="color: rgba(128, 0, 0, 1)">"</span>, filename=(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gbk</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">, filename))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 附件名称非中文时的写法,这里的filename可以任意写,写什么名字,邮件中显示什么名字</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> att["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)</span>
<span style="color: rgba(0, 0, 0, 1)"> msg.attach(att)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件主题描述</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Subject</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> Subject
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 发件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">from</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> sender_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 收件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">to</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> recipient_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 抄送人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Cc</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> cc_show
with SMTP_SSL(host</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">smtp.exmail.qq.com</span><span style="color: rgba(128, 0, 0, 1)">"</span>,port=465<span style="color: rgba(0, 0, 0, 1)">) as smtp:
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 登录发送邮件服务器</span>
smtp.login(user = user, password =<span style="color: rgba(0, 0, 0, 1)"> password)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发送、接收邮件配置</span>
smtp.sendmail(from_addr = user, to_addrs=to_addrs.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">,</span><span style="color: rgba(128, 0, 0, 1)">'</span>), msg=<span style="color: rgba(0, 0, 0, 1)">msg.as_string())
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> ==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
message </span>= <span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
<p>Python 邮件发送测试...</p>
<p><a href="https://www.baidu.com">纵里寻她千百度</a></p>
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
Subject </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">主题测试</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示发送人</span>
sender_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示收件人</span>
recipient_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发给的收件人</span>
to_addrs = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx@qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
sendMail(message,Subject,sender_show,recipient_show,to_addrs)</span></pre>
</div>
<p> 邮件效果:</p>
<p><img src="https://img2020.cnblogs.com/blog/1236854/202101/1236854-20210113100951819-2131055241.png"></p>
<p> </p>
<h3>4、发送带图片邮件</h3>
<p> 邮件带图片需要用到另一个类 MIMEImage(),多张图片 attach 多次。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> smtplib <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> SMTP_SSL
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.text <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEText
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.multipart <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEMultipart
</span><span style="color: rgba(0, 0, 255, 1)">from</span> email.mime.image <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> MIMEImage
</span><span style="color: rgba(0, 0, 255, 1)">def</span> sendMail(message,Subject,sender_show,recipient_show,to_addrs,cc_show=<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">):
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
:param message: str 邮件内容
:param Subject: str 邮件主题描述
:param sender_show: str 发件人显示,不起实际作用如:"xxx"
:param recipient_show: str 收件人显示,不起实际作用 多个收件人用','隔开如:"xxx,xxxx"
:param to_addrs: str 实际收件人
:param cc_show: str 抄送人显示,不起实际作用,多个抄送人用','隔开如:"xxx,xxxx"
</span><span style="color: rgba(128, 0, 0, 1)">'''</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 填写真实的发邮件服务器用户名、密码</span>
user = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">root</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
password </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件内容</span>
msg =<span style="color: rgba(0, 0, 0, 1)"> MIMEMultipart()
msg.attach(MIMEText(message, </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(128, 0, 0, 1)">'</span>, _charset=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 邮件主题描述</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Subject</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> Subject
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 发件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">from</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> sender_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 收件人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">to</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> recipient_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 抄送人显示,不起实际作用</span>
msg[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Cc</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> cc_show
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 指定图片为当前目录</span>
fp = open(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">test.png</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
msgImage </span>=<span style="color: rgba(0, 0, 0, 1)"> MIMEImage(fp.read())
fp.close()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 定义图片 ID,在 HTML 文本中引用</span>
msgImage.add_header(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Content-ID</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"><image></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
msg.attach(msgImage)
with SMTP_SSL(host</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">smtp.exmail.qq.com</span><span style="color: rgba(128, 0, 0, 1)">"</span>,port=465<span style="color: rgba(0, 0, 0, 1)">) as smtp:
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 登录发送邮件服务器</span>
smtp.login(user = user, password =<span style="color: rgba(0, 0, 0, 1)"> password)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发送、接收邮件配置</span>
smtp.sendmail(from_addr = user, to_addrs=to_addrs.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">,</span><span style="color: rgba(128, 0, 0, 1)">'</span>), msg=<span style="color: rgba(0, 0, 0, 1)">msg.as_string())
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> ==<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
message </span>= <span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(128, 0, 0, 1)">
<p>Python 邮件发送测试...</p>
<p><a href="https://www.baidu.com">纵里寻她千百度</a></p>
<p><img src="cid:image"></p>
</span><span style="color: rgba(128, 0, 0, 1)">'''</span><span style="color: rgba(0, 0, 0, 1)">
Subject </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">主题测试</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示发送人</span>
sender_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 显示收件人</span>
recipient_show = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 实际发给的收件人</span>
to_addrs = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxx@qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
sendMail(message,Subject,sender_show,recipient_show,to_addrs)</span></pre>
</div>
<p> 邮件效果:</p>
<p><img src="https://img2020.cnblogs.com/blog/1236854/202101/1236854-20210113102514578-917077534.png"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/shenh/p/14267345.html
頁:
[1]