0

I see people using the send_mail function like this:

send_mail(subject, message, from_email, ['admin@example.com'])

My form have more fields such as 'name' and 'phone_number'. How can I pass those extra fields to the send_mail function?

forms.py

class ContactForm(forms.Form):
    name = forms.CharField(required=True)
    phone_number = forms.CharField(required=True)
    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=True)
    message = forms.CharField(widget=forms.Textarea, required=True)

views.py

def contactView(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['admin@example.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
    return render(request, "email.html", {'form': form})
GTA.sprx
  • 817
  • 1
  • 8
  • 24

1 Answers1

0

no, the fields in the sendmail correspond to the data in the email header. If you want to add more contact data to the email, you should render it in the body (for examole as a disclaimer). You can also check this thread: add sender's name in the from field of the email in python

tstoev
  • 1,415
  • 11
  • 12