django send defferent html message to defferent email address

64 views
Skip to first unread message

SKYLINE TV

unread,
Jul 3, 2021, 8:35:21 AM7/3/21
to Django users
i am currently building a contact us form.
when my users filed the form  and submit  it will send them welcome message and send also forward the user data to my email.

 i was able to used mailMultiAlternatives  to achieve  sending  the html template to my users with the code below.

EmailMultiAlternatives email class doesn't  allow sending multiple message to different email address. 


class Conactme(CreateView):
    form_class = Contactusform
    context_object_name = "forms"
    template_name = "registration/contactus.html"
    success_url = reverse_lazy('home')

    def form_valid(self, form):
        if form.is_valid():


            email = form.cleaned_data['email']
            full_name = form.cleaned_data['full_name']
            purpose = form.cleaned_data['purpose']
            phone = form.cleaned_data['phone']
            comment = form.cleaned_data['comment']
            form.save()
        content = {"fullname": full_name,
                   "phone":  phone,
                   "purpose": purpose
                   }
        userssubject = "Users Registrations"
        adminsubject = "Welcome message"
        html_message = render_to_string(
            template_name="emails/email_comfirmation.html", context=content)
        meg = EmailMultiAlternatives(
            adminsubject, full_name, EMAIL_HOST_USER, [email])
        meg.attach_alternative(html_message, 'text/html')
        meg.send()
        if meg.send:
         return render(self.request, "registration/sucesss.html")
fff.PNG

Avi shah

unread,
Jul 3, 2021, 9:10:21 AM7/3/21
to django...@googlegroups.com
from django.conf import settings
from django.core.mail import send_mail

def valorant_view(request):
    if (valorant_registration.objects.count() <= 160):
        if request.method == 'POST':
            first_name = request.POST['first_name']
            last_name = request.POST['last_name']
            email = request.POST['email']
            mobile_number = request.POST['mobile_number']
            branch = request.POST['mobile_number']
            dicord_id = request.POST['dicord_id']
            valorant_id = request.POST['valorant_id']
            college_name = request.POST['college_name']
            student_id = request.FILES['student_id']

            valorant = valorant_registration(first_name=first_name, last_name=last_name, email=email,
                                             mobile_number=mobile_number, branch=branch, dicord_id=dicord_id, valorant_id=valorant_id, college_name=college_name, student_id=student_id)

            valorant.save()
            print('Event Registration Done Succesfully')
            subject = 'Techela 6.0 Registration'
            from_email = settings.DEFAULT_FROM_EMAIL
            template = render_to_string(
                'thank-you.html', {'name': first_name + ' ' + last_name, 'event''Valorant'})
            message = 'Test'
            html_message = template

            send_mail(subject, message, from_email, [email],
                      fail_silently=Falsehtml_message=html_message)
            return HttpResponse('Done')

        else:
            print('Not there events')
            return redirect('warning.html2')

    else:
        return HttpResponse('Registration closed')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cfabe10e-0dae-4ec2-9936-b6061a4ae146n%40googlegroups.com.

SKYLINE TV

unread,
Jul 3, 2021, 10:41:26 AM7/3/21
to Django users
thanks very much but base on your code you are sending all the  message to user. i want to send a specific message to   admin emails like 
form input data than and also send to user  such as welcome message 

Avi shah

unread,
Jul 3, 2021, 11:41:27 AM7/3/21
to django...@googlegroups.com
You can use context or string concatenation to pass a specific type of user a specific message 
you can do it by and if statement where 

if request.user == admin :
then the below code for it 

subject = 'Techela 6.0 Registration'
            from_email = settings.DEFAULT_FROM_EMAIL
            template = render_to_string(
                'thank-you.html', {'name': first_name + ' ' + last_name, 'event''Valorant'})
            message = 'Test'
            html_message = template 
Reply all
Reply to author
Forward
0 new messages