简体   繁体   English

在 Django 中激活帐户 HTML email

[英]Activate account HTML email in Django

I have a Django App that lets users to register to the system.我有一个 Django 应用程序,可让用户注册到系统。 After the user register via a form receive a email with an activation link.用户通过表单注册后,收到带有激活链接的 email。 The app works very well, but I want to send the user an HTML mail, not only plain text.该应用程序运行良好,但我想向用户发送 HTML 邮件,而不仅仅是纯文本。 I use an template to provide fields to be filled.我使用模板来提供要填写的字段。 The problem I face is how to provide the uuid and token in an HTML template in an activate link.我面临的问题是如何在激活链接的 HTML 模板中提供 uuid 和令牌。

I've reading about the send_mail() function, but I don't understand how to use for my purpose.我已阅读有关 send_mail() function 的信息,但我不明白如何用于我的目的。

I'm using Django 3.2.8.我正在使用 Django 3.2.8。

Show my code:显示我的代码:

myApp/urls.py我的应用程序/urls.py

path('registro/<str:usuario>', views.registro, name='registro'),
path('activate/<uidb64>/<token>/', views.activate, name='activate'),    
path('esperaconfirmacionregistro/', views.esperaConfirmacionRegistro, name='esperaconfirmacionregistro'),
path('confirmacionregistro/<str:status>', views.confirmacionRegistro, name='confirmacionregistro'),

acc_active_email.html acc_active_email.html

{% autoescape off %}  

Saludos, {{user.first_name}} {{user.last_name}}.

Por favor da click en el enlace para confirmar tu registro, 
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}  
{% endautoescape %}

views.py视图.py

def registro(request,usuario):

    if request.method == 'POST':
        
        form = FormularioAutoRegistro(request.POST, initial={'usuario':usuario})
        if form.is_valid():

            user = form.save()  

            current_site_info = get_current_site(request)  
            mail_subject = 'Activar cuenta de usuario'  
            message = render_to_string('myApp/acc_active_email.html', {  
                'user': user,  
                'domain': current_site_info.domain,  
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),  
            })
            to_email = form.cleaned_data.get('email')  

            email = EmailMessage(mail_subject, message, to=[to_email])  
            email.send()  

            form = FormularioAutoRegistro(initial={'usuario':''})   
            return redirect('esperaconfirmacionregistro') 
            
    else:
        form = FormularioAutoRegistro(initial={'usuario':usuario})
        return render(request, 'myApp/registro.html', {'form': form})

def activate(request, uidb64, token):  
    user = get_user_model()  
    try:  
        uid = force_text(urlsafe_base64_decode(uidb64))  
        user = customuser.objects.get(pk=uid)  
    except(TypeError, ValueError, OverflowError, user.DoesNotExist):  
        user = None  
    
    if user is not None and account_activation_token.check_token(user, token):  
        user.is_active = True  
        user.save()  
        return redirect('confirmacionregistro', status='confirm') 
    else:  
        return redirect('confirmacionregistro', status='fail')  

def confirmacionRegistro(request, status):

    titulo = ''
    mensaje = ''

    if status=='confirm':
        titulo = 'Cuenta confirmada'
        mensaje = 'Gracias por confirmar tu email.'
    elif status=='fail':
        titulo = 'Link inválido'
        mensaje = 'El link de activación es inválido.'
    else:
        raise Http404

    return render (request,'myApp/confirmacionregistro.html', {'titulo':titulo, 'mensaje':mensaje})


def esperaConfirmacionRegistro(request):
    mensaje = "Autorización pendiente"
    return render(request, 'myApp/esperaconfirmacionregistro.html', {'mensaje': mensaje})

Try using EmailMultiAlternatives instead of EmailMessage .尝试使用EmailMultiAlternatives而不是EmailMessage It is pretty similar:它非常相似:

from django.core.mail import EmailMultiAlternatives
mail = EmailMultiAlternatives("title", "content", "email_from@test.pl", ["email_to@test.pl"])

Then you can attach html "alternative":然后你可以附加html “替代”:

from django.urls import reverse
url = reverse('activate', kwargs={'uidb64': your_uidb64, 'token': your_token})
html_content = f"Link: <a href='{url}'>{url}</a>"
mail.attach_alternative(html_content, "text/html")

And send it:并发送:

mail.send()

Finally I resove my problem.最后我解决了我的问题。 NixonSparrow you are right, I had the answer: NixonSparrow 你是对的,我有答案:

def registro(request,usuario):

    if request.method == 'POST':
        
        form = FormularioAutoRegistro(request.POST, initial={'usuario':usuario})
        if form.is_valid():

            user = form.save()  

            current_site_info = get_current_site(request) 
            mail_subject = 'Activar cuenta de usuario'  
            to_email = form.cleaned_data.get('email')     
            nombre = user.first_name
            apellidos = user.last_name
            domain = current_site_info.domain
            uid = urlsafe_base64_encode(force_bytes(user.pk))
            token = account_activation_token.make_token(user)  
            url = f"http://{domain}/activate/{uid}/{token}"

            content = f"Hola {nombre} {apellidos} \n" \
                      f"Por favor da click en el enlace o copialo y pégalo en el navegador para confirmar tu registro en el sistema: {url} \n" \
                      f"Gracias."
            email = EmailMultiAlternatives(mail_subject, content, "pepe.eloy@gmail.com", [to_email])

            html_content = f"Hola <strong>{nombre} {apellidos}</strong>. <br> <br>" \
                           f"Por favor da click en el enlace para confirmar tu registro en el sistema: <a href='{url}'>{url}</a> <br> <br>" \
                           f"Gracias."
            email.attach_alternative(html_content, "text/html")

            email.send()  

            form = FormularioAutoRegistro(initial={'usuario':''})  
            return redirect('esperaconfirmacionregistro') 
            
    else:
        
        form = FormularioAutoRegistro(initial={'usuario':usuario})

        return render(request, 'myApp/registro.html', {'form': form})
 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM