通過Python可以很方便地使用SMTP連接到郵件伺服器,發送郵件到指定的郵箱地址。在這裡,我們將詳細闡述如何使用Python SMTP發送電子郵件。
一、SMTP介紹
SMTP(簡單郵件傳輸協議)是一種用於發送電子郵件的標準協議。它定義了電子郵件是如何傳輸到其他伺服器和如何通過其他伺服器進行傳輸的規則。
SMTP伺服器通常是免費的,您可以在網上找到許多可供選擇的SMTP伺服器。當您需要使用SMTP伺服器發送電子郵件時,您需要提供以下信息:
- SMTP伺服器地址
- SMTP伺服器的埠號
- 發件人郵箱地址
- 發件人郵箱的密碼
- 收件人郵箱地址
- 郵件主題
- 郵件正文
二、Python SMTP模塊
在Python SMTP模塊中,我們可以輕鬆地與SMTP伺服器進行交互。Python通過SMTP和smtplib模塊來提供SMTP客戶端服務。該smtplib模塊定義了SMTP類,該類有用於連接到SMTP伺服器並在一個或多個步驟中進行電子郵件發送的方法。
下面是一個簡單的Python程序,用於連接到SMTP伺服器並發送電子郵件:
import smtplib smtp_server = "smtp.gmail.com" smtp_port = 587 username = "your_email_address" password = "your_email_password" recipient = "recipient_email_address" subject = "Sample Subject" message = "This is a sample message." server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(username, password) server.sendmail(username, recipient, "Subject: {}\n\n{}".format(subject, message)) server.quit()
三、發送HTML郵件
SMTP郵件可以包含格式化文本、圖片以及鏈接。對於發送HTML郵件而言,SMTP模塊也能勝任。下面是一個程序示例,用於連接到SMTP伺服器並以HTML格式發送電子郵件:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText smtp_server = "smtp.gmail.com" smtp_port = 587 username = "your_email_address" password = "your_email_password" recipient = "recipient_email_address" subject = "HTML Sample Subject" message_body = "HTML Sample Message
This is a sample HTML email message.
Here is a link to Google
" message = MIMEMultipart('alternative') message['From'] = username message['To'] = recipient message['Subject'] = subject message.attach(MIMEText(message_body, 'html')) server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(username, password) server.sendmail(username, recipient, message.as_string()) server.quit()
四、發送帶附件的電子郵件
SMTP郵件還可以包含附件。要發送帶附件的電子郵件,需要將附件添加到電子郵件消息中,然後將該消息發送到SMTP伺服器。下面是一個程序示例,用於連接到SMTP伺服器並發送帶附件的電子郵件:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email import encoders smtp_server = "smtp.gmail.com" smtp_port = 587 username = "your_email_address" password = "your_email_password" recipient = "recipient_email_address" subject = "Attachment Sample Subject" message_body = "This is a sample message with an attachment." message = MIMEMultipart() message['From'] = username message['To'] = recipient message['Subject'] = subject message.attach(MIMEText(message_body)) file_path = "path/to/attachment/file" attachment = open(file_path, "rb") part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= %s" % file_path.split("/")[-1]) message.attach(part) server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(username, password) server.sendmail(username, recipient, message.as_string()) server.quit()
五、結束語
Python SMTP模塊提供了一個簡單而強大的方法來給指定的郵箱地址發送電子郵件。不論你是需要發送包含文本、圖片、超鏈接、附件的電子郵件,或是需要以HTML格式發送電子郵件,Python SMTP模塊都能夠勝任。希望這篇Python SMTP的完整教程能夠對你有所幫助。
原創文章,作者:SVEO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/149381.html