Python基于smtplib模塊發(fā)送郵件代碼實例
smtplib模塊負責發(fā)送郵件:是一個發(fā)送郵件的動作,連接郵箱服務器,登錄郵箱,發(fā)送郵件(有發(fā)件人,收信人,郵件內(nèi)容)。
email模塊負責構造郵件:指的是郵箱頁面顯示的一些構造,如發(fā)件人,收件人,主題,正文,附件等。
email模塊下有mime包,mime英文全稱為“Multipurpose Internet Mail Extensions”,即多用途互聯(lián)網(wǎng)郵件擴展,是目前互聯(lián)網(wǎng)電子郵件普遍遵循的郵件技術規(guī)范。
該mime包下常用的有三個模塊:text,image,multpart。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
#郵件服務器信息
smtp_server = "smtp.qq.com"
port = 465 # For starttls
sender_email = "12345689@qq.com"
password="" #get password from mailsetting
#發(fā)送郵件信息,可以發(fā)送給多個收件人
receivers=["12345689@163.com","12345689@qq.com"]
subject="This is import Python SMTP 郵件(文件傳輸) 多媒體測試"
# 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 attach1。\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 attach1。\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'))
# 構造附件1
attach_file1='IMG1965.JPG'
attach1 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8')
attach1["Content-Type"] = 'application/octet-stream'
attach1["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1)
message.attach(attach1)
# 構造附件2
attach_file2='YLJ.jpg'
attach2 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8')
attach2["Content-Type"] = 'application/octet-stream'
attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2)
message.attach(attach2)
# 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: 無法發(fā)送郵件")
print(e)
finally:
server.quit()
結(jié)果
郵件發(fā)送成功!!!
Mail with IMG1965.JPG & IMG1963.jpg has been send to ['12345689@163.com', '12345689@qq.com'] successfully.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python深度學習albumentations數(shù)據(jù)增強庫
下面開始albumenations的正式介紹,在這里我強烈建議英語基礎還好的讀者去官方網(wǎng)站跟著教程一步步學習,而這里的內(nèi)容主要是我自己的一個總結(jié)以及方便英語能力較弱的讀者學習2021-09-09
python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實例
下面小編就為大家分享一篇python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
如何利用python多線程爬取天氣網(wǎng)站圖片并保存
最近做個天 氣方面的APP需要用到一些天氣數(shù)據(jù),所以下面這篇文章主要給大家介紹了關于如何利用python多線程爬取天氣網(wǎng)站圖片并保存的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-11-11
python3反轉(zhuǎn)字符串的3種方法(小結(jié))
這篇文章主要介紹了python3反轉(zhuǎn)字符串的3種方法(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11

