The email_from in Django send_mail function not working [duplicate] - django

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.

Related

SMTPServerDisconnected when sending email from a Django App on Heroku with Mailgun

I'm attempting to send email from a very simple Django app. The app is hosted on Heroku, and I have the Mailgun add-on installed. However, whenever I attempt to actually send an email using Mailgun, I get a SMTPServerDisconnected error, with the message Connection unexpectedly closed.
I've gone through almost every tutorial that I can find on the subject, and have still had no luck. My settings.py is exactly as described in the Heroku Mailgun documentation:
EMAIL_USE_TLS = True
EMAIL_HOST = os.environ.get('MAILGUN_SMTP_SERVER')
EMAIL_PORT = os.environ.get('MAILGUN_SMTP_PORT', '')
EMAIL_HOST_USER = os.environ.get('MAILGUN_SMTP_LOGIN', '')
EMAIL_HOST_PASSWORD = os.environ.get('MAILGUN_SMTP_PASSWORD', '')
The os environment variables referenced are managed by Heroku. When I've searched for similar problems, every other question has been using Port 465, however, my port is on 587, which shouldn't have this issue as far as I know.
Currently, I'm using the sandbox domain provided by Mailgun just to attempt to send a test email to a verified recipient email. The following code is used in my views.py to actually send the email, using the django django.core.mail.send_mail() function.
subject = "Thank you for RSVP-ing!"
message = f'Hello World!'
email_from = "mailgun#blannholley.com"
recipient_list = [request.POST.get('email'), ]
send_mail(subject, message, email_from, recipient_list, fail_silently=False)
Any help would be greatly appreciated, as this is my first time attempting to send email over SMTP, so I'm very lost here and feel like I've tried everything.

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.

Could not send email in django rest framework

I am developing password reset part of the project. I am using django_rest_passwordreset to get the password reset. I am using mailjet smtp. I could not send the email to the user.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'in-v3.mailjet.com'
# EMAIL_PORT = 465
EMAIL_PORT = 587
EMAIL_USE_TLS = True
# EMAIL_USE_SSL = True
EMAIL_HOST_USER = '5e4329460b3c88f1d24d19c3e7374548aa213da%asasd1asd'
EMAIL_HOST_PASSWORD = 'a6c5ab2515d6ae761253a396453530ba$42asasdasdaasdasd'
If I change the EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' to the EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' it is printing it to the console. I have no idea why it is not working.
the part of the code where I am trying to send an email.
#receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
# send an e-mail to the user
context = {
'current_user': reset_password_token.user,
'username': reset_password_token.user.firstname,
'email': reset_password_token.user.email,
'reset_password_url': "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)
}
# just checking if it works
send_mail('Hello from something', 'hello there', 'abdukhashimov#yandex.ru',
[reset_password_token.user.email, ], fail_silently=False)
# render email text
email_html_message = render_to_string('user_reset_password.html', context)
email_plaintext_message = render_to_string(
'user_reset_password.txt', context)
msg = EmailMultiAlternatives(
# title:
"Password Reset for {title}".format(title="Some website title"),
# message:
email_plaintext_message,
# from:
"noreply#somehost.local",
# to:
[reset_password_token.user.email]
)
msg.attach_alternative(email_html_message, "text/html")
msg.send()
I came up with a different solution. I used the google smtp service. I have followed the steps from this kinsta.com - steps to setup up google smtp.
Step1: The very first thing you will need to do is ensure that you have 2-step verification enabled on your primary Gmail account. Important: If you don’t do this you will get an invalid password error further below when trying to authenticate your email address. So first go and enable 2-step verification.
Step2: Next, you will need to generate an App password. You then use the app password in place of your personal Gmail password further below. This is the only way this process will work.
Step3: Now back in Gmail, go to settings, and can click on “Accounts and Import.” Then click on “Add another email address you own.”. Basically to the gmail and sign in with your account and go to the settings.
Step4: Enter your additional business name and business email that is on the custom domain.
(Extra Info). I usually use yandex mail and added it then it generated the followings.
Step5: It will then send an email confirmation code to the email you just added. You will need to click the link in the email to confirm it or manually enter the code (this proves that you are in fact the owner of the additional email account). And that’s it!
From my experience, you might need to tweak some settings from google if it did not work for you. For example, I read from other source, you might need to allow less secure apps from google. I have not done it as I was using yandex mail, I guess.
In case if you are not sure what to put in settings.py
EMAIL_HOST = 'smtp.yandex.ru' # in my case
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'added account'
EMAIL_HOST_PASSWORD = 'your password'
Credits to kinsta.com

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

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