8

I put a contact form in my site, and I have this in my settings.py

# Email settings
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail@gmail.com'
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = 587

And this in my views.py

name = form.cleaned_data['name']
email = form.cleaned_data['email']
message = form.cleaned_data['message']
subject = 'Email from ' + name
content = name + '\r\n' + email + '\r\n\r\n' + message

send_mail(subject, content, email, ['me@myemail.com'])

It all works correctly, i get the email with all the information, but, the email comes from myemail@gmail.com even though the from_email parameter has the email var with the senders email.

It doesn't work that way or i'm doing something wrong?

I wanted to get the email from the sender, so y can just reply to it, like i do in PHP.

Thank you.

ramono
  • 462
  • 5
  • 16

1 Answers1

9

Gmail will not let you spoof where the email came from.

per user iAn in a similar post

The short answer - you can't.

Google rewrites the From and Reply-To headers in messages you send via it's SMTP service to values which relate to your gmail account.

The SMTP feature of gmail isn't intended to be an open or relay service. If it allowed any values for the From header, it would significantly dilute Google's standing with spam services, as there would be no way to verify the credentials of the sender.

You need to consider alternatives. How are you planning to host your script/application/website when it's finished: virtually every hosting solutions (shared/vps/dedicated server) will come pre-configured with an email transfer solution: be it sendmail or postfix on *nix, or IIS on Windows.

If you are intent on using gmail then you could:

Setup a dedicated "myapp@gmail.com" account If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a "myapp@mydomain.com" account.

Community
  • 1
  • 1
Andy
  • 44,610
  • 13
  • 70
  • 69
  • So the only way is using a different server to send the email or just leave it like that? – ramono Jul 23 '11 at 20:34
  • Yes, for the reasons stated in iAn's post, Google's servers won't let you arbitrarily set the contents of an email's source. – Andy Jul 23 '11 at 20:45
  • I edited my view like so `send_mail(subject+" - "+from_email, message, from_email, ['user@test.com'])` in order to receive the senders email as part of the subject – Josh Nov 29 '17 at 14:30