<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Ops运维 | Mr`kang Blog｜小豆子]]></title> 
<link>https://www.cnmysql.com/index.php</link> 
<description><![CDATA[关注服务器运维技术、DB优化、Docker虚拟化、自动化运维、工作积累]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Ops运维 | Mr`kang Blog｜小豆子]]></copyright>
<item>
<link>https://www.cnmysql.com/post//</link>
<title><![CDATA[Python发送邮件 (STMPLib)]]></title> 
<author>kangyang &lt;kangyang315@qq.com&gt;</author>
<category><![CDATA[Python]]></category>
<pubDate>Wed, 13 Aug 2014 10:16:07 +0000</pubDate> 
<guid>https://www.cnmysql.com/post//</guid> 
<description>
<![CDATA[ 
	<span style="font-size: 12px;"><span style="font-family: 宋体;">python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。<br/><br/>smtp协议的基本命令包括：<br/>HELO 向服务器标识用户身份<br/>MAIL 初始化邮件传输 mail from:<br/>RCPT 标识单个的邮件接收人；常在MAIL命令后面,可有多个rcpt to:<br/>DATA 在单个或多个RCPT命令后,表示所有的邮件接收人已标识,并初始化数据传输,以.结束<br/>VRFY 用于验证指定的用户/邮箱是否存在；由于安全方面的原因,服务器常禁止此命令<br/>EXPN 验证给定的邮箱列表是否存在,扩充邮箱列表,也常被禁用<br/>HELP 查询服务器支持什么命令<br/>NOOP 无操作,服务器应响应OK<br/>QUIT 结束会话<br/>RSET 重置会话,当前传输被取消<br/>MAIL FROM 指定发送者地址<br/>RCPT TO 指明的接收者地址<br/> <br/>Python创建 SMTP 对象:<br/><div class="code">import smtplib<br/>SMTPObj = smtplib.SMTP( &#91;host &#91;,port &#91;,local_hostname&#93;&#93;&#93; )<br/><br/>参数说明：<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;host: SMTP 服务器主机。 你可以指定主机的ip地址或者域名如:i-it.info，这个是可选参数。<br/>&nbsp;&nbsp;&nbsp;&nbsp;port: 如果你提供了 host 参数, 你需要指定 SMTP 服务使用的端口号，一般情况下SMTP端口号为25。<br/>&nbsp;&nbsp;&nbsp;&nbsp;local_hostname: 如果SMTP在你的本机上，你只需要指定服务器地址为 localhost 即可。</div><br/> <br/>python smtp对象使用sendmail方法发送邮件的语法如下：<br/><div class="code">SMTP.sendmail(from_addr, to_addrs, msg&#91;, mail_options, rcpt_options)<br/><br/>参数说明：<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;from_addr: 邮件发送者地址。<br/>&nbsp;&nbsp;&nbsp;&nbsp;to_addrs: 字符串列表，邮件发送地址。<br/>&nbsp;&nbsp;&nbsp;&nbsp;msg: 发送消息 <br/><br/>这里要注意一下第三个参数，msg是字符串，表示邮件。邮件一般由标题，发信人，收件人，邮件内容，附件等构成，发送邮件的时候，要注意msg的格式。这个格式就是smtp协议中定义的格式。</div><br/> <br/>一个发送邮件的小例子:<br/><div class="code">import smtplib<br/><br/>send = &#039;18717776124@163.com&#039;<br/>receivers = &#91;&#039;itchenyi@gmail.com&#039;&#93;<br/>UserPass = &#91;&#039;18717776124@163.com&#039;,&#039;%password%&#039;&#93;<br/><br/>message = &quot;&quot;&quot;From: From Person &lt;18717776124@163.com&gt;<br/>TO: To Person &lt;itchenyi@gmail.com&gt;<br/>Subject: SMTP e-mail test<br/><br/>This is a test email message.<br/>&quot;&quot;&quot;<br/><br/>try:<br/>&nbsp;&nbsp;&nbsp;&nbsp;SMTPObj = smtplib.SMTP(&quot;smtp.163.com&quot;,&quot;25&quot;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;SMTPObj.login(&#039;%s&#039;%UserPass&#91;0&#93;,&quot;%s&quot;%UserPass&#91;1&#93;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;SMTPObj.sendmail(send,receivers,message)<br/>&nbsp;&nbsp;&nbsp;&nbsp;SMTPObj.quit()<br/>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Successfully sent email&quot;<br/>except:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Error: unable to send email&quot;</div><br/> <br/>用python发送HTML格式的邮件:<br/><div class="code">###Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html###<br/>import smtplib<br/>from email.mime.text import MIMEText<br/><br/>mailto_list = &#91;&quot;itchenyi@gmail.com&quot;&#93;<br/>mail_host = &quot;smtp.163.com&quot;<br/>##smtp server##<br/>mail_user = &quot;18717776124&quot;<br/>##username##<br/>mail_pass = &quot;%password%&quot;<br/>##password##<br/>mail_postfix = &quot;163.com&quot;<br/>##后缀##<br/><br/>def send_email(to_list,sub,content):<br/>###定义收件人列表，主题，邮件内容###<br/>me = &quot;hello&quot;+&quot;&lt;&quot;+mail_user+&quot;@&quot;+mail_postfix+&quot;&gt;&quot;<br/>###hello信息###<br/>msg = MIMEText(content,_subtype=&#039;html&#039;,_charset=&#039;utf8&#039;)<br/>###创建实例，设置为html格式##<br/>msg&#91;&#039;Subject&#039;&#93; = sub<br/>###主题###<br/>msg&#91;&#039;From&#039;&#93; = me<br/>msg&#91;&#039;To&#039;&#93; = &quot;;&quot;.join(to_list)<br/>try:<br/>s = smtplib.SMTP()<br/>s.connect(mail_host)<br/>s.login(mail_user,mail_pass)<br/>s.sendmail(me,to_list,msg.as_string())<br/>s.close()<br/>return True<br/>except Exception,e:<br/>print str(e)<br/>return False<br/><br/>if __name__ == &#039;__main__&#039;:<br/>if send_email(mailto_list,&quot;hello&quot;,&quot;&lt;a href=&quot;http://www.ipython.me&quot;&gt;IT辰逸&lt;/a&gt;&quot;):<br/>print &quot;send Successfully&quot;<br/>else:<br/>print &quot;send Error&quot;</div><br/> <br/><br/>Python 发送带附件的邮件：<br/><div class="code">from email.mime.text import MIMEText<br/>from email.mime.multipart import MIMEMultipart<br/>import smtplib<br/><br/>##创建一个带附件的实例##<br/>msg = MIMEMultipart()<br/><br/>##构造附件##<br/>att_one = MIMEText(open(&#039;D:&#92;&#92;testfile&#039;,&#039;rb&#039;).read(),&#039;base64&#039;,&#039;utf8&#039;)<br/>att_one&#91;&quot;Content-Type&quot;&#93; = &#039;application/octet-stream&#039;<br/>att_one&#91;&quot;Content-Disposition&quot;&#93; = &#039;attachment; filename=&quot;testfile&quot;&#039;<br/>msg.attach(att_one)<br/><br/>##构造附件##<br/>att_two = MIMEText(open(&#039;D:&#92;&#92;testfile.zip&#039;,&#039;rb&#039;).read(),&#039;base64&#039;,&#039;utf8&#039;)<br/>att_two&#91;&quot;Content-Type&quot;&#93; = &#039;applecation/octet-stream&#039;<br/>att_two&#91;&quot;Content-Disposition&quot;&#93; = &#039;attachment; filename=&quot;testfile.zip&quot;&#039;<br/>msg.attach(att_two)<br/><br/>##邮件头##<br/>msg&#91;&#039;to&#039;&#93; = &quot;itchenyi@gmail.com&quot;<br/>msg&#91;&#039;from&#039;&#93; = &#039;18717776124@163.com&#039;<br/>msg&#91;&#039;subject&#039;&#93; = &#039;test email&#039;<br/><br/>##发送邮件##<br/>try:<br/>&nbsp;&nbsp;&nbsp;&nbsp;server = smtplib.SMTP()<br/>&nbsp;&nbsp;&nbsp;&nbsp;server.connect(&#039;smtp.163.com&#039;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;server.login(&#039;18717776124@163.com&#039;,&#039;%password%&#039;)<br/>&nbsp;&nbsp;&nbsp;&nbsp;server.sendmail(msg&#91;&#039;from&#039;&#93;,msg&#91;&#039;to&#039;&#93;,msg.as_string())<br/>&nbsp;&nbsp;&nbsp;&nbsp;server.quit()<br/>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;send Successfully&quot;<br/>except Exception,e:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print str(e)</div><br/> <br/>Python 发送带图片的HTML邮件：<br/><div class="code">import smtplib<br/>from email.mime.multipart import MIMEMultipart<br/>from email.mime.text import MIMEText<br/>from email.mime.image import MIMEImage<br/><br/>send = &#039;18717776124@163.com&#039;<br/>receiver = &#039;itchenyi@gmail.com&#039;<br/>subject = &#039;email test&#039;<br/>username = &#039;18717776124@163.com&#039;<br/>password = &#039;%password%&#039;<br/><br/>msg = MIMEMultipart(&#039;related&#039;)<br/>msg&#91;&#039;subject&#039;&#93; = &#039;test email&#039;<br/><br/>msgText = MIMEText(&#039;&lt;b&gt;Some &lt;i&gt;HTML&lt;/i&gt; text&lt;/b&gt; and an image.&lt;br&gt;&lt;img src=&quot;cid:image1&quot;&gt;&lt;br&gt;good!&#039;,&#039;html&#039;,&#039;utf-8&#039;)<br/>msg.attach(msgText)<br/><br/>fp = open(&#039;D:&#92;&#92;i-it.jpg&#039;,&#039;rb&#039;)<br/>msgImage = MIMEImage(fp.read())<br/>fp.close()<br/><br/>msgImage.add_header(&#039;Content-ID&#039;, &#039;&lt;image1&gt;&#039;)<br/>msg.attach(msgImage)<br/><br/>try:<br/>smtp = smtplib.SMTP()<br/>smtp.connect(&#039;smtp.163.com&#039;)<br/>smtp.login(username, password)<br/>smtp.sendmail(send, receiver, msg.as_string())<br/>smtp.quit()<br/>print &quot;send Successfully&quot;<br/>except Exception,e:<br/>print str(e)</div><br/> <br/>Python 发送包含多种元素的邮件<br/><div class="code">import smtplib<br/>from email.mime.multipart import MIMEMultipart<br/>from email.mime.text import MIMEText<br/>from email.mime.image import MIMEImage<br/><br/>send = &#039;18717776124@163.com&#039;<br/>receiver = &#039;itchenyi@gmail.com&#039;<br/>subject = &#039;python email test&#039;<br/>smtpserver = &#039;smtp.163.com&#039;<br/>username = &#039;18717776124@163.com&#039;<br/>password = &#039;%password%&#039;<br/><br/>msg = MIMEMultipart(&#039;alternative&#039;)<br/>msg&#91;&#039;Subject&#039;&#93; = &quot;Link&quot;<br/><br/>text = &quot;Hi!&#92;nHow are you?&quot;<br/>html = &quot;&quot;&quot;<br/>Hi!<br/>How are you?<br/>&quot;&quot;&quot;<br/><br/>part1 = MIMEText(text, &#039;plain&#039;)<br/>part2 = MIMEText(html, &#039;html&#039;)<br/><br/>msg.attach(part1)<br/>msg.attach(part2)<br/><br/>att = MIMEText(open(&#039;d:&#92;&#92;i-it.jpg&#039;, &#039;rb&#039;).read(), &#039;base64&#039;, &#039;utf-8&#039;)<br/>att&#91;&quot;Content-Type&quot;&#93; = &#039;application/octet-stream&#039;<br/>att&#91;&quot;Content-Disposition&quot;&#93; = &#039;attachment; filename=&quot;iit.jpg&quot;&#039;<br/>msg.attach(att)<br/><br/>try:<br/>smtp = smtplib.SMTP()<br/>smtp.connect(&#039;smtp.163.com&#039;)<br/>smtp.login(username, password)<br/>smtp.sendmail(send, receiver, msg.as_string())<br/>smtp.quit()<br/>print &quot;send Successfully&quot;<br/>except Exception,e:<br/>print str(e)</div></span></span><br/>Tags - <a href="https://www.cnmysql.com/tags/python/" rel="tag">python</a> , <a href="https://www.cnmysql.com/tags/mail/" rel="tag">mail</a> , <a href="https://www.cnmysql.com/tags/stmplib/" rel="tag">stmplib</a>
]]>
</description>
</item><item>
<link>https://www.cnmysql.com/post//#blogcomment</link>
<title><![CDATA[[评论] Python发送邮件 (STMPLib)]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.cnmysql.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>