欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python自動(dòng)發(fā)郵件總結(jié)及實(shí)例說(shuō)明【推薦】

 更新時(shí)間:2019年05月31日 11:44:01   作者:古風(fēng)塵  
python發(fā)郵件需要掌握兩個(gè)模塊的用法,smtplib和email,這倆模塊是python自帶的,只需import即可使用。這篇文章主要介紹了python自動(dòng)發(fā)郵件總結(jié)及實(shí)例說(shuō)明 ,需要的朋友可以參考下

python發(fā)郵件需要掌握兩個(gè)模塊的用法,smtplib和email,這倆模塊是python自帶的,只需import即可使用。smtplib模塊主要負(fù)責(zé)發(fā)送郵件,email模塊主要負(fù)責(zé)構(gòu)造郵件。

smtplib模塊主要負(fù)責(zé)發(fā)送郵件:是一個(gè)發(fā)送郵件的動(dòng)作,連接郵箱服務(wù)器,登錄郵箱,發(fā)送郵件(有發(fā)件人,收信人,郵件內(nèi)容)。

email模塊主要負(fù)責(zé)構(gòu)造郵件:指的是郵箱頁(yè)面顯示的一些構(gòu)造,如發(fā)件人,收件人,主題,正文,附件等。

1.smtplib模塊

smtplib使用較為簡(jiǎn)單。以下是最基本的語(yǔ)法。

導(dǎo)入及使用方法如下:

import smtplib
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com,25') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

說(shuō)明:

smtplib.SMTP():實(shí)例化SMTP()

connect(host,port):

host:指定連接的郵箱服務(wù)器。常用郵箱的smtp服務(wù)器地址如下:

新浪郵箱:smtp.sina.com,新浪VIP:smtp.vip.sina.com,搜狐郵箱:smtp.sohu.com,126郵箱:smtp.126.com,139郵箱:smtp.139.com,163網(wǎng)易郵箱:smtp.163.com。

port:指定連接服務(wù)器的端口號(hào),默認(rèn)為25.

login(user,password):

user:登錄郵箱的用戶名。

password:登錄郵箱的密碼,像筆者用的是網(wǎng)易郵箱,網(wǎng)易郵箱一般是網(wǎng)頁(yè)版,需要用到客戶端密碼,需要在網(wǎng)頁(yè)版的網(wǎng)易郵箱中設(shè)置授權(quán)碼,該授權(quán)碼即為客戶端密碼。

sendmail(from_addr,to_addrs,msg,...):

from_addr:郵件發(fā)送者地址

to_addrs:郵件接收者地址。字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址'

msg:發(fā)送消息:郵件內(nèi)容。一般是msg.as_string():as_string()是將msg(MIMEText對(duì)象或者M(jìn)IMEMultipart對(duì)象)變?yōu)閟tr。

quit():用于結(jié)束SMTP會(huì)話。

2.email模塊

email模塊下有mime包,mime英文全稱為“Multipurpose Internet Mail Extensions”,即多用途互聯(lián)網(wǎng)郵件擴(kuò)展,是目前互聯(lián)網(wǎng)電子郵件普遍遵循的郵件技術(shù)規(guī)范。

該mime包下常用的有三個(gè)模塊:text,image,multpart。

導(dǎo)入方法如下:

from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage

構(gòu)造一個(gè)郵件對(duì)象就是一個(gè)Message對(duì)象,如果構(gòu)造一個(gè)MIMEText對(duì)象,就表示一個(gè)文本郵件對(duì)象,如果構(gòu)造一個(gè)MIMEImage對(duì)象,就表示一個(gè)作為附件的圖片,要把多個(gè)對(duì)象組合起來(lái),就用MIMEMultipart對(duì)象,而MIMEBase可以表示任何對(duì)象。它們的繼承關(guān)系如下:

Message
+- MIMEBase
 +- MIMEMultipart
 +- MIMENonMultipart
  +- MIMEMessage
  +- MIMEText
  +- MIMEImage

2.1 text說(shuō)明

郵件發(fā)送程序?yàn)榱朔乐褂行┼]件閱讀軟件不能顯示處理HTML格式的數(shù)據(jù),通常都會(huì)用兩類型分別為"text/plain"和"text/html"

構(gòu)造MIMEText對(duì)象時(shí),第一個(gè)參數(shù)是郵件正文,第二個(gè)參數(shù)是MIME的subtype,最后一定要用utf-8編碼保證多語(yǔ)言兼容性。

2.1.1添加普通文本

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com" 
text_plain = MIMEText(text,'plain', 'utf-8') 

查看MIMEText屬性:可以觀察到MIMEText,MIMEImage和MIMEMultipart的屬性都一樣。

print dir(text_plain)

['__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']

2.1.2添加超文本

html = """
<html> 
 <body> 
 <p> 
  Here is the <a >link</a> you wanted.
 </p> 
 </body> 
</html> 
""" 
text_html = MIMEText(html,'html', 'utf-8') 

2.1.3添加附件

sendfile=open(r'D:\pythontest\1111.txt','rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8') 
text_att["Content-Type"] = 'application/octet-stream' 
text_att["Content-Disposition"] = 'attachment; filename="顯示的名字.txt"' 

2.2 image說(shuō)明

添加圖片:

sendimagefile=open(r'D:\pythontest\testimage.png','rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID','<image1>')

查看MIMEImage屬性:

print dir(image)

['__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']

2.3 multpart說(shuō)明

常見的multipart類型有三種:multipart/alternative, multipart/related和multipart/mixed。

郵件類型為"multipart/alternative"的郵件包括純文本正文(text/plain)和超文本正文(text/html)。

郵件類型為"multipart/related"的郵件正文中包括圖片,聲音等內(nèi)嵌資源。

郵件類型為"multipart/mixed"的郵件包含附件。向上兼容,如果一個(gè)郵件有純文本正文,超文本正文,內(nèi)嵌資源,附件,則選擇mixed類型。

msg = MIMEMultipart('mixed')

我們必須把Subject,F(xiàn)rom,To,Date添加到MIMEText對(duì)象或者M(jìn)IMEMultipart對(duì)象中,郵件中才會(huì)顯示主題,發(fā)件人,收件人,時(shí)間(若無(wú)時(shí)間,就默認(rèn)一般為當(dāng)前時(shí)間,該值一般不設(shè)置)。

msg = MIMEMultipart('mixed') 
msg['Subject'] = 'Python email test'
msg['From'] = 'XXX@163.com <XXX@163.com>'
msg['To'] = 'XXX@126.com'
msg['Date']='2012-3-16'

查看MIMEMultipart屬性:

msg = MIMEMultipart('mixed') 
print dir(msg)

結(jié)果:

['__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']

說(shuō)明:

msg.add_header(_name,_value,**_params):添加郵件頭字段。

msg.as_string():是將msg(MIMEText對(duì)象或者M(jìn)IMEMultipart對(duì)象)變?yōu)閟tr,如果只有一個(gè)html超文本正文或者plain普通文本正文的話,一般msg的類型可以是MIMEText;如果是多個(gè)的話,就都添加到MIMEMultipart,msg類型就變?yōu)镸IMEMultipart。

msg.attach(MIMEText對(duì)象或MIMEImage對(duì)象):將MIMEText對(duì)象或MIMEImage對(duì)象添加到MIMEMultipart對(duì)象中。MIMEMultipart對(duì)象代表郵件本身,MIMEText對(duì)象或MIMEImage對(duì)象代表郵件正文。

以上的構(gòu)造的文本,超文本,附件,圖片都何以添加到MIMEMultipart('mixed')中:

msg.attach(text_plain) 
msg.attach(text_html) 
msg.attach(text_att) 
msg.attach(image)

3.文字,html,圖片,附件實(shí)現(xiàn)實(shí)例

#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
from email.header import Header 
#設(shè)置smtplib所需的參數(shù)
#下面的發(fā)件人,收件人是用于郵件傳輸?shù)摹?
smtpserver = 'smtp.163.com'
username = 'XXX@163.com'
password='XXX'
sender='XXX@163.com'
#receiver='XXX@126.com'
#收件人為多個(gè)收件人
receiver=['XXX@126.com','XXX@126.com']
subject = 'Python email test'
#通過(guò)Header對(duì)象編碼的文本,包含utf-8編碼信息和Base64編碼信息。以下中文名測(cè)試ok
#subject = '中文標(biāo)題'
#subject=Header(subject, 'utf-8').encode()
#構(gòu)造郵件對(duì)象MIMEMultipart對(duì)象
#下面的主題,發(fā)件人,收件人,日期是顯示在郵件頁(yè)面上的。
msg = MIMEMultipart('mixed') 
msg['Subject'] = subject
msg['From'] = 'XXX@163.com <XXX@163.com>'
#msg['To'] = 'XXX@126.com'
#收件人為多個(gè)收件人,通過(guò)join將列表轉(zhuǎn)換為以;為間隔的字符串
msg['To'] = ";".join(receiver) 
#msg['Date']='2012-3-16'
#構(gòu)造文字內(nèi)容 
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com" 
text_plain = MIMEText(text,'plain', 'utf-8') 
msg.attach(text_plain) 
#構(gòu)造圖片鏈接
sendimagefile=open(r'D:\pythontest\testimage.png','rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID','<image1>')
image["Content-Disposition"] = 'attachment; filename="testimage.png"'
msg.attach(image)
#構(gòu)造html
#發(fā)送正文中的圖片:由于包含未被許可的信息,網(wǎng)易郵箱定義為垃圾郵件,報(bào)554 DT:SPM :<p><img src="cid:image1"></p>
html = """
<html> 
 <head></head> 
 <body> 
 <p>Hi!<br> 
  How are you?<br> 
  Here is the <a >link</a> you wanted.<br> 
 </p> 
 </body> 
</html> 
""" 
text_html = MIMEText(html,'html', 'utf-8')
text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"' 
msg.attach(text_html) 
#構(gòu)造附件
sendfile=open(r'D:\pythontest\1111.txt','rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8') 
text_att["Content-Type"] = 'application/octet-stream' 
#以下附件可以重命名成aaa.txt 
#text_att["Content-Disposition"] = 'attachment; filename="aaa.txt"'
#另一種實(shí)現(xiàn)方式
text_att.add_header('Content-Disposition', 'attachment', filename='aaa.txt')
#以下中文測(cè)試不ok
#text_att["Content-Disposition"] = u'attachment; filename="中文附件.txt"'.decode('utf-8')
msg.attach(text_att) 
#發(fā)送郵件
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com')
#我們用set_debuglevel(1)就可以打印出和SMTP服務(wù)器交互的所有信息。
#smtp.set_debuglevel(1) 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit()

說(shuō)明:

如果大家對(duì)add_header的入?yún)⒂懈嗟牧私猓–ontent-Disposition,Content-Type,Content-ID),希望告訴我,謝謝。

總結(jié)

以上所述是小編給大家介紹的python自動(dòng)發(fā)郵件總結(jié)及實(shí)例說(shuō)明,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 詳解Python裝飾器由淺入深

    詳解Python裝飾器由淺入深

    裝飾器的功能在很多語(yǔ)言中都有,名字也不盡相同,其實(shí)它體現(xiàn)的是一種設(shè)計(jì)模式,強(qiáng)調(diào)的是開放封閉原則,更多的用于后期功能升級(jí)而不是編寫新的代碼。本文盡量描述得淺顯易懂,從最基礎(chǔ)的內(nèi)容講起。
    2016-12-12
  • python中的正則表達(dá)式,貪婪匹配與非貪婪匹配方式

    python中的正則表達(dá)式,貪婪匹配與非貪婪匹配方式

    這篇文章主要介紹了python中的正則表達(dá)式,貪婪匹配與非貪婪匹配方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python 實(shí)現(xiàn)黑客帝國(guó)中的字符雨的示例代碼

    Python 實(shí)現(xiàn)黑客帝國(guó)中的字符雨的示例代碼

    這篇文章主要介紹了Python 實(shí)現(xiàn)黑客帝國(guó)中的字符雨的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)

    Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)

    這篇文章主要介紹了Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Python實(shí)現(xiàn)對(duì)照片中的人臉進(jìn)行顏值預(yù)測(cè)

    Python實(shí)現(xiàn)對(duì)照片中的人臉進(jìn)行顏值預(yù)測(cè)

    今天給大家?guī)?lái)的是關(guān)于Python實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞如何用Python實(shí)現(xiàn)對(duì)照片中的人臉進(jìn)行顏值預(yù)測(cè)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Python圖像分割之均勻性度量法分析

    Python圖像分割之均勻性度量法分析

    均勻性度量圖像分割是圖像像素分割的一種方法,當(dāng)然還有其他很多的方法。本文將主要介紹下其原理和實(shí)現(xiàn)代碼,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • python常用知識(shí)梳理(必看篇)

    python常用知識(shí)梳理(必看篇)

    下面小編就為大家?guī)?lái)一篇python常用知識(shí)梳理(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • python-try-except:pass的用法及說(shuō)明

    python-try-except:pass的用法及說(shuō)明

    這篇文章主要介紹了python-try-except:pass的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 使用python turtle畫高達(dá)

    使用python turtle畫高達(dá)

    今天小編就為大家分享一篇使用python turtle畫高達(dá),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • django框架CSRF防護(hù)原理與用法分析

    django框架CSRF防護(hù)原理與用法分析

    這篇文章主要介紹了django框架CSRF防護(hù)原理與用法,結(jié)合實(shí)例形式分析了Django框架CSRF防護(hù)的概念、原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-07-07

最新評(píng)論