孙士君 發表於 2019-5-10 12:00:00

python接口自动化(三十三)-python自动发邮件总结及实例说明番外篇——下(详解)

<h2>简介</h2>
<div>
<div>
<p>  发邮件前我们需要了解的是邮件是怎么一个形式去发送到对方手上的,通俗点来说就是你写好一封信,然后装进信封,写上地址,贴上邮票,然后就近找个邮局,把信仍进去,其他的就不关心了,只是关心时间,而电子邮件不像日常发送邮件的按天算,时间都是按</p>
<p>秒算的。</p>
<p>电子邮件的发送流程:</p>
<p>1、你使用某款电子邮件软件MUA:mail user agent --邮件用户代理,填写完成点击发送</p>
<p>2、在你点击发送的时候电子邮件软件发出去,到MTA:mail transfer agent--邮件传输代理,即email服务提供商,如网易等</p>
<p>3、MTA--邮箱传输代理会把邮箱投递到邮件的最终目的地MDA:mail delivery agent---邮箱投递服务</p>
<p>4、email到达MDA后就会放在某个服务器上,存在数据库里,收件人必须通过MUA从MDA中把邮箱放到自己电脑上</p>
<p>发件人 -&gt; MUA -&gt; MTA -&gt; MTA -&gt; 若干个MTA -&gt; MDA&lt;-MUA&lt;-收件人</p>
</div>
</div>
<p>python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。</p>
<p>smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。</p>
<p>email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190510090233349-828034619.png" alt=""></p>
<h2><strong>1.smtplib模块</strong></h2>
<p>smtplib使用较为简单。以下是最基本的语法。</p>
<p>导入及使用方法如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">import smtplib
</span><span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> smtp =<span style="color: rgba(0, 0, 0, 1)"> smtplib.SMTP()
</span><span style="color: rgba(0, 128, 128, 1)">4</span> smtp.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">smtp.163.com,25</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, 128, 1)">5</span> <span style="color: rgba(0, 0, 0, 1)">smtp.login(username, password)
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)">smtp.sendmail(sender, receiver, msg.as_string())
</span><span style="color: rgba(0, 128, 128, 1)">7</span> smtp.quit()</pre>
</div>
<p>说明:</p>
<p>smtplib.SMTP():实例化SMTP()</p>
<p>connect(host,port):</p>
<p>host:指定连接的邮箱服务器。常用邮箱的smtp服务器地址如下:</p>
<p>新浪邮箱:smtp.sina.com,新浪VIP:smtp.vip.sina.com,搜狐邮箱:smtp.sohu.com,126邮箱:smtp.126.com,139邮箱:smtp.139.com,163网易邮箱:smtp.163.com。</p>
<p>port:指定连接服务器的端口号,默认为25.</p>
<p>login(user,password):</p>
<p>user:登录邮箱的用户名。</p>
<p>password:登录邮箱的密码,像笔者用的是网易邮箱,网易邮箱一般是网页版,需要用到客户端密码,需要在网页版的网易邮箱中设置授权码,该授权码即为客户端密码。</p>
<p>sendmail(from_addr,to_addrs,msg,...):</p>
<p>from_addr:邮件发送者地址</p>
<p>to_addrs:邮件接收者地址。发送邮件,多人时用list,字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址'</p>
<p>msg:发送消息:邮件内容。邮件正文是一个str,一般是msg.as_string():as_string()是将msg(MIMEText对象或者MIMEMultipart对象)变为str。</p>
<p>quit(): 退出关闭邮箱,用于结束SMTP会话。</p>
<h2><strong>2.email模块</strong></h2>
<p>email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。</p>
<p>该mime包下常用的有三个模块:<strong>text<strong>,image,</strong>multpart</strong>。</p>
<p>导入方法如下:</p>
<div class="cnblogs_code">
<pre>from email.mime.multipart import MIMEMultipart   
from email.mime.text import MIMEText   
from email.mime.image import MIMEImage</pre>
</div>
<p>构造一个邮件对象就是一个<code>Message</code>对象,如果构造一个<code>MIMEText</code>对象,就表示一个文本邮件对象,如果构造一个<code>MIMEImage</code>对象,就表示一个作为附件的图片,要把多个对象组合起来,就用<code>MIMEMultipart</code>对象,而<code>MIMEBase</code>可以表示任何对象。它们的继承关系如下:</p>
<pre><code class="undefined">Message
+- MIMEBase
   +- MIMEMultipart
   +- MIMENonMultipart
      +- MIMEMessage
      +- MIMEText
      +- MIMEImage</code></pre>
<p>2.1 text说明</p>
<p>邮件发送程序为了防止有些邮件阅读软件不能显示处理HTML格式的数据,通常都会用两类型分别为"text/plain"和"text/html"</p>
<p>构造MIMEText对象时,第一个参数是邮件正文,第二个参数是MIME的subtype,最后一定要用utf-8编码保证多语言兼容性。</p>
<p>2.1.1添加普通文本</p>
<p>方法很简单,在构造MIMEText对象时,把TEXT字符串传进去,再把第二个参数plain和第三个参数编码传进去即可。plain表示纯文本 ,后面的则是编译,保证多语言兼容&nbsp;</p>
<div class="cnblogs_code">
<pre>text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.cnblogs.com/du-hong"   
text_plain = MIMEText(text,'plain', 'utf-8')    </pre>
</div>
<p>查看MIMEText属性:<strong>可以观察到MIMEText,MIMEImage和MIMEMultipart的属性都一样。</strong></p>
<div class="cnblogs_code">
<pre>print dir(text_plain)</pre>
</div>
<p>['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '__setitem__', '__str__', '_charset', '_default_type', '_get_params_preserve', '_headers', '_payload', '_unixfrom', 'add_header', 'as_string', 'attach', 'defects', 'del_param', 'epilogue', 'get', 'get_all', 'get_boundary', 'get_charset', 'get_charsets', 'get_content_charset', 'get_content_maintype', 'get_content_subtype', 'get_content_type', 'get_default_type', 'get_filename', 'get_param', 'get_params', 'get_payload', 'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys', 'preamble', 'replace_header', 'set_boundary', 'set_charset', 'set_default_type', 'set_param', 'set_payload', 'set_type', 'set_unixfrom', 'values', 'walk']</p>
<p>2.1.2添加超文本(HTML)</p>
<p>方法也很简单,在构造MIMEText对象时,把HTML字符串传进去,再把第二个参数由plain变为html就可以了</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> html = <span style="color: rgba(128, 0, 0, 1)">"""
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> &lt;html&gt;
<span style="color: rgba(0, 128, 128, 1)"> 3</span>   &lt;body&gt;
<span style="color: rgba(0, 128, 128, 1)"> 4</span>   &lt;p&gt;
<span style="color: rgba(0, 128, 128, 1)"> 5</span>      Here <span style="color: rgba(0, 0, 255, 1)">is</span> the &lt;a href=<span style="color: rgba(128, 0, 0, 1)">"https://www.cnblogs.com/du-hong</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;link&lt;/a&gt;<span style="color: rgba(0, 0, 0, 1)"> you wanted.
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>   &lt;/p&gt;
<span style="color: rgba(0, 128, 128, 1)"> 7</span>   &lt;/body&gt;
<span style="color: rgba(0, 128, 128, 1)"> 8</span> &lt;/html&gt;
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 0, 1)">"""</span>   
<span style="color: rgba(0, 128, 128, 1)">10</span> text_html = MIMEText(html,<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>, <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>)</pre>
</div>
<p>2.1.3添加附件</p>
<p>首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送</p>
<div class="cnblogs_code">
<pre>sendfile=open(r'D:\pythontest\text.txt','rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')   
text_att["Content-Type"] = 'application/octet-stream'   
text_att["Content-Disposition"] = 'attachment; filename="显示的名字.txt"'</pre>
</div>
<p>2.2 image说明</p>
<p>添加图片:</p>
<div class="cnblogs_code">
<pre>sendimagefile=open(r'D:\pythontest\testimage.png','rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID','&lt;image1&gt;')</pre>
</div>
<p>查看MIMEImage属性:</p>
<div class="cnblogs_code">
<pre>print dir(image)</pre>
</div>
<p>['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '__setitem__', '__str__', '_charset', '_default_type', '_get_params_preserve', '_headers', '_payload', '_unixfrom', 'add_header', 'as_string', 'attach', 'defects', 'del_param', 'epilogue', 'get', 'get_all', 'get_boundary', 'get_charset', 'get_charsets', 'get_content_charset', 'get_content_maintype', 'get_content_subtype', 'get_content_type', 'get_default_type', 'get_filename', 'get_param', 'get_params', 'get_payload', 'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys', 'preamble', 'replace_header', 'set_boundary', 'set_charset', 'set_default_type', 'set_param', 'set_payload', 'set_type', 'set_unixfrom', 'values', 'walk']</p>
<p>2.3 multpart说明</p>
<p>常见的multipart类型有三种:multipart/alternative, multipart/related和multipart/mixed。</p>
<p>邮件类型为"multipart/alternative"的邮件包括纯文本正文(text/plain)和超文本正文(text/html)。</p>
<p>邮件类型为"multipart/related"的邮件正文中包括图片,声音等内嵌资源。</p>
<p>邮件类型为"multipart/mixed"的邮件包含附件。向上兼容,如果一个邮件有纯文本正文,超文本正文,内嵌资源,附件,则选择mixed类型。</p>
<div class="cnblogs_code">
<pre>msg = MIMEMultipart('mixed')</pre>
</div>
<p>我们必须把Subject,From,To,Date添加到MIMEText对象或者MIMEMultipart对象中,邮件中才会显示主题,发件人,收件人,时间(若无时间,就默认一般为当前时间,该值一般不设置)。</p>
<div class="cnblogs_code">
<pre>msg = MIMEMultipart('mixed')
msg['Subject'] = 'Python email test'
msg['From'] = 'XXX@163.com &lt;XXX@163.com&gt;'
msg['To'] = 'XXX@126.com'
msg['Date']='2019-5-10'</pre>
</div>
<p>查看MIMEMultipart属性:</p>
<div class="cnblogs_code">
<pre>msg = MIMEMultipart('mixed')
print dir(msg) </pre>
</div>
<p>结果:</p>
<p>['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '__setitem__', '__str__', '_charset', '_default_type', '_get_params_preserve', '_headers', '_payload', '_unixfrom', 'add_header', 'as_string', 'attach', 'defects', 'del_param', 'epilogue', 'get', 'get_all', 'get_boundary', 'get_charset', 'get_charsets', 'get_content_charset', 'get_content_maintype', 'get_content_subtype', 'get_content_type', 'get_default_type', 'get_filename', 'get_param', 'get_params', 'get_payload', 'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys', 'preamble', 'replace_header', 'set_boundary', 'set_charset', 'set_default_type', 'set_param', 'set_payload', 'set_type', 'set_unixfrom', 'values', 'walk']</p>
<p>说明:</p>
<p>msg.add_header(_name,_value,**_params):添加邮件头字段。</p>
<p>msg.as_string():是将msg(MIMEText对象或者MIMEMultipart对象)变为str,<strong>如果只有一个html超文本正文或者plain普通文本正文的话,一般msg的类型可以是MIMEText;如果是多个的话,就都添加到MIMEMultipart,msg类型就变为MIMEMultipart。</strong></p>
<p>msg.attach(MIMEText对象或MIMEImage对象):将MIMEText对象或MIMEImage对象添加到MIMEMultipart对象中。MIMEMultipart对象代表邮件本身,MIMEText对象或MIMEImage对象代表邮件正文。</p>
<p>以上的构造的文本,超文本,附件,图片都何以添加到MIMEMultipart('mixed')中:</p>
<div class="cnblogs_code">
<pre>msg.attach(text_plain)   
msg.attach(text_html)   
msg.attach(text_att)   
msg.attach(image)</pre>
</div>
<h2><strong>3.文字,html,图片,附件实现实例</strong></h2>
<p>3.1实例</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190510112838820-1608628037.png" alt=""></p>
<p>3.2运行结果</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190510113010537-33392216.png" alt=""></p>
<p>3.3参考代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> # coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> #<span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> #<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">10</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> @author: 北京-<span style="color: rgba(0, 0, 0, 1)">宏哥
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">Project:学习和使用邮箱发文字,html,图片,附件实现实例邮件
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(128, 0, 0, 1)">'''
</span><span style="color: rgba(0, 128, 128, 1)">10</span> #<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入模块
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">import smtplib
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> email.mime.multipart import MIMEMultipart
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> email.mime.text import MIMEText
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> email.mime.image import MIMEImage
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> email.header import Header
</span><span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)"># 设置smtplib所需的参数
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"># 下面的发件人,收件人是用于邮件传输的。
</span><span style="color: rgba(0, 128, 128, 1)">19</span> smtpserver = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">smtp.mxhichina.com</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> username = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">noreply@xxx.cn</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> password = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">xxxx@@xx3</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">22</span> sender = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">noreply@xxx.cn</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 0, 0, 1)"># 收件人为一个收件人
</span><span style="color: rgba(0, 128, 128, 1)">24</span> # receiver=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">XXX@126.com</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 0, 1)"># 收件人为多个收件人
</span><span style="color: rgba(0, 128, 128, 1)">26</span> receiver = [<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1918991791@qq.com</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)">2014816656@qq.com</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)">hongge@xxx.cn</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, 128, 1)">27</span>
<span style="color: rgba(0, 128, 128, 1)">28</span> subject = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Python email test</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">29</span> # 通过Header对象编码的文本,包含utf-<span style="color: rgba(0, 0, 0, 1)">8编码信息和Base64编码信息。以下中文名测试ok
</span><span style="color: rgba(0, 128, 128, 1)">30</span> # subject = <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, 128, 1)">31</span> # subject=Header(subject, <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)">).encode()
</span><span style="color: rgba(0, 128, 128, 1)">32</span>
<span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 0, 0, 1)"># 构造邮件对象MIMEMultipart对象
</span><span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 0, 0, 1)"># 下面的主题,发件人,收件人,日期是显示在邮件页面上的。
</span><span style="color: rgba(0, 128, 128, 1)">35</span> msg = MIMEMultipart(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">mixed</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, 128, 1)">36</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, 128, 1)">37</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(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">北京-宏哥&lt;noreply@xxx.cn&gt;</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">38</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(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1918991791@qq.com</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)">2014816656@qq.com</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)">hongge@xxx.cn</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">39</span> <span style="color: rgba(0, 0, 0, 1)"># 收件人为多个收件人,通过join将列表转换为以;为间隔的字符串
</span><span style="color: rgba(0, 128, 128, 1)">40</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(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, 0, 0, 1)">.join(receiver)
</span><span style="color: rgba(0, 128, 128, 1)">41</span> # msg[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Date</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)">2019-5-10</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">42</span>
<span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)"># 构造文字内容
</span><span style="color: rgba(0, 128, 128, 1)">44</span> text = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.cnblogs.com/du-hong</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">45</span> text_plain = MIMEText(text, <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>, <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, 128, 1)">46</span> <span style="color: rgba(0, 0, 0, 1)">msg.attach(text_plain)
</span><span style="color: rgba(0, 128, 128, 1)">47</span>
<span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 0, 0, 1)"># 构造图片链接
</span><span style="color: rgba(0, 128, 128, 1)">49</span> sendimagefile = open(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E:\pythontest\testimage.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)">).read()
</span><span style="color: rgba(0, 128, 128, 1)">50</span> image =<span style="color: rgba(0, 0, 0, 1)"> MIMEImage(sendimagefile)
</span><span style="color: rgba(0, 128, 128, 1)">51</span> image.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)">&lt;image1&gt;</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, 128, 1)">52</span> image[<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; filename="honggeimage.png"</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">53</span> <span style="color: rgba(0, 0, 0, 1)">msg.attach(image)
</span><span style="color: rgba(0, 128, 128, 1)">54</span>
<span style="color: rgba(0, 128, 128, 1)">55</span> <span style="color: rgba(0, 0, 0, 1)"># 构造html
</span><span style="color: rgba(0, 128, 128, 1)">56</span> # 发送正文中的图片:由于包含未被许可的信息,网易邮箱定义为垃圾邮件,报554 DT:SPM :&lt;p&gt;&lt;img src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cid:image1</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/p&gt;
<span style="color: rgba(0, 128, 128, 1)">57</span> html = <span style="color: rgba(128, 0, 0, 1)">"""
</span><span style="color: rgba(0, 128, 128, 1)">58</span> &lt;html&gt;
<span style="color: rgba(0, 128, 128, 1)">59</span>   &lt;head&gt;&lt;/head&gt;
<span style="color: rgba(0, 128, 128, 1)">60</span>   &lt;body&gt;
<span style="color: rgba(0, 128, 128, 1)">61</span>   &lt;p&gt;Hi!&lt;br&gt;
<span style="color: rgba(0, 128, 128, 1)">62</span>      How are you?&lt;br&gt;
<span style="color: rgba(0, 128, 128, 1)">63</span>      Here <span style="color: rgba(0, 0, 255, 1)">is</span> the &lt;a href=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://www.cnblogs.com/du-hong/</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;link 北京-宏哥&lt;/a&gt; you wanted.&lt;br&gt;
<span style="color: rgba(0, 128, 128, 1)">64</span>   &lt;/p&gt;
<span style="color: rgba(0, 128, 128, 1)">65</span>   &lt;/body&gt;
<span style="color: rgba(0, 128, 128, 1)">66</span> &lt;/html&gt;
<span style="color: rgba(0, 128, 128, 1)">67</span> <span style="color: rgba(128, 0, 0, 1)">"""
</span><span style="color: rgba(0, 128, 128, 1)">68</span> text_html = MIMEText(html, <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>, <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, 128, 1)">69</span> text_html[<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; filename="texthtml.html"</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">70</span> <span style="color: rgba(0, 0, 0, 1)">msg.attach(text_html)
</span><span style="color: rgba(0, 128, 128, 1)">71</span>
<span style="color: rgba(0, 128, 128, 1)">72</span> <span style="color: rgba(0, 0, 0, 1)"># 构造附件
</span><span style="color: rgba(0, 128, 128, 1)">73</span> sendfile = open(r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">E:\pythontest\text.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><span style="color: rgba(0, 0, 0, 1)">).read()
</span><span style="color: rgba(0, 128, 128, 1)">74</span> text_att = MIMEText(sendfile, <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)">)
</span><span style="color: rgba(0, 128, 128, 1)">75</span> text_att[<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, 128, 1)">76</span> <span style="color: rgba(0, 0, 0, 1)"># 以下附件可以重命名成aaa.txt
</span><span style="color: rgba(0, 128, 128, 1)">77</span> # text_att[<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; filename="aaa.txt"</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">78</span> <span style="color: rgba(0, 0, 0, 1)"># 另一种实现方式
</span><span style="color: rgba(0, 128, 128, 1)">79</span> text_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)">hongge.txt</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, 128, 1)">80</span> <span style="color: rgba(0, 0, 0, 1)"># 以下中文测试不ok
</span><span style="color: rgba(0, 128, 128, 1)">81</span> # text_att[<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>] = u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">attachment; filename="中文附件.txt"</span><span style="color: rgba(128, 0, 0, 1)">'</span>.decode(<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, 128, 1)">82</span> <span style="color: rgba(0, 0, 0, 1)">msg.attach(text_att)
</span><span style="color: rgba(0, 128, 128, 1)">83</span>
<span style="color: rgba(0, 128, 128, 1)">84</span> <span style="color: rgba(0, 0, 0, 1)"># 发送邮件
</span><span style="color: rgba(0, 128, 128, 1)">85</span> smtp =<span style="color: rgba(0, 0, 0, 1)"> smtplib.SMTP()
</span><span style="color: rgba(0, 128, 128, 1)">86</span> smtp.connect(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">smtp.mxhichina.com</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, 128, 1)">87</span> # 我们用set_debuglevel(<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)就可以打印出和SMTP服务器交互的所有信息。
</span><span style="color: rgba(0, 128, 128, 1)">88</span> # smtp.set_debuglevel(<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">89</span> <span style="color: rgba(0, 0, 0, 1)">smtp.login(username, password)
</span><span style="color: rgba(0, 128, 128, 1)">90</span> <span style="color: rgba(0, 0, 0, 1)">smtp.sendmail(sender, receiver, msg.as_string())
</span><span style="color: rgba(0, 128, 128, 1)">91</span> smtp.quit()</pre>
</div>
<p>说明:</p>
<div>
<div>
<p>对有些 header 要特别留意,服务器会针对这些 header 做检查</p>
<p>User-Agent : 有些服务器或 Proxy 会通过该值来判断是否是浏览器发出的请求</p>
<p>Content-Type : 在使用 REST 接口时,服务器会检查该值,用来确定 HTTP Body 中的内容该怎样解析。常见的取值有:</p>
<p>application/xml : 在 XML RPC,如 RESTful/SOAP 调用时使用</p>
<p>application/json : 在 JSON RPC 调用时使用</p>
<p>application/x-www-form-urlencoded : 浏览器提交 Web 表单时使用</p>
<p>在使用服务器提供的 RESTful 或 SOAP 服务时, Content-Type 设置错误会导致服务器拒绝服务</p>
</div>
</div>
<h2>小结</h2>
<p>&nbsp;  好了,哈哈,到这里把python发邮件的十八辈祖宗都被我们挖出来了,自己都感觉到有点缺德,但是我们学习就是需要这种精神,但是也不要过于钻牛角筋,要适可而止。想必小伙伴们对python发邮件有了更深刻的认识了,以后遇到类似的问题,</p>
<p>往上套就可以了,但是要注意方式和方法,不要生搬硬套,生搬硬套又会出现问题,要灵活有技巧的套。</p>

</div>
<div id="MySignature" role="contentinfo">
    <div id="MySignature" style="display: block">
        <div style="font-size: 13px; border: 1px dashed rgb(45, 161, 45); padding: 10px 15px; background-color: rgb(248, 248, 248)">
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家在移动端也能看到我分享的博文,现已注册个人微信公众号,扫描左下方二维码即可,欢迎大家关注,提前解锁更多测试干货!有时间会及时分享相关技术博文。
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,刚刚建立了咱们的专门的微信群交流互动群,群内会分享交流测试领域前沿知识。请您扫描中间的微信二维码进群
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,现已组建专门的微信群,由于微信群满100,请您扫描右下方宏哥个人微信二维码拉你进群
                        <label style="font-weight: bold; color: red; font-size: 15px">
                                (请务必备注:已关注公众号进群)平时上班忙(和你一样),所以加好友不及时,请稍安勿躁~
                        </label>
                        ,欢迎大家加入这个大家庭,我们一起畅游知识的海洋。
                </label>
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;如果您觉得阅读本文对您有帮助,请点一下左下角
               
                        “推荐”
               
                按钮,您的
                <label style="font-weight: bold; color: red; font-size: 15px">
                        “推荐”
                </label>
                将是我最大的写作动力!另外您也可以选择
               
                        【
                        <strong>
                                关注我
                        </strong>
                        】
               
                ,可以很方便找到我!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;本文版权归作者和博客园共有,来源网址:
               
                        https://www.cnblogs.com/du-hong
               
                欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!
        </div>
        <div style="text-align: center; margin-top: 10px">
                <p style=" font-weight: bolder; color: red; ">
                        公众号(关注宏哥)&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace; &NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        微信群(扫码进群) &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;客服微信
                </p>
                <img style="width: 200px;padding-right: 50px;" alt="个人微信公众号" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191119095948011-608816619.png">
                <img style="width: 200px;padding-right: 65px;" alt="微信群" src="https://img2024.cnblogs.com/blog/1232840/202506/1232840-20250610113707419-637869921.png">
                <img style="width: 200px" alt="个人微信" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191106101257091-849954564.png">
        </div>
</div><br><br>
来源:https://www.cnblogs.com/du-hong/p/10832048.html
頁: [1]
查看完整版本: python接口自动化(三十三)-python自动发邮件总结及实例说明番外篇——下(详解)