Django-DRF-实现邮箱发送注册邮件

邮箱发送的网页

<div style="height: 400px;background-color: whitesmoke;margin: 0 auto;">
     <h2 style="text-align: center;padding-top: 15px;">XX</h2>
     <div style="margin: 0 auto;background-color: white;height: 200px;width: 500px;border: 1px solid rgb(172, 172, 172);">
          <h3 style="border-bottom: 1px solid  rgb(172, 172, 172);height: 40px;line-height: 40px;margin-top: 0;padding-left: 25px;">注册验证</h3>
          <p style="margin-left: 25px;color:steelblue;">你好! yanggeol@qq.com</p>
          <p style="margin-left: 25px;color:steelblue;">欢迎注册XX网,请将注册码填到相应页面</p>
          <p style="margin-left: 25px;color:steelblue;">您的验证码是:965341</p>
          <p style="margin-left: 25px;color:gray;">@ginet.com</p>
      </div>
</div>

代码

setting.py
配置163

# 邮件发送配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 25
EMAIL_USE_SSL = True
#EMAIL_USE_TLS = True #安全协议,与EMAIL_USE_SSL二选其一,只能有一个是True
# 发送邮件的邮箱
EMAIL_HOST_USER = '[email protected]'
# 在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = 'xxxxx'
# 收件人看到的发件人
EMAIL_FROM = 'XX'  # 需要和邮箱号码一致

配置qq邮箱

# 使用qq 邮箱发送邮件
EMAIL_HOST = 'smtp.qq.com'
邮箱
EMAIL_HOST_USER = '[email protected]'
授权码
EMAIL_HOST_PASSWORD = 'xxxx'
端口
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_FROM = '[email protected]'  需要和邮箱号码一致

视图里添加

from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.mail import send_mail
from django.conf import settings

class EmailView(APIView):
    def post(self, request, *args, **kwargs):
       from_who = settings.EMAIL_FROM  # 发件人 已经写在配置中
       to_who = '******@163.com'  # 收件人 
       subject = '发送验证码'  # 发送的主题
       # 发送的消息
       message = '注册信息'  # 发送普通的消息使用的时候message
       meg_html = '网页的字符串 比如开头的网页可以复制进来'  # 发送一个html
       send_mail(subject, message, from_who, [to_who], html_message=meg_html)
    return HttpResponse("ok")

你可能感兴趣的:(Django,html,python,django)