Skip to main content

Python SMTP 寄信

BUBU 記錄怎麼使用 Python 寄信

import smtplib, ssl
from email.mime.text import MIMEText

# smtp port 號
port = 587  # For starttls
# smtp mail 伺服器
smtp_server = ""
# 寄件者帳號
sender_email = ""
# 寄件者密碼
password = ''
# 收件者
receiver_email = ""

# 要傳送的文字
mime=MIMEText("您好。我是用 python 方式寄信", "plain", "utf-8") #撰寫內文內容,以及指定格式為plain,語言為中文
mime["Subject"]="Python GoGoGo 測試" #撰寫郵件標題
mime["From"]=sender_email #撰寫你的暱稱或是信箱
mime["To"]=receiver_email #撰寫你要寄的人
msg=mime.as_string() #將msg將text轉成str

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, msg)



參考相關網站