Django内置email发送邮件
1、首先在settings.py文件设置相关参数
STATIC_URL = '/static/'
2、回到views.py 文件,加入以下代码
3、多封给多个人进行发送
message1 = ('这个周日你有空吗',
'孔子东游见两小儿便日',
'qiqi1931@163.com',
['891811395@qq.com', '819614513@qq.com'])
message2 = ('这个周 日你有空吗?',
'孔子东游,见两小儿,便日',
'qiqi1931@163.com',
['891811395@qq.com', '1311173658@qq.com'])
res = send_mass_email((message1, message2))
if res == 2:
return HttpResponse('多封邮件发送成功')
else:
return HttpResponse('多封邮件发送失败')
4、进阶使用,多线程异步发送
import threading
from django.core.mail import send_mail
from django.conf import settings
class SendMail(threading.Thread):
def __init__(self,subject,text,email,fail_silently=False):
self.subject = subject
self.text = text
self.email = email
self.fail_silently = fail_silently
threading.Thread.__init__(self)
def run(self):
send_mail(
self.subject,
self.text,
settings.EMAIL_HOST_USER,
[self.email],
fail_silently=self.fail_silently
)
send_mail = SendMail(subject,text,email)
send_mail.start()