這篇文章主要講解了Python基于smtplib模塊發(fā)送郵件的代碼詳解,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

smtplib模塊負(fù)責(zé)發(fā)送郵件:是一個(gè)發(fā)送郵件的動(dòng)作,連接郵箱服務(wù)器,登錄郵箱,發(fā)送郵件(有發(fā)件人,收信人,郵件內(nèi)容)。
email模塊負(fù)責(zé)構(gòu)造郵件:指的是郵箱頁(yè)面顯示的一些構(gòu)造,如發(fā)件人,收件人,主題,正文,附件等。
email模塊下有mime包,mime英文全稱為“Multipurpose Internet Mail Extensions”,即多用途互聯(lián)網(wǎng)郵件擴(kuò)展,是目前互聯(lián)網(wǎng)電子郵件普遍遵循的郵件技術(shù)規(guī)范。
該mime包下常用的有三個(gè)模塊:text,image,multpart。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
#郵件服務(wù)器信息
smtp_server = "smtp.qq.com"
port = 465 # For starttls
sender_email = "12345689@qq.com"
password="" #get password from mailsetting
#發(fā)送郵件信息,可以發(fā)送給多個(gè)收件人
receivers=["12345689@163.com","12345689@qq.com"]
subject="This is import Python SMTP 郵件(文件傳輸) 多媒體測(cè)試"
# message = MIMEText(text, "plain", "utf-8") #文本郵件
message = MIMEMultipart()
message["Subject"] = Header(subject, "utf-8")
message["from"] = sender_email
message["to"] = ",".join(receivers)
# 郵件正文內(nèi)容
text="""
Dear Sir:
how are you ? \n
for detail information pls refer to attach2。\n
The files you need are as followed.\n
If you have any concern pls let me known.\n
enjoy your weekend.\n
BEST REGARDS \n
"""
# message.attach(MIMEText('for detail information pls refer to attach2。\n The files you need are as followed. \n If you have any concern pls let me known. \n enjoy your weekend', 'plain', 'utf-8')
message.attach(MIMEText(text,'plain','utf-8'))
# 構(gòu)造附件1
attach_file1='IMG1965.JPG'
attach2 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8')
attach2["Content-Type"] = 'application/octet-stream'
attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1)
message.attach(attach2)
# 構(gòu)造附件2
attach_file2='YLJ.jpg'
attach3 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8')
attach3["Content-Type"] = 'application/octet-stream'
attach3["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2)
message.attach(attach3)
# Try to log in to server and send email
# server = smtplib.SMTP_SSL(smtp_server,port)
server = smtplib.SMTP_SSL(smtp_server,port)
try:
server.login(sender_email, password)
server.sendmail(sender_email,receivers,message.as_string())
print("郵件發(fā)送成功!!!")
print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers))
except Exception as e:
# Print any error messages to stdout
print("Error: 無(wú)法發(fā)送郵件")
print(e)
finally:
server.quit()另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享標(biāo)題:Python基于smtplib模塊發(fā)送郵件的代碼詳解-創(chuàng)新互聯(lián)
文章地址:http://chinadenli.net/article6/cohjig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣、關(guān)鍵詞優(yōu)化、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容