一、連接SMTP伺服器
在開始發送電子郵件之前,需要連接SMTP(Simple Mail Transfer Protocol)伺服器。SMTP伺服器是處理郵件發送的伺服器,通過SMTP伺服器來發送電子郵件。
首先需要導入smtplib庫,然後使用smtplib的SMTP()函數來連接SMTP伺服器。
import smtplib # 連接SMTP伺服器 smtpObj = smtplib.SMTP('smtp.example.com', 25)
其中,’smtp.example.com’需要替換成對應的SMTP伺服器地址,25是SMTP伺服器埠號。
二、登錄SMTP伺服器
連接SMTP伺服器之後,需要使用登錄信息登錄SMTP伺服器。
import smtplib # 連接SMTP伺服器 smtpObj = smtplib.SMTP('smtp.example.com', 25) # 登錄SMTP伺服器 smtpObj.login('example@domain.com', 'password')
其中,’example@domain.com’需要替換成發件人的電子郵件地址,’password’替換成發件人的電子郵件密碼。
三、設置郵件內容
登錄SMTP伺服器之後,需要設置電子郵件的內容,包括郵件主題、發件人、收件人、抄送、密送、純文本內容、HTML內容等。
import smtplib from email.mime.text import MIMEText from email.header import Header # 連接SMTP伺服器 smtpObj = smtplib.SMTP('smtp.example.com', 25) # 登錄SMTP伺服器 smtpObj.login('example@domain.com', 'password') # 郵件內容 message = MIMEText('郵件內容', 'plain', 'utf-8') message['Subject'] = Header('郵件主題', 'utf-8') message['From'] = Header('發件人', 'utf-8') message['To'] = Header('收件人', 'utf-8') message['Cc'] = Header('抄送', 'utf-8') message['Bcc'] = Header('密送', 'utf-8') # 發送郵件 smtpObj.sendmail('example@domain.com', ['recipient1@domain.com', 'recipient2@domain.com'])
其中,’郵件內容’需要替換成實際的郵件內容,Header()函數用於設置編碼格式為utf-8,’發件人’、’收件人’、’抄送’、’密送’、’郵箱地址’需要替換成實際的信息。’recipient1@domain.com’、’recipient2@domain.com’需要替換成實際的收件人地址。如果需要抄送或密送,需要在發送郵件函數sendmail()中添加相應的參數。
四、發送電子郵件
設置好郵件內容後,調用sendmail()函數即可發送電子郵件。
import smtplib from email.mime.text import MIMEText from email.header import Header # 連接SMTP伺服器 smtpObj = smtplib.SMTP('smtp.example.com', 25) # 登錄SMTP伺服器 smtpObj.login('example@domain.com', 'password') # 郵件內容 message = MIMEText('郵件內容', 'plain', 'utf-8') message['Subject'] = Header('郵件主題', 'utf-8') message['From'] = Header('發件人', 'utf-8') message['To'] = Header('收件人', 'utf-8') message['Cc'] = Header('抄送', 'utf-8') message['Bcc'] = Header('密送', 'utf-8') # 發送郵件 smtpObj.sendmail('example@domain.com', ['recipient1@domain.com', 'recipient2@domain.com'], message.as_string())
其中,message.as_string()用於將MIMEText類型的郵件內容轉化為字元串類型,使其可以被SMTP伺服器識別。
五、斷開連接
發送電子郵件完成後,需要使用quit()函數斷開SMTP伺服器連接。
import smtplib from email.mime.text import MIMEText from email.header import Header # 連接SMTP伺服器 smtpObj = smtplib.SMTP('smtp.example.com', 25) # 登錄SMTP伺服器 smtpObj.login('example@domain.com', 'password') # 郵件內容 message = MIMEText('郵件內容', 'plain', 'utf-8') message['Subject'] = Header('郵件主題', 'utf-8') message['From'] = Header('發件人', 'utf-8') message['To'] = Header('收件人', 'utf-8') message['Cc'] = Header('抄送', 'utf-8') message['Bcc'] = Header('密送', 'utf-8') # 發送郵件 smtpObj.sendmail('example@domain.com', ['recipient1@domain.com', 'recipient2@domain.com'], message.as_string()) # 斷開連接 smtpObj.quit()
六、完整代碼示例
import smtplib from email.mime.text import MIMEText from email.header import Header # 連接SMTP伺服器 smtpObj = smtplib.SMTP('smtp.example.com', 25) # 登錄SMTP伺服器 smtpObj.login('example@domain.com', 'password') # 郵件內容 message = MIMEText('郵件內容', 'plain', 'utf-8') message['Subject'] = Header('郵件主題', 'utf-8') message['From'] = Header('發件人', 'utf-8') message['To'] = Header('收件人', 'utf-8') message['Cc'] = Header('抄送', 'utf-8') message['Bcc'] = Header('密送', 'utf-8') # 發送郵件 smtpObj.sendmail('example@domain.com', ['recipient1@domain.com', 'recipient2@domain.com'], message.as_string()) # 斷開連接 smtpObj.quit()
以上就是Python SMTP Client發送電子郵件的步驟,包括連接SMTP伺服器、登錄SMTP伺服器、設置郵件內容、發送電子郵件、斷開連接等。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242415.html