1.settings.py 配置
注意:要开启IMAP服务 才能发送邮件 ,在邮箱设置里的账户设置中
# 邮件配置
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com
EMAIL_PORT = 465
EMAIL_HOST_USER = environ.get("EMAIL_SENDER") # 帐号
EMAIL_HOST_PASSWORD = environ.get("EMAIL_PWD") # 授权码(****)
# 默认邮件
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
2.views 视图
from django.core.mail import send_mail, send_mass_mail
def send_my_email(req):
title = "美团骑手offer"
msg = "恭喜你成为美团骑手"
email_from = settings.DEFAULT_FROM_EMAIL
reciever = [
'[email protected]'
]
# 发送邮件
send_mail(title, msg, email_from, reciever)
return HttpResponse("ok")
3.1views 视图 (邮件中发送页面)
def send_email_v1(req):
title = "美团骑手offer"
msg = " "
email_from = settings.DEFAULT_FROM_EMAIL
reciever = [
'[email protected]'
]
# 加载模板
template = loader.get_template('email.html')
# 渲染模板
html_str = template.render({"msg": "123456"})
print(html_str)
# 发送邮件
send_mail(title, msg, email_from, reciever, html_message=html_str)
return HttpResponse("ok")
3.2 前端
好困好无聊
请点击一个快乐的链接 快乐源泉
{{ msg }}
4.1 生成随机字符串
import uuid
import hashlib
def get_random_str():
uuid_val = uuid.uuid4()
uuid_str = str(uuid_val).encode("utf-8")
md5 = hashlib.md5()
md5.update(uuid_str)
return md5.hexdigest()
4.2 拼接激活链接url,加载激活模板并进行渲染
def verify(req):
if req.method == "GET":
return render(req, 'verify.html')
else:
param = req.POST
email = param.get('email')
# 生成随机字符
random_str = get_random_str()
# 拼接验证链接(加网址)
url = "http://118.24.95.20:8000/dj06app/active/" + random_str
# 加载激活模板
tmp = loader.get_template('active.html')
# 渲染
html_str = tmp.render({'url': url})
title = "美团骑手offer"
msg = " "
email_from = settings.DEFAULT_FROM_EMAIL
reciever = [
# '[email protected]'
email,
]
send_mail(title, msg, email_from, reciever, html_message=html_str)
# 记录 token 对应的邮箱是谁 v k
cache.set(random_str, email, 120)
return HttpResponse('ok')
4.3 拿参数对应的缓存数据,来自找邮箱对应的用户,激活用户
def active(req, random_str):
# 拿参数对应的缓存数据
res = cache.get(random_str)
if res:
# 通过邮箱找到对应用户
# 给用户的状态字段做更新,从未激活变成激活状态
return HttpResponse(res+"激活成功")
else:
return HttpResponse("验证链接无效")
4.4 前端,输入发送邮箱页面
发送邮件生成随机url页面
欢迎注册 澳门赌城会员 想要体验 美女荷官
请点击 下面的连接
5. 一次发送多个邮件
def send_many_email(req):
title = "美团骑手offer"
content1 = "该点外卖啦"
email_from = settings.DEFAULT_FROM_EMAIL
reciever1 = [
'[email protected]',
'[email protected]',
'[email protected]',
]
content2 = "吃饭睡觉不想敲代码"
# 邮件1
msg1 = (title, content1, email_from, reciever1)
# 邮件2
msg2 = ("渣渣们", content2, email_from, ['[email protected]', '[email protected]'])
send_mass_mail((msg1,msg2), fail_silently=True)
return HttpResponse("发送好了")
send_mail 每发送一封邮件 就要和SMTP服务 做一次链接
send_mass_mail 可以一次链接 发送多封邮件
(celery在我的其他博文中有介绍)
1. 在tasks.py 文件中写邮件任务
from celery import task
from django.core.mail import send_mail
from django.conf import settings
import time
@task
def send_email(email):
title = '邮件的标题'
msg = '异步操作'
from_email = settings.DEFAULT_FROM_EMAIL
recievers = [email, ]
send_mail(
title,
msg,
from_email,
recievers,
# 发送异常时不提示
fail_silently=True
)
2. 在views 视图中调用邮件函数
from django.http import HttpResponse
from django.shortcuts import render
from .tasks import first_task, send_email
# Create your views here.
def first_celery(req):
# 任务参数名.delay(参数)
first_task.delay(4)
send_email.delay("[email protected]")
return HttpResponse("ok")
3. 实现异步发送(增强用户体验感)