Django 应用 -- 发送邮件
出现以上错误,是没有设置参数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 = 'youremail@163.com' # 帐号
EMAIL_HOST_PASSWORD = 'passwd' # 密码
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
核心代码
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# fileName : mail_client.py
# author : zoujiameng@aliyun.com.cn
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 XXX, it doesn't a junk mail. Good Luck To U."
from_email = settings.DEFAULT_FROM_EMAIL
to_emails = ["username1@163.com", "username2@163.com"]
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 XXX, it doesn't a junk mail.\n\t Good Luck To U."
html_content = '<p>这是一封<strong>重要的</strong>邮件.</p>'
from_email = settings.DEFAULT_FROM_EMAIL
to_emails = ["username1@163.com", "username2@163.com"]
send_emails(subject, content, from_email, to_emails)
以下内容为网上copy:
各大型邮箱smtp服务器及端口收集:
新浪邮箱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
新浪VIP邮箱
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
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shentong1/article/details/78864325