James Bullock
James Bullock

Reputation: 11

Email sent to wrong account

Ive built an contact form which sends an email. I'm just having a bit of trouble in relation to the account its being sent to. I want the email to be sent from "[email protected]" to the "Contact_Email". Right now the email is going from "Contact_Email" to "[email protected]".

my views.py looks like this:

def contact(request):
    Contact_Form = ContactForm
    if request.method == 'POST':
        form = Contact_Form(data=request.POST)

        if form.is_valid():
            contact_name = request.POST.get('contact_name')
            contact_email = request.POST.get('contact_email')
            contact_content = request.POST.get('content')

            template = get_template('users/contact_form.txt')
            context = {
                'contact_name' : contact_name,
                'contact_email' : contact_email,
                'contact_content' : contact_content,
            }

            content = template.render(context)

            email = EmailMessage(
                "New contact form email",
                content,
                "Creative web" + '',
                ['[email protected]'],
                headers = { 'Reply To': contact_email }
            )

            email.send()


    return render(request, 'users/contact.html', {'form':Contact_Form })

And my setting.py looks like:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = '*******'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Upvotes: 1

Views: 52

Answers (1)

Zev
Zev

Reputation: 3491

If you look at the order of your arguments and convert them from positional to keyword, you currently have:

        email = EmailMessage(
            subject="New contact form email",
            body=content,
            from_email="Creative web" + '',
            to=['[email protected]'],
            headers = { 'Reply To': contact_email }
        )

I think there were a couple of issues here. I think you probably meant to do:

from_email='"Creative web" <[email protected]>'

But since you didn't get that, it messed up the order of your positional arguments.

To should be to=contact_email

The other issue is I think you are misunderstanding the 'Reply To' header. That's who, when the recipient hits the reply button, the email will be sent back to. It is not who you are sending the email to.

Upvotes: 2

Related Questions