一、準備工作
在使用Python SMTP發送電子郵件之前,需要進行一些準備工作。首先,需要獲取郵件服務器的信息,包括SMTP服務器地址、SMTP端口、郵箱賬號和密碼等信息。若不清楚該如何獲取這些信息,可以通過聯繫郵件服務提供商進行諮詢。
接下來需要安裝Python的email和smtplib庫。
pip install email smtplib
二、構建郵件內容
可以通過Python中email庫提供的類來構建郵件內容。下面是一個簡單的構建郵件內容的示例代碼:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'send@example.com'
receivers = ['receiver@example.com']
message = MIMEText('Python發送郵件示例', 'plain', 'utf-8')
message['From'] = Header('發件人姓名', 'utf-8')
message['To'] = Header('收件人姓名', 'utf-8')
message['Subject'] = Header('Python SMTP郵件示例', 'utf-8')
smtpObj = smtplib.SMTP('smtp.example.com', 25)
smtpObj.login('send@example.com', 'password')
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
執行上述代碼會發送一封簡單的郵件。
三、添加附件
若需要添加附件,可以通過email庫中的MIMEImage、MIMEAudio、MIMEApplication、MIMEText等類來分別創建不同類型的附件對象,並通過MIMEMultipart類將文本和附件組合在一起。下面是一個添加圖片附件的示例代碼:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
sender = 'send@example.com'
receivers = ['receiver@example.com']
message = MIMEMultipart()
message['From'] = Header('發件人姓名', 'utf-8')
message['To'] = Header('收件人姓名', 'utf-8')
message['Subject'] = Header('Python SMTP郵件帶附件示例', 'utf-8')
text = MIMEText('Python發送郵件示例', 'plain', 'utf-8')
message.attach(text)
with open('/path/to/image.jpg', 'rb') as f:
img_data = f.read()
image = MIMEImage(img_data)
image.add_header('Content-Disposition', 'attachment', filename='image.jpg')
message.attach(image)
smtpObj = smtplib.SMTP('smtp.example.com', 25)
smtpObj.login('send@example.com', 'password')
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
執行上述代碼會發送一封帶有圖片附件的郵件。
四、使用SSL加密連接
若需要使用SSL加密連接,可以通過SMTP_SSL類來進行連接。下面是一個使用SSL加密連接的示例代碼:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'send@example.com'
receivers = ['receiver@example.com']
message = MIMEText('Python發送郵件示例', 'plain', 'utf-8')
message['From'] = Header('發件人姓名', 'utf-8')
message['To'] = Header('收件人姓名', 'utf-8')
message['Subject'] = Header('Python SMTP郵件示例', 'utf-8')
smtpObj = smtplib.SMTP_SSL('smtp.example.com', 465)
smtpObj.login('send@example.com', 'password')
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
執行上述代碼會使用SSL加密連接發送一封簡單的郵件。
五、使用TLS加密連接
若需要使用TLS加密連接,可以在連接前調用starttls()方法。下面是一個使用TLS加密連接的示例代碼:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'send@example.com'
receivers = ['receiver@example.com']
message = MIMEText('Python發送郵件示例', 'plain', 'utf-8')
message['From'] = Header('發件人姓名', 'utf-8')
message['To'] = Header('收件人姓名', 'utf-8')
message['Subject'] = Header('Python SMTP郵件示例', 'utf-8')
smtpObj = smtplib.SMTP('smtp.example.com', 25)
smtpObj.starttls()
smtpObj.login('send@example.com', 'password')
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
執行上述代碼會使用TLS加密連接發送一封簡單的郵件。
原創文章,作者:ZKWXU,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/316034.html