1. 程式人生 > >[Python3]SMTP發送郵件

[Python3]SMTP發送郵件

ini connect utf-8 文本格式 python oct qq郵箱 密碼 string

概述

在本文中,主要介紹使用smtplib進行文本格式、HTML格式和帶附件的郵件發送處理。

  • 導入smtplib模塊

import smtplib
  • 關鍵函數說明

# 創建smtp對象
smtp = smtplib.SMTP([host [, port [, localhost]]] )

# 參數說明
# host: smtp服務地址,例如126郵箱的是:smtp.126.com
# port: smtp服務端口
# localhost: 如果你的smtp服務在本機,則只需指定localhost即可


# 發送郵件函數
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])

# 參數說明 # from_addr: 郵件發送地址 # to_addrs: 郵件接收地址列表 # msg: 郵件內容 # mail_options, rcpt_options 可選參數,暫時不需要了解

文本郵件示例

下面演示如何通過126郵箱發送純文本郵件。

# -*- coding:utf-8 -*-

__author__ = 谷白
import smtplib
from email.mime.text import MIMEText
from email.header import Header

if __name__ == "__main__
": print("發送文本郵件示例") # 郵件發送者 sender = "[email protected]" # 郵件接收地址列表 # 請將xxx改為你的qq郵箱名或整個改為你的目標接收郵箱地址 receivers = "[email protected]" # 發送內容構建 # text標識發送內容為文本格式 msg = MIMEText("博客園:谷白", "plain", "utf-8") msg["From"] = "[email protected]
" msg["To"] = receivers # 構建郵件標題 msg["Subject"] = Header("Python3_Test", "utf-8") # smtp服務 smtpserver = "smtp.qq.com" smtpport = xx # 發送人郵件用戶名或專用於smtp賬戶用戶名 username = "xxx" # 發送人郵件密碼或專用於smtp賬戶的密碼 password = "xxxx" # 構建smtp對象 smtp = smtplib.SMTP() # 連接到smtp服務 con = smtp.connect(smtpserver, smtpport) print("連接結果: ", con) # 登錄smtp服務 log = smtp.login(username, password) print("登錄結果:", log) # 發送郵件 print(receivers) res = smtp.sendmail(sender, receivers, msg.as_string()) print("郵件發送結果: ", res) # 退出 smtp.quit() print("send email finish")

運行上述代碼,你的目標郵箱將收到一封測試郵件。

HTML格式郵件

下面我們試著發送html格式的郵件。

將文本格式郵件代碼中的以下部分替換如下:

# 發送內容構建

# html標識發送內容為文本格式
msg = MIMEText("<p>博客園:谷白</p><a href=‘http://www.cnblogs.com/igubai/‘>Python3</a>>",
         "html", 
         "utf-8")

完整的代碼示例如下:

# -*- coding:utf-8 -*-

__author__ = 谷白

import smtplib

from email.mime.text import MIMEText
from email.header import Header

if __name__ == "__main__":    
    print("發送HTML郵件示例")

    # 郵件發送者
    sender = "[email protected]"

    # 郵件接收地址列表
    # 請將xxx改為你的126郵箱名或整個改為你的目標接收郵箱地址
    receivers = "[email protected]"

    # 發送內容構建
    # html標識發送內容為文本格式
    msg = MIMEText("<p>博客園:谷白</p><a href=‘http://www.cnblogs.com/igubai/‘>Python3</a>>", 
        "html", 
        "utf-8")
    msg["From"] = "[email protected]"
    msg["To"] = receivers

    # 構建郵件標題
    msg["Subject"] = Header("Python3_DeepTest", "utf-8")

    # smtp服務
    smtpserver = "smtp.126.com"
    smtpport = 25

    # 發送人郵件用戶名或專用於smtp賬戶用戶名
    username = "xxx"

    # 發送人郵件密碼或專用於smtp賬戶的密碼
    password = "xxx"

    # 構建smtp對象
    smtp = smtplib.SMTP()

    # 連接到smtp服務
    con = smtp.connect(smtpserver, smtpport)    
    print("連接結果: ", con)

    # 登錄smtp服務
    log = smtp.login(username, password)
    print("登錄結果:", log)

    # 發送郵件
    print(receivers)
    res = smtp.sendmail(sender, receivers, msg.as_string())    
    print("郵件發送結果: ", res)

    # 退出
    smtp.quit()    print("send email finish")

註:

  • 將plain改為html標識郵件內容為html格式

  • 郵件內容采用html語言來格式化

附件格式郵件

下面看看如何發送帶附件的郵件。 需要導入新的類,如下:

from email.mime.multipart import MIMEMultipart

需要使用MIMEMultipart構建內容結構,關鍵代碼如下:

# 發送內容構建# html標識發送內容為文本格式
msg = MIMEMultipart()
msg["From"] = "[email protected]"
msg["To"] = receivers

# 構建郵件標題
msg["Subject"] = Header("博客園谷白", "utf-8")

# 構建郵件正文內容
msg.attach(MIMEText("谷白:Python3", "plain", "utf-8"))
# 構造附件,多個附件同理
attach1 = MIMEText(open("發送附件郵件.py", rb).read(), "base64", "utf-8")
attach1["Content-Type"] = "application/octet-stream"

# 這裏filename隨意寫,將會在郵件中顯示
attach1["Content-Disposition"] = "attrachment;filename=code.py"

# 關聯附件到郵件中msg.attach(attach1)

完整的代碼示例如下:

# -*- coding:utf-8 -*-

__author__ = 谷白

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

if __name__ == "__main__":    
    print("發送HTML郵件示例")    
    # 郵件發送者
    sender = "[email protected]"

    # 郵件接收地址列表
    # 請將xxx改為你的126郵箱名或整個改為你的目標接收郵箱地址
    receivers = "[email protected]"

    # 發送內容構建
    # html標識發送內容為文本格式
    msg = MIMEMultipart()
    msg["From"] = "[email protected]"
    msg["To"] = receivers    
    
    # 構建郵件標題
    msg["Subject"] = Header("博客園", "utf-8")    
    
    # 構建郵件正文內容
    msg.attach(MIMEText("谷白:Python3", "plain", "utf-8"))    

    # 構造附件,多個附件同理
    attach1 = MIMEText(open("發送附件郵件.py", rb).read(), "base64", "utf-8")
    attach1["Content-Type"] = "application/octet-stream"

    # 這裏filename隨意寫,將會在郵件中顯示
    attach1["Content-Disposition"] = "attrachment;filename=code.py"

    # 關聯附件到郵件中
    msg.attach(attach1)    
    
    # smtp服務
    smtpserver = "smtp.126.com"
    smtpport = 25

    # 發送人郵件用戶名或專用於smtp賬戶用戶名
    username = "xxx"

    # 發送人郵件密碼或專用於smtp賬戶的密碼
    password = "xxx"

    # 構建smtp對象
    smtp = smtplib.SMTP()    
    
    # 連接到smtp服務
    con = smtp.connect(smtpserver, smtpport)    
    print("連接結果: ", con)    
    
    # 登錄smtp服務
    log = smtp.login(username, password)    
    
    print("登錄結果:", log)    
    
    # 發送郵件
    print(receivers)
    res = smtp.sendmail(sender, receivers, msg.as_string())    
    
    print("郵件發送結果: ", res)    
    
    # 退出
    smtp.quit()    
    
    print("send email finish")

小結

上述各示例最終郵件收到如下所示:

  • 構建文本和html格式的郵件使用MIMEText構建,使用plain標識文本內容格式,使用html標識html內容格式

  • 對於附件格式則需使用MIMEMultipart

[Python3]SMTP發送郵件