python中同步傳送郵件的步驟
阿新 • • 發佈:2018-12-18
1. 設定smtp伺服器, 開啟客戶端授權碼, 不同的郵箱設定略有不同, 下面是以126郵箱為例
2. 專案配置檔案中配置資訊
# 傳送郵件配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # smpt服務地址 EMAIL_HOST = 'smtp.126.com' EMAIL_PORT = 25 # 傳送郵件的郵箱 EMAIL_HOST_USER = '郵箱名@126.com' # 在郵箱中設定的客戶端授權密碼 EMAIL_HOST_PASSWORD = '授權碼' # 收件人看到的發件人, 與EMAIL_HOST_USER一致 EMAIL_FROM = '<郵箱名@126.com>'
3. 傳送郵件
# 匯入專案 配置檔案 from django.conf import settings # 匯入傳送郵件模組 from django.core.mail import send_mail # 傳送郵件 subject = "郵件主題" # 主題 # message = "郵件正文" # 不能加格式的內容 message = "" html_message = "<h1>這是有格式的內容</h1>" sender = settings.EMAIL_FROM # 發件人 receivers = [收件人1, 收件人2, 收件人3] # 收件人列表 send_mail(subject, message, sender, receivers, html_message=html_message)