0

I have a form in index page and i have 12 inputs in forms , so if user submits the form i need all 12 inputs in body of my email . I tried different methods and tried searching on google and youtube but could not find the solution.

My urls.py:

from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [    
    path('', views.index,name='index'),
    path('about/', views.about,name='about'),
    path('contact/', views.contact,name='contact'),
    
]

my views.py:

from django.shortcuts import render
from django.http import HttpResponse
from django.core.mail import send_mail
   
def index(request):
    if request.method == "POST":
        year = request.POST['year']
        make = request.POST['make']
        part = request.POST['part']
        engine = request.POST['engine']
        transmission = request.POST['transmission']
        name = request.POST['name']
        email = request.POST['email']
        phone = request.POST['phone']
        state = request.POST['state']
        zipcode = request.POST['zipcode']
        address = request.POST['address']

        form = "Year: " + year + "Make: " +  make + "Part: " + part + "Engine: " + engine + "transmission: " + transmission + "Name: " + name + "Email: " + email + "Phone: " + phone + "State: " + state + "Zipcode: " + zipcode + "Address: "  + address

        send_mail(
        'Mail from carparts',
        form,
        'arshadkhanak05@gmail.com.',
        ['arshadkhanak05@gmail.com'],
        fail_silently=False,
        )
    return render(request, 'index.html')
    
def about(request):
    return render(request, 'about.html')
    
def contact(request):
    return render(request, 'contact.html')

help me solve this code

Thank you

Pradip Kachhadiya
  • 2,067
  • 10
  • 28

1 Answers1

0

Try with EmailMultiAlternatives :

    from django.core.mail.message import EmailMultiAlternatives
    from django.template.loader import render_to_string

    def index(request):
        if request.method == "POST":
            year = request.POST['year']
            make = request.POST['make']
            part = request.POST['part']
            engine = request.POST['engine']
            transmission = request.POST['transmission']
            name = request.POST['name']
            email = request.POST['email']
            phone = request.POST['phone']
            state = request.POST['state']
            zipcode = request.POST['zipcode']
            address = request.POST['address']
    
            email_template = render_to_string('index_email.html',{"Year": year,"Make": make,"Part": part,"Engine":engine,....})    
            send_email = EmailMultiAlternatives(
                                "Mail from carparts", 
                                None, 
                                settings.EMAIL_HOST_USER, 
                                ['arshadkhanak05@gmail.com'],
                            )
            send_email.attach_alternative(email_template, 'text/html')
            send_email.send()
         return render(request, 'index.html')

Create index_email.html and fetch this all context.

Pradip Kachhadiya
  • 2,067
  • 10
  • 28