html傳送郵件 Python傳送郵件(三十)

2021-10-17 09:54:06 字數 2999 閱讀 6510

簡單郵件傳輸協議(smtp)是一種協議,用於在郵件伺服器之間傳送電子郵件和路由電子郵件。

python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。

這是乙個簡單的語法,用來建立乙個smtp物件,稍後將演示如何用它來傳送電子郵件 -

import smtplibsmtpobj = smtplib.smtp( [host [, port [, local_hostname]]] )
這裡是上面語法的引數細節 -

smtp物件有乙個sendmail的例項方法,該方法通常用於執行郵件傳送的工作。它需要三個引數 -

1.使用python傳送純文字電子郵件

示例

以下是使用python指令碼傳送一封電子郵件的簡單方法 -

#!/usr/bin/python3import smtplibsender = '[email protected]'receivers = ['[email protected]']message = """from: from person to: to person subject: smtp e-mail testthis is a test e-mail message."""try: smtpobj = smtplib.smtp('localhost') smtpobj.sendmail(sender, receivers, message)  print "successfully sent email"except smtpexception: print "error: unable to send email"
在這裡,已經傳送了一封基本的電子郵件,使用三重引號,請注意正確格式化標題。一封電子郵件需要乙個from,to和乙個subject標題,與電子郵件的正文與空白行分開。

要傳送郵件,使用smtpobj連線到本地機器上的smtp伺服器。 然後使用sendmail方法以及訊息,從位址和目標位址作為引數(即使來自和位址在電子郵件本身內,這些並不總是用於路由郵件)。

mail = smtplib.smtp('smtp.qq.com', 587) # 埠465或587
2.使用python傳送html電子郵件

當使用python傳送郵件資訊時,所有內容都被視為簡單文字。 即使在簡訊中包含html標籤,它也將顯示為簡單的文字,html標籤將不會根據html語法進行格式化。 但是,python提供了將html訊息作為html訊息傳送的選項。

傳送電子郵件時,可以指定乙個mime版本,內容型別和傳送html電子郵件的字符集。

以下是將html內容作為電子郵件傳送的示例 -

#!/usr/bin/python3import smtplibmessage = """from: from person to: to person mime-version: 1.0content-type: text/htmlsubject: smtp html e-mail testthis is an e-mail message to be sent in html formatthis is html message.
"""try: smtpobj = smtplib.smtp('localhost') smtpobj.sendmail(sender, receivers, message) print "successfully sent email"except smtpexception: print "error: unable to send email"3.傳送附件作為電子郵件

要傳送具有混合內容的電子郵件,需要將content-type標題設定為multipart / mixed。 然後,可以在邊界內指定文字和附件部分。

乙個邊界以兩個連字元開始,後跟乙個唯一的編號,不能出現在電子郵件的訊息部分。 表示電子郵件最終部分的最後乙個邊界也必須以兩個連字元結尾。

所附的檔案應該用包(「m」)功能編碼,以便在傳輸之前具有基本的64編碼。

4.傳送示例

首先我們要知道用python**登入qq郵箱發郵件,是需要更改自己qq郵箱設定的。在這裡大家需要做兩件事情:郵箱開啟smtp功能 、獲得授權碼。之後我們來看看如何更改模板**,實現使用python登入qq郵箱傳送qq郵件。

注意:也可以使用其他服務商的 smtp 訪問(qq、網易、gmail等)。

使用qq郵件傳送郵件之前如何設定授權碼,參考:

4.1.傳送一純文字郵件到指定郵件

#! /usr/bin/env python#coding=utf-8from email.mime.text import mimetextfrom email.header import headerfrom smtplib import smtp_ssl#qq郵箱smtp伺服器host_server = 'smtp.qq.com'#sender_qq為發件人的qq號碼sender_qq = '[email protected]'#pwd為qq郵箱的授權碼pwd = '****kenbb***' ## xh**********bdc#發件人的郵箱sender_qq_mail = '[email protected]'#收件人郵箱receiver = '[email protected]'#郵件的正文內容mail_content = '你好,這是使用python登入qq郵箱發郵件的測試'#郵件標題mail_title = 'maxsu的郵件'#ssl登入smtp = smtp_ssl(host_server)#set_debuglevel()是用來除錯的。引數值為1表示開啟除錯模式,引數值為0關閉除錯模式smtp.set_debuglevel(1)smtp.ehlo(host_server)smtp.login(sender_qq, pwd)msg = mimetext(mail_content, "plain

html傳送郵件 python傳送HTML郵件

1.由於html 是單獨的檔案,如下 mail.html ipport backupstatus backuptime 10.6.160.146 3307 success 2019 12 18 2.python指令碼 coding utf 8 import smtplib from email.he...

python3 6傳送郵件

目的 python3.6傳送郵件 環境 python3.6 pycharm import smtplib from email.mime.text import mimetext from email.header import header 傳送郵箱 sender yuan com 接收郵箱 re...

python 傳送郵件的html模板

傳送郵件模板格式 def delete msg content cname,software info,software content,software definition,software price subject quotedprice 器 產品配置變更通知 head title s 刪除...