Adding Submitted Email to Reply Notification


#1

@limedaring In the section of your tutorial at https://hellowebapp.com/news/tutorial-setting-up-a-contact-form-with-django, you have the header reply-to going to contact_email, which works great if you are using the internal Django smtp service, but I wired in SendGrid with the django-sendgrid api, which works brilliantly, but I can’t seem to get the headers for the reply-to to work. Have you ever wired in an app with SendGrid? If so, can you please point me in the right direction; I am still in Django training wheels :) If I insert the headers like in your tutorial, I get an ‘unexpected argument’ error. Thank you for your help.

Here is my code (shortened for brevity)

....
send_mail("Form Submission from Contact Page", content,
                  "Star RV & Trailer Rentals <[email protected]>", ["[email protected]"]
                  )

        return redirect('contact')
....

#2

To add custom headers and whatnot, I think you’ll need to use the EmailMessage class directly instead of the send_mail helper which only exposes a little bit of functionality.

Relevant excerpt from the tutorial you linked:

from django.core.mail import EmailMessage

# ...

def contact(request):
    # ...
            email = EmailMessage(
                "New contact form submission",
                content,
                "Your website" +'<[email protected]>',
                ['[email protected]'],
                headers = {'Reply-To': contact_email }
            )
            email.send()

Note how it’s using email = EmailMessage(...) and then calling email.send() on it.

Hope this helps! :)


#3

Brilliant!! Thank you so much; I can’t believe the answer was right in front of me the whole time :) I was probably looking at it for too long. Enjoy your holidays!


#4

Yay, you too! :)