共計 844 個字符,預計需要花費 3 分鐘才能閱讀完成。
要編寫一個 Python 自動發郵件的腳本,你可以使用 smtplib 模塊。下面是一個簡單的示例腳本:
import smtplib
from email.mime.text import MIMEText
def send_email(sender, password, recipient, subject, message):
# 創建一個 MIMEText 對象
msg = MIMEText(message)
# 設置發件人和收件人
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = subject
# 使用 SMTP 服務器發送郵件
server = smtplib.SMTP('smtp.gmail.com', 587) # 這里使用的是 Gmail 的 SMTP 服務器,如果是其他郵箱,請修改對應的 SMTP 服務器地址
server.starttls()
server.login(sender, password)
server.send_message(msg)
server.quit()
# 設置發件人、密碼、收件人、主題和消息內容
sender = 'your_email@gmail.com'
password = 'your_password'
recipient = 'recipient_email@example.com'
subject = 'Hello, World!'
message = 'This is a test email.'
# 調用 send_email 函數發送郵件
send_email(sender, password, recipient, subject, message)
在使用這個腳本之前,請確保你已經安裝了 smtplib 模塊。并將代碼中的發件人郵箱、密碼、收件人郵箱以及 SMTP 服務器地址進行替換。
丸趣 TV 網 – 提供最優質的資源集合!
正文完