一、Python發送郵件的基本知識
Python發送郵件的基本知識包括SMTP、MIME和SSL/TLS協議三個方面。
SMTP(Simple Mail Transfer Protocol)是Internet郵件傳輸的協議,Python中使用smtplib模塊提供基於SMTP協議的郵件發送功能。
MIME(Multipurpose Internet Mail Extensions)是一種郵件傳輸協議,可用於發送複雜或富文本郵件。Python中使用email及其子模塊提供MIME郵件相關的功能。
SSL(Secure Socket Layer)與TLS(Transport Layer Security)是加密通信協議,可用於保護電子郵件通信中的數據,Python中使用ssl模塊提供SSL/TLS相關功能。
import smtplib from email.mime.text import MIMEText from email.header import Header from email.utils import formataddr sender = 'sender@example.com' # 發件人郵箱 password = 'password' # 發件人郵箱密碼或授權碼 receivers = ['receiver1@example.com', 'receiver2@example.com'] # 接收人郵箱,可支持多個接收人 message = MIMEText('Python郵件發送測試', 'plain', 'utf-8') message['From'] = formataddr((Header('發件人', 'utf-8').encode(), sender)) message['To'] = ','.join(receivers) message['Subject'] = Header('Python SMTP 郵件測試', 'utf-8').encode() try: smtpObj = smtplib.SMTP('smtp.example.com', 25) # SMTP伺服器地址和埠 smtpObj.login(sender, password) # 登錄SMTP伺服器 smtpObj.sendmail(sender, receivers, message.as_string()) # 發送郵件 smtpObj.quit() # 退出SMTP伺服器 print("郵件發送成功!") except smtplib.SMTPException as e: print("郵件發送失敗,錯誤信息:", e)
二、SMTP協議的使用
使用Python發送郵件的第一步是建立SMTP連接。Python提供了smtplib模塊以支持SMTP協議。建立SMTP連接的方法是使用SMTP類的connect()或starttls()方法,這兩種方法的選擇取決於你是否需要使用加密連接。
在建立SMTP連接後,你可以使用SMTP類的login()方法提供SMTP伺服器的用戶名和密碼進行身份驗證。這是必須的,因為大多數SMTP伺服器需要驗證以防止惡意或未經授權的訪問。
一旦你成功連接到SMTP伺服器並進行身份驗證,就可以使用SMTP類的sendmail()方法來發送郵件。在 sendmail() 方法中,你需要提供發件人地址,收件人地址以及要發送的消息。如果你要發送HTML或帶附件的消息,你需要使用email庫和MIME協議。
三、MIME協議的使用
郵件中的MIME(Multipurpose Internet Mail Extensions)協議是一種郵件內容格式。它允許您在郵件中發送不同類型的內容,例如文本、HTML、圖像或附件。
Python中使用email庫來創建MIME消息。將要發送的郵件消息作為一個字元串傳遞給MIMEText類。然後設置消息的發送者、接收者和主題,將該消息附加到MIME郵件中並發送它。
from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders msg = MIMEMultipart() msg['From'] = formataddr((Header('XX網站管理員', 'utf-8').encode(), 'webmaster@example.com')) msg['To'] = formataddr((Header('用戶', 'utf-8').encode(), 'user@example.com')) msg['Subject'] = Header('XX網站密碼重置', 'utf-8').encode() # 郵件正文 message = MIMEText('請點擊下面的鏈接重置您的密碼:\n\nhttp://www.example.com/resetpassword?key=abcdefg\n\n', 'plain', 'utf-8') msg.attach(message) # 添加附件 filename = 'guide.pdf' attachment = open(filename, 'rb') part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % filename) msg.attach(part) try: smtpObj = smtplib.SMTP('smtp.example.com', 25) smtpObj.login(sender, password) smtpObj.sendmail(sender, receivers, msg.as_string()) smtpObj.quit() print("郵件發送成功!") except smtplib.SMTPException as e: print("郵件發送失敗,錯誤信息:", e)
四、使用SSL/TLS進行加密通信
SSL(Secure Socket Layer)與TLS(Transport Layer Security)是加密通信協議,可用於保護電子郵件通信中的數據。Python中使用ssl模塊提供SSL/TLS相關功能。
如果你要使用SSL加密SMTP通信,請使用SMTP_SSL類代替SMTP類。如果你要使用STARTTLS加密SMTP通信,請使用SMTP類的starttls()方法來啟動加密連接。
import ssl context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.example.com", 465, context=context) as server: server.login(sender, password) server.sendmail(sender, receivers, message.as_string()) print('郵件發送成功!')
五、使用Python發送HTML郵件
使用Python發送HTML格式的郵件也很簡單。只需使用MIMEText類,並指定「html」作為消息的子類型即可。
from email.mime.text import MIMEText html = 'Hello, World!
這是一封HTML郵件。
' message = MIMEText(html, 'html', 'utf-8')
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/205927.html