溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python基于smtplib模塊發(fā)送郵件的代碼詳解

發(fā)布時間:2020-07-20 16:26:42 來源:億速云 閱讀:136 作者:小豬 欄目:開發(fā)技術(shù)

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

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

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

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

該mime包下常用的有三個模塊: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ā)送給多個收件人
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 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: 無法發(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基于smtplib模塊發(fā)送郵件的代碼詳解有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI