changing from address on send_mail - django

I've hooked in Gsuite as my SMTP server. It sends out emails and it works very well. However, when using the send_mail function in django, I want to indicate a from address other than the email address that I have hooked into Gsuite, which is admin#mywebsite.com.
subject = 'Order Confirmation'
from_email = "jim#gmail.com"
to_email = ("james#gmail.com",)
txt_message = "some text"
html_message = "some other text"
send_mail(subject,
txt_message,
from_email,
to_email,
html_message=html_message,
fail_silently=False,
auth_user="admin#mywebsite.com",
auth_password="PASSWORD"
)
When an email is sent out with this code, when I look at the resulting email email in the email client of james#gmail.com, the "from" address is admin#mywebsite.com rather than jim#gmail.com.
Is there a way that I can change this so that the 'from' is jim#gmail.com or at least the 'reply-to' is jim#gmail.com?
Thanks for your help!

I think this answers it. Gmail won't allow me to forge a from address, therefore the from address will always be admin#mywebsite.com
to send email with different 'from' email address using gmail smtp server in django

Gmail doesn't allow you to change the From address when sending email through its servers.
Sources:
change sender address when sending mail through gmail in c#
How to change from-address when using gmail smtp server
https://support.google.com/mail/answer/22370?hl=en

Related

send_mail Django send as alias email

I'm solving problem, that of course I'm sending an email like sender email, which I authenticated in settings.py, but what if I want to send and email as request.user.email alias?
send_mail("Hello", "hello", settings.EMAIL_HOST_USER, [settings.EMAIL_RECEIVER])
is this some reason how to do that?
Thanks a lot
settings.EMAIL_HOST_USER Is used for authentication.
It should be like this:
send_mail("Hello", "hello", “sender#email.com”, [“recipient#email.com”], fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=None, connection=None, html_message=None)
To add an email alias you just need to add that field to your user model and reference that instead of “sender#email.com”

Django send email where smtp auth is different then from_email

I have set up the django smtp backend to use gmail smtp.
and it sends email perfectly, But there is one problem.
The authentication I use for gmail smtp is different then the from_email, still when I receive an email I see the from email id as the smtp auth email.
example:
my settings are as follow:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER= 'something#somedomain.com'
EMAIL_HOST_PASSWORD= 'password_for_something_at_gmail_com'
and to send an email I did
send_mail(subject=subject, message="test", from_email="other#mydomain.com",
recipient_list=to, fail_silently=False)
this works but the received email does not show
from : other#mydomain.com
it shows
from: something#somedomain.com
How do I make sure it shows other#mydomain.com.
NOTE: somedomain.com is connected with google apps and mydomain.com
is alias with it
and other#mydomain.com is just an fwd email id.
Gmail doesn't allow you to change the From address when sending email through its servers.
You can override From header with below email sending code.
from django.core.mail import EmailMessage
EmailMessage(subject, message, "<"+str(from_email)+">", recipient_list)
Note: Actually it will send email from the email id which you configured in settings.py file but From header will show from_email address

How to change reply-to and return-path header with gmail smtp in django

I am using gmail smtp in my django website. I have a contact form where user put email and message then I send a mail to the admin with :
email = EmailMessage('email subject', 'email message', settings.EMAIL_HOST_USER,
['admin.mysite#gmail.com'],
headers = {'Reply-To': 'user#foo.bar',
'Sender': 'user#foo.bar','from': 'user#foo.bar','Return-Path': 'user#foo.bar'})
email.send(fail_silently=False)
email is sent/received correctly but when admin client select reply in gmail, it always reply to the settings.EMAIL_HOST_USER and not the user address. On the email original header the From and Return-path are set with the setting.EMAIL_HOST_USER
Google violates the RFPs defining the expected operation of an SMTP server, rewriting the headers. This may be the root cause of your problem: http://lee-phillips.org/gmailRewriting/
If you use smtp.gmail.com to send a message, and the sender's email address is not yourgoogleemailname#gmail.com, then Gmail will rewrite the headers and set the from address to yourgoogleemailname#gmail.com. See http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server for more info, and for a possible solution.

The email_from in Django send_mail function not working [duplicate]

This question already has answers here:
How to change from-address when using gmail smtp server
(6 answers)
Closed 3 years ago.
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.
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.

Emails, a different 'reply to' address than sender address

I have a contact form on a website (a general form: name, email, subject, message) in which mails are sent using google apps smtp to the admins.
Currently if an administrator wants to reply to the mail directly selecting the reply option, the person's reply's To field will be filled by the sender's address automatically.
What I wan't to ask is, Is there any standardized way to pass on some additional info with the mail which would define any reply to the mail should go to this address instead of the sender's?
It does seems that there is a little chance for this option as it may lead to some problems due to spammers (They may define a custom reply field in their mail and a general user might not look where they are replying).
So as an alternative what I thought is to find a way to create a filter with sender's account which figures out the reply email address from the format and forwards the mail (Doesn't seems like a good solution and I have no idea how to achieve this).
I have tagged django, though this is not directly related with this, as I will finally implement this through django.
There are in fact standardized headers to specify response headers: http://cr.yp.to/immhf/response.html.
As far as implementing this in Django is concerned, the documentation contains an example:
from django.core.mail import EmailMessage
email = EmailMessage(
'Hello', # email subject
'Body goes here', # email body
'from#example.com', # sender address
['to1#example.com', 'to2#example.com'],
['bcc#example.com'],
headers={'Reply-To': 'another#example.com'},
)
This solved my problem.
Reply-To is a standard SMTP header.
I can't find a good reference for it at the moment, but it is mentioned in the Wikipedia article on Email.
Edit: Found it: RFC 5322, section 3.6.2
The RFC says you can specify multiple emails and that is what I was looking for. Came up with this:
from django.core.mail import EmailMessage
headers = {'Reply-To': 'email#one.com;email#two.com'}
msg = EmailMessage(subject, html_content, EMAIL_HOST_USER, email_list, headers=headers)
msg.content_subtype = "html"
msg.send()
Works like a charm. Note: EMAIL_HOST_USER is imported from your settings file as per Django doc email setup. More on this here, search for 'reply-to': https://docs.djangoproject.com/en/dev/topics/email/
Here is also how reply-to can be used
from django.core.mail import EmailMessage
email = EmailMessage(
'Hello',
'Body goes here',
'from#example.com',
['to1#example.com', 'to2#example.com'],
['bcc#example.com'],
reply_to=['another#example.com'],
headers={'Message-ID': 'foo'},
)
Read more at docs docs.djangoproject