出現以上錯誤,是沒有設定引數django_settings_module,
os.environ.setdefault("django_settings_module", "emailclient.settings")
email 配置
# emailclient/settings.py
....
email_use_ssl = true
email_host = 'smtp.163.com' # smtp.163.com smtp.qq.com
email_port = 465
email_host_user = '[email protected]' # 帳號
email_host_password = 'passwd' # 密碼
default_from_email = email_host_user
核心**
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# filename : mail_client.py
# author : [email protected]
import os
from django.conf import settings
from django.core.mail import emailmultialternatives
# subject 主題 content 內容 to_email 是乙個列表,傳送給誰們
def send_emails(subject, content, from_email, to_email):
msg = emailmultialternatives(subject, content, from_email, to_email)
msg.content_subtype = 'html'
# 新增附件(可選)
msg.attach_file('./test.py')
# 傳送
msg.send()
print("send finish")
if __name__ == "__main__":
os.environ.setdefault("django_settings_module", "emailclient.settings")
subject = "test send email from django with 163-email"
content = " hello ***, it doesn't a junk mail. good luck to u."
from_email = settings.default_from_email
to_emails = ["[email protected]", "[email protected]"]
send_emails(subject, content, from_email, to_emails)
如果接收者的客戶端不支援html格式的郵件,則可以改進如下:
def send_emails(subject, content, html_content, from_email, to_email):
msg = emailmultialternatives(subject, content, from_email, to_email)
msg.attach_alternative(html_content, "text/html")
msg.send()
print("send finish")
if __name__ == "__main__":
os.environ.setdefault("django_settings_module", "emailclient.settings")
subject = "test send email from django with 163-email"
content = "hello ***, it doesn't a junk mail.\n\t good luck to u."
html_content = '這是一封重要的郵件.
' from_email = settings.default_from_email
to_emails = ["[email protected]", "[email protected]"]
send_emails(subject, content, from_email, to_emails)
以下內容為網上copy:
各大型郵箱smtp伺服器及埠收集:
外發伺服器:smtp.vip.sina.com
收件伺服器:pop3.vip.sina.com
外發伺服器:smtp.sina.com.cn
收件伺服器:pop3.sina.com.cn
163郵箱smtp伺服器
pop: pop.163.com
smtp: smtp.163.com
qq郵箱smtp伺服器及埠
接收郵件伺服器:imap.exmail.qq.com,使用ssl,埠號993
傳送郵件伺服器:smtp.exmail.qq.com,使用ssl,埠號465或587
yahoo郵箱smtp伺服器
接:pop.mail.yahoo.com.cn
發:smtp.mail.yahoo.com
126郵箱smtp伺服器
pop: pop.126.com
smtp: smtp.126.com
pop3:pop.sina.com
smtp:smtp.sina.com
smtp埠號:25
pop3:pop3.vip.sina.com
smtp:smtp.vip.sina.com
smtp埠號:25
pop3:pop.sina.com
smtp:smtp.sina.com
smtp埠號:25
雅虎郵箱
pop3:pop.mail.yahoo.cn
smtp:smtp.mail.yahoo.cn
smtp埠號:25
pop3:pop3.sohu.com
smtp:smtp.sohu.com
smtp埠號:25
tom郵箱
pop3:pop.tom.com
smtp:smtp.tom.com
smtp埠號:25
gmail郵箱
pop3:pop.gmail.com
smtp:smtp.gmail.com
smtp埠號:587 或 25
qq郵箱
pop3:pop.exmail.qq.com
smtp:smtp.exmail.qq.com
smtp埠號:25
263郵箱
網域名稱:263.net
pop3:263.net
smtp:smtp.263.net
smtp埠號:25
網域名稱:x263.net
pop3:pop.x263.net
smtp:smtp.x263.net
smtp埠號:25
網域名稱:263.net.cn
pop3:263.net.cn
smtp:263.net.cn
smtp埠號:25
網域名稱:炫我型
pop3:pop.263xmail.com
smtp:smtp.263xmail.com
smtp埠號:25
21cn 免費郵箱
pop3:pop.21cn.com
smtp:smtp.21cn.com
imap:imap.21cn.com
smtp埠號:25
21cn 經濟郵郵箱
pop3:pop.21cn.com
smtp:smtp.21cn.com
smtp埠號:25
21cn 商務郵郵箱
pop3:pop.21cn.net
smtp:smtp.21cn.net
smtp埠號:25
21cn 快感郵箱
pop3:vip.21cn.com
smtp:vip.21cn.com
smtp埠號:25
21cn y郵箱
pop3:pop.y.vip.21cn.com
smtp:smtp.y.vip.21cn.com
smtp埠號:25
中華網任我郵郵箱
pop3:rwpop.china.com
smtp:rwsmtp.china.com
smtp埠號:25
中華網時尚、商務郵箱
pop3:pop.china.com
smtp:smtp.china.com
smtp埠號:25
Django傳送郵件
簡介 雖然python提供了smtplib庫,來完成email的傳送功能,但是django對其進行了封裝,使得傳送郵件的介面變得更簡單,更方便,django的封裝位於django.core.mail 例子 from django.core.mail import send mail send mai...
django傳送郵件
django封裝了python自帶的傳送郵件的功能,使其更加簡單易用。1 settings中進行配置 email backend django.core.mail.backends.smtp.emailbackend email use tls true email host smtp.163.co...
Django傳送郵件
django提供了傳送郵件的介面,僅需做簡單的設定即可實現傳送郵件的功能。首先需要在setting做簡單的配置,以163郵箱為例 email backend django.core.mail.backends.smtp.emailbackend email host smtp.163.com ema...