Django send email where smtp auth is different then from_email - django

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

Related

AWS SES sending email from Django App fails

I am trying to send mail with using SES and already setup the mail configuration. Now SES running on production mode -not sandbox-. But in Django App, when I try to send mail nothing happens. it's only keep trying to send, no error.
in setting.py made the configuration.
INSTALLED_APPS = [
'django_ses',
]
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_SES_REGION_NAME = 'ap-northeast-1'
AWS_SES_REGION_ENDPOINT = 'email-smtp.ap-northeast-1.amazonaws.com'
and email method.
def send_mail(request, user, emails, subject, path):
current_site = get_current_site(request)
message = render_to_string(f"store/emails/{path}.html", {
"some": "context"
})
send_email = EmailMessage(subject, message, "info#mydomain.com", to=emails)
send_email.send()
By the way I already verified the info#mydomain.com in SES console. And when I try to send email from the SES console using send test email option, I can send without a problem. But in Django App. I can't.
Is there any other settings should I do. Because I can't see any error popping when I try to send mail. It's only keep trying to send. But it can't.
AWS SES provides two types of endpoints: API and SMTP
You are using SMTP one
AWS_SES_REGION_ENDPOINT = 'email-smtp.ap-northeast-1.amazonaws.com'
But 'django_ses.SESBackend' working with API type.
Try to set:
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_SES_REGION_ENDPOINT = 'email.ap-northeast-1.amazonaws.com'
If you want to use usual SMTP connection, you probably need to swap current backend with backends.smtp.EmailBackend and set SMTP connection parameters.

changing from address on send_mail

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

unable to send emails django-helpdesk

I am using django-helpdesk for ticketing system.
its working fine but i am not able to send emails. i am testing this on my localhost.
while i submit a ticket, its getting submitted properly but submitter is not getting any email for newly created ticket.
i added new queue and provided details like E-Mail Address, E-Mail Box Type, E-Mail Hostname, E-Mail Port, Use SSL for E-Mail=False, E-Mail Username, E-Mail Password, IMAP Folder, E-Mail Check Interval properly.
but emails not getting sent.
then i removed all off above from queue and added settings like
QUEUE_EMAIL_BOX_TYPE = IMAP
QUEUE_EMAIL_BOX_SSL = True
QUEUE_EMAIL_BOX_HOST = 'smtp.gmail.com'
QUEUE_EMAIL_BOX_USER = 'email'
QUEUE_EMAIL_BOX_PASSWORD = 'pwd'
but still its not working.
am i missing any settings? please help me out.
QUEUE_EMAIL_BOX settings are not for sending emails, but for receiving them. Configuration for sending emails goes into standard email settings from django.
If you're using SMTP backend (this is default), configuration should be:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_USE_SSL = True

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.