邮箱注册,登录及激活

setting.py


(1)新浪邮箱

# 发送邮件的相关配置
# 邮箱账号密码
EMAIL_HOST_USER  = '[email protected]'
EMAIL_HOST_PASSWORD = 'wj56343'
# 邮箱服务器地址
EMAIL_HOST = 'smtp.sina.cn'
# 邮箱端口号
# 3306:mysql  6379:redis  5000:flask 8000:django 6800:scrapyd 27017:mongodb 80:http  443:https
EMAIL_PORT = 25
# 是否使用TLS安全传输协议
EMAIL_USE_TLS = True

(2)网易邮箱

 # 发送邮件的相关配置
# 邮箱账号密码
EMAIL_HOST_USER  = '[email protected]'
EMAIL_HOST_PASSWORD = '1369391111b1'  #网易授权的密码。(客户端授权码)
# 邮箱服务器地址
EMAIL_HOST = 'smtp.163.com'
# 邮箱端口号
# 3306:mysql  6379:redis  5000:flask 8000:django 6800:scrapyd 27017:mongodb 80:http  443:https
EMAIL_PORT = 25
# 是否使用TLS安全传输协议

EMAIL_USE_TLS = True



views.py文件

from django.shortcuts import render,redirect,HttpResponse
from django.core.mail import send_mail,send_mass_mail

# Create your views here.
from Send_Eamil import settings

def index(request):

    return render(request,'index.html')

def email(request):
    if request.method == 'POST':
        # 取出邮箱地址
        to_email = request.POST.get('email',None)

        if to_email:
            # 1.邮件标题 2.邮件内容 3.发送邮件的账户 4.接收邮件的账号(可以是多个用户)
            # html_message 添加一些HTML代码
            res = send_mail('这是一封测试邮件','测试邮件内容',settings.EMAIL_HOST_USER,[to_email,'[email protected]'],html_message='测试链接')
            if res != 1:
                return HttpResponse('邮件发送失败!')

        return redirect('/email/')
    else:
        redirect('/email/')

# 发送多封邮件给多人
def email_more(request):
    
    if request.method == 'POST':
        
        # 取出要发送至的邮箱
        email1 = request.POST.get('email1',None)
        email2 = request.POST.get('email2',None)
        email3 = request.POST.get('email3',None)
        
        if email1 and email2 and email3:
            # 构造邮件元组
            message1 = ('取快递啦','你的快递被放在六楼西北三前台,请前往领取,验证码:6379',settings.EMAIL_HOST_USER,[email1,email3])
            message2 = ('外卖到啦','你的外卖已经送达,回复奖励5元大红包',settings.EMAIL_HOST_USER,[email2])
            message3 = ('该吃饭啦','楼下已经准备好饭菜,了解一下?',settings.EMAIL_HOST_USER,[email3])
            try:
                # 发送
                res = send_mass_mail((message1,message2,message3))
            except Exception as e:
                print(e)
                return HttpResponse('邮件发送失败!')

            print(res)
            
    return redirect('/email/')



============================================================

邮箱注册,登录及激活_第1张图片邮箱注册,登录及激活_第2张图片邮箱注册,登录及激活_第3张图片邮箱注册,登录及激活_第4张图片


=====================================


总结: 有时候,发送的邮件会被当做垃圾邮件处理,所以可以在垃圾邮件中找到.



你可能感兴趣的:(Django)