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.
Related
I've tried sending emails with my Gmail account in Django, but I always get errors. I tried using a sandbox (Mailtrap) and it worked but using the actual Gmail account doesn't. I've also tried using App Password in my google account but it still doesn't work.
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "example#gmail.com"
EMAIL_HOST_PASSWORD = "password"
Error I always get;
SMTPConnectError at /send/
(421, b'Service not available')
Please how can this be fixed.
I've done it recently using Google App Password. Follow instructions to get 2-step verification, get the password and then put it in your EMAIL_HOST_PASSWORD enviromental variable with quotes. (EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxxxxxx'). The other parameters I've set are as follows:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
Finally I send the emails on my view with send_mail
from django.core.mail import send_mail
You have serval options.
Apps password smtp
Does the google account you are using have 2fa enabled, if it does then you can create an apps password. Then take the apps password and use that implace of the password in your code.
XOauth2 smtp
Depending upon the system you are using to send emails it may support Xoauth2 authorization. If it does then you can use that to send emails via the smtp server.
Gmail api. Oauth2 or service accounts
You can switch to using the gmail api, if you have a workspace account then you could use service accounts. If not then you will need to stick with Oauth2 and authorize your appliaton once. This is simarl to using Xoauth2 with the smtp server only you will be going though the gmail api instead.
How to create a Apps Password for connecting to Google's SMTP server.
I am trying to send a mail from my django application through gmail and i get the error saying "SMTP AUTH extension not supported by server."
I have added
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USER_TLS = True
EMAIL_HOST_USER = 'abc#gmail.com'
EMAIL_HOST_PASSWORD = 'random'
in my setting.py.
Can anyone please help me figure out the solution?
EMAIL_USER_TLS IS WRONG.
Use EMAIL_USE_TLS instead (fix typing error).
Gmail offers SMTP AUTH on port 587 only after STARTTLS.
Without STARTTLS (turning on encryption) SMTP AUTH is unavailable.
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
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'
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.