SMTPRecipientsRefused: Sender address rejected: not owned by user - django

I have a Django app running on a server with uWSGI and nginx.
In my local_settings.py file I have this:
###############
# EMAIL SETUP #
###############
EMAIL_HOST = 'smtp.privateemail.com'
EMAIL_HOST_USER = 'support#mydomain.com'
EMAIL_HOST_PASSWORD = 'MY EMAIL PASSWORD'
EMAIL_PORT = 465
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
########################
# OTHER EMAIL SETTINGS #
########################
ADMIN_EMAIL = "admin#mydomain.com"
SUPPORT_EMAIL = "support#mydomain.com"
DEFAULT_FROM_EMAIL = ADMIN_EMAIL
SERVER_EMAIL = ADMIN_EMAIL
I run python manage.py runserver on my local machine in the Django project's virtual environment. I fill out the password reset form at password_rest/ using the email my.personal#gmail.com and submit it. I get this error.
SMTPRecipientsRefused: {u'my.personal#gmail.com': (553, '5.7.1 <admin#mydomain.com>: Sender address rejected: not owned by user support#mydomain.com')}
My website's email provider is Namecheap.
Why do I get this error when testing on my local machine? What must I change/add to get rid of it?

Changed EMAIL_HOST = 'smtp.privateemail.com' to EMAIL_HOST = 'mail.privateemail.com'

Related

how do I configure my Django app to use Dreamhost email?

I created a django app that has a contact form where users can send an email to the company (The email configured in the APP)
I am getting "SERVER ERROR 500"
After reading this:
django email settings on dreamhost
I tried the following format in my settings.py:
EMAIL_HOST = 'smtp.dreamhost.com' # I also tried pop.dreamhost.com / imap.dreamhost.com with their respective port numbers.
EMAIL_USE_TLS = True # I also tried EMAIL_USE_SSL
EMAIL_PORT = 587 # I also tried 25 and 465
EMAIL_HOST_PASSWORD = 'EMAIL_PASSWORD' # also tried SERVER_PASSWORD
EMAIL_HOST_USER = 'my_dreamhost_email' # Also tried admin#mydomain.com
EMAIL_SUBJECT_PREFIX = ''
SERVER_EMAIL = 'my_dreamhost_email' # also tried 'localhost' / 'admin#mydomain.com'
ADMINS = (('Jack Shedd', 'jack#example.com'),) # I used my details
PS: The app is working perfectly if I use gmail
What are the correct details to use?
Some thoughts about that:
Change your settings.py:
DEBUG = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ADMIN_EMAIL = 'Dont reply <no-reply#your_dreamhost_email.com>'
SUPPORT_EMAIL = 'Dont reply <no-reply#your_dreamhost_email.com>'
DEFAULT_FROM_EMAIL = ADMIN_EMAIL
SERVER_EMAIL = ADMIN_EMAIL

Django / Sending email with Mail Service Prodiver like Mailjet

I have installed django-anymail
My SPF and DKIM are configured in my ISP
Here is my settings.py :
EMAIL_BACKEND = "anymail.backends.mailjet.EmailBackend"
ANYMAIL = {
"MAILJET_API_KEY": os.getenv('MYAPP_MAILJET_API_KEY'),
"MAILJET_SECRET_KEY": os.getenv('MYAPP_MAILJET_API_SECRET'),
}
EMAIL_HOST = 'in-v3.mailjet.com'
EMAIL_HOST_USER = '????'
EMAIL_HOST_PASSWORD = '????'
EMAIL_HOST_PORT = 587
Views.py
send_mail('contact form','click',mail1,[mail2],fail_silently=False)
I got confused for
EMAIL_HOST_USER and EMAIL_HOST_PASSWORD
As I want to use mailjet, is it necessary ? Also is send_mail the right function in this case ?

How to send with Sendgrid a reset password email from a Django app deployed in Heroku?

I have been struggling for a while in trying to correctly setup the settings.py to send an email for password reset.This is my current configuration:
SENDGRID_API_KEY = os.environ["SENDGRID_API_KEY"]
SENDGRID_PASSWORD= os.environ["SENDGRID_PASSWORD"]
SENDGRID_USERNAME= os.environ["SENDGRID_USERNAME"]
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
#EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ["SENDGRID_PASSWORD"]
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ["DEFAULT_FROM_EMAIL"]
#SENDGRID_SANDBOX_MODE_IN_DEBUG = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
I have come across the following posts that are related to my problem but none of them have worked:
Sending SMTP email with Django and Sendgrid on Heroku
No module named 'sendgrid_backend' in django
Send email with Sendgrid in Django
Setting up email with Sendgrid in Heroku for a Django App
Heroku, Django, and Sendgrid - emails not sending?
When I used the EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"(after I installed the django-sendgrid-v5 library) I didn't receive any error but I didn't receive any email :'( and in the previous cases I encountered the following error SMTPServerDisconnected at /password-reset/ and Connection unexpectedly closed. Any help, suggestion, comment, crazy idea is really appreciated because I have spent several hours in this problem that will be a milestone in my project.
Thank you everyone :)
I had a similar issue deploying a Django app on Google Cloud App Engine. The following configuration (in settings.py) worked for me:
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # Exactly that.
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587 # 25 or 587 (for unencrypted/TLS connections).
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL')
For SENDGRID_API_KEY, use the API key created in the 'Email API/Integration Guide' section. In your case, choose the SMTP Relay option.
For DEFAULT_FROM_EMAIL, use the verified single sender email. You can find it in 'Settings/Sender Authentication'. This is what you type in "from email address" when you create a Sender. More details here.
I don't recommend Robert D's answer because by using ALLOWED_HOSTS = ['*'] you will be prone to HTTP Host header attacks. From the documentation:
"A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE)."
More details here.
I sympathise, having also spent too long on this. But just fixed it today, with as little as the following added into settings.py (and everything else 'out of the box' from Django re password reset).
ALLOWED_HOSTS = ['*']
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True

Failing at sending mail from django

I'm trying to send an "email" from a website in django.
I have completed the main code for doing so:
-)the view function
-)the URLs mapping to make the function reachable from code
-)the sending form at a template
So my sending form would trigger the view function using the path specified in the URLS.
On my server, I have a "postfix" instance installed and tried.
I tried to edit changes in the settings.py and the views.py for about 2 days now but nothing worked.
The errors range between these two
1)
SMTPNotSupportedError at /website/email_send
when settings are
EMAIL_HOST = 'mydomain.com'
EMAIL_PORT = 25 //same for port 587
EMAIL_HOST_USER = 'uname'
EMAIL_HOST_PASSWORD = 'pwd!'
EMAIL_USE_TLS = True
2)
gaierror at /website/email_send
[Errno -2] Name or service not known
when settings are
EMAIL_HOST = 'mail.mydomain.com' or 'smtp.mydomain.com'
EMAIL_PORT = 25 //same for port 587
EMAIL_HOST_USER = 'uname'
EMAIL_HOST_PASSWORD = 'pwd!'
EMAIL_USE_TLS = True
I expect the email to be sent using a form in my django site run on a server using postfix
The problem is now fixed.
I found this question and used its answer:
Postfix + Django: SMTPException: SMTP AUTH extension not supported by server
I removed EMAIL_HOST_USER = 'uname' and EMAIL_HOST_PASSWORD = 'pwd!'
Then it worked without errors. The settings now are:
...
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_USE_TLS = True
...

Setting up email with Sendgrid in Heroku for a Django App

I am deploying a Django app on Heroku, and using the Sendgrid addon to send out validation email when a user registers on the site.
I followed the instructions here and pasted the following into settings.py:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgrid_username'
EMAIL_HOST_PASSWORD = 'sendgrid_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
However, my app is crashing after registration.
What exactly am I supposed to put for EMAIL_HOST_USER and EMAIL_HOST_PASSWORD?
Under the developer's tab in the sendgrid addon in heroku, it gives me the username app*******#heroku.com, and for password it just says "Your Password". Is the password my Heroku password?
Also, do I need to include DEFAULT_FROM_EMAIL in my settings.py file? And where do I tell Sendgrid what it is?
EDIT: I've set DEBUG = True, and it looks like the error is:
SMTPSenderRefused
(550, 'Cannot receive from specified address <info#myapp.com>: Unauthenticated senders not allowed', 'info#myapp.com')
it looks like the problem is happening before Sendgrid does its thing. Do I need to authenticate the email address with Heroku somehow?
Within your settings.py include:
import os
EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
EMAIL_HOST= 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = os.environ['SENDGRID_PASSWORD']
Edit: changed EMAIL_PASSWORD to EMAIL_HOST_PASSWORD as that's the correct spelling.
In the intervening 10 years, the above answer has become obsolete. Sendgrid now uses an API key.
https://docs.sendgrid.com/for-developers/sending-email/django
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True