How to configure Django and G Suite for SMTP - django

When the user is registering on my website an E-Mail is sent to the user to confirm his/her E-Mail.
It works with this settings:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'abcdefghiklmnopq'
EMAIL_PORT = 587
Now I want to switch to noreply#mydomain.com.
I created an account on G Suite and made the following configurations:
Comprehensive mail storage (Locally applied) Ensure that a copy of all
sent and received mail is stored in associated users' mailboxes: ON
SMTP relay service (Locally applied) ms_mail Allowed senders: Only
addresses in my domains Only accept mail from the specified IP
addresses: No Require SMTP Authentication: Yes Require TLS encryption:
Yes
Less secure apps (Locally applied) Allow Users to manage their access to less secure apps
Less Secure Apps
Allow less secure apps: ON
Than I created an App Password an tried a lot of configurations like this:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp-relay.gmail.com'
EMAIL_HOST_USER = 'noreply#mydomain.com'
EMAIL_HOST_PASSWORD = 'abcdefghiklmnopq'
DEFAULT_FROM_EMAIL = 'noreply#mydomain.com'
SERVER_EMAIL = 'noreply#mydomain.com'
EMAIL_PORT = 465
I can't find a good documentation on Google or Django how to configure the settings. Does anybody now a good resource? Is the App Password/Less secure Apps the right way to do it? Because Google has some security warnings. Noreply is a group but I created the app password for my.name#mydomain.com. Is this a problem when I am part of the group? I also tried several options with my.name#mydomain.com instead of noreply#mydomain.com but I always get the error
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Use this in your settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'noreply#mydomain.com'
EMAIL_HOST_PASSWORD = '#########'
EMAIL_PORT = 587

Related

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

How to send report mail like Google

I have a web using Django framework run in localhost, I want web send email automatically to a email address, but I have a trouble in config EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD and EMAIL_PORT in settings.py
this is settings.py I configured:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST = 'localhost'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 25
How can I fix to send email?
Your project runs on localhost but your mailserver runs on google
try this
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD
You will need to set up Gmail, say with an application password.
See the google guide here and for app passwords here
If you connect using SSL or TLS, you can send mail to anyone with
smtp.gmail.com.
Note: Before you start the configuration, we recommend you set up App
passwords for the the desired account. Learn more at Sign in using App
Passwords and Manage a user's security settings.
Connect to smtp.gmail.com on port 465, if you’re using SSL. (Connect
on port 587 if you’re using TLS.) Sign in with a Google username and
password for authentication to connect with SSL or TLS. Ensure that
the username you use has cleared the CAPTCHA word verification test
that appears when you first sign in.
But if you are using gmail then you should look into the gmail api because then you can deal with inbound mails and you can use labels etc to manage the mails, it works well eventually once you get your head around oAuth etc. I know you didn't ask about that, but I personally find it useful.

SMTPSenderRefused at /users/password-reset/ (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/ma

I am getting this error while sending mail using django.
/usr/lib/python3.6/smtplib.py in sendmail, line 867
/usr/lib/python3.6/smtplib.py in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
tried mailjet also same issues
If your are using Google SMTP make sure you have the following settings set in your settings.py file:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user emailid'
EMAIL_HOST_PASSWORD = 'password'
Also make sure you are logged in with gmail with the provided email id and password in your machine.
Also you need to enable access for less secure apps in your Google account. Here is the link to help you change your Google Account configuration settings link
If you saved your email/password in environ variable for the first time. Please close your terminal and open it again, I hope this will solve your problem. I have run into the same problem and solved issues just like this.
You need to create app password via your google account.
You can't use your email address to send mail.
Go to this link: https://myaccount.google.com/security
For me the error was caused by:
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
as the env variables where not correctly imported.
Also remember to enable access for less secure apps in your Google account.
Your users must be active and have valid e-mail.
Go here https://myaccount.google.com/security, press on app password and gererate 16-symbolic password and put it to settings.
And use it in settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'put here your email-address with 16-symbolic password'
EMAIL_HOST_PASSWORD = 'put here your 16-symbols symbolic'

How to setup Smtp and webfaction email host in a single settings.py file in django1.5.1?

This the one host that i created I want to add another one host also.
EMAIL_USE_TLS = True`
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587 `
EMAIL_HOST_PASSWORD = 'developer#sangeeth'
EMAIL_HOST_USER = 'developer.sangeeeth#gmail.com'
More than one EMAIL_HOST cannot be defined in django settings.py. Read more about email settings and sending emails in django. If you want to use different email hosts for sending emails, you can try python smtplib for sending emails instead of django's email feature.

How can I send e-mail from django using the google smtp server?

I'm looking at https://docs.djangoproject.com/en/dev/topics/email/
My question is, is there way I can use smtp.google.com without authentication or without having to put my auth information into settings.py or as a parameter in the django.core.mail.send_mail function?
At this point I'm looking for best practices for using smtp.google.com on django, I understand there are better solutions such as http://sendgrid.com/
You cannot use smpt.gmail.com without providing your auth_information i.e your gmail password.
However you can put your auth information in a local_settings.py and do not add this local_settings in version control so no one except you would see this file. Include this local_settings in your settings.py.
settings.py
...
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
...
...
from local_settings import *
local_settings.py
EMAIL_HOST_USER = 'user#gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
try including this in settings.py:
# Email configuration.
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user#domain.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'user#domain.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
If you have a web domain provider (like namecheap, godady, etc) you can associate you domain (mycompany.com) with Gmail. For that feature ask help in your domain provider or look info in Internet:
http://www.namecheap.com/support/knowledgebase/article.aspx/1244/78/
http://help.squarespace.com/customer/portal/articles/581494-how-do-i-set-up-google-apps-for-my-domain-
Hope it helps,
cheers.