Send emails to the users without requiring EMAIL_HOST_PASSWORD in Django - django

I am trying to make a "noreply" email to send the users. But I dont want to put password in the EMAIL_HOST_PASSWORD field. Is it possible? I tried this exact thing with PHP and it was a success. How can I do this using Django?

Run in terminal:
pip3 install python-decouple
If you are using python3, but if you are using python2 type only pip instead of pip3
Create .env file in the root of your django project and write:
EMAIL_HOST_USER = 'blablabla#gmail.com' #my email
EMAIL_HOST_PASSWORD = 'fhiahsffd' #my password
Add the line .env in your .gitignore
...
#other lines
.env
...
In settings.py
import os
from decouple import config
...
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
...
Doing this your email password is protected, that was the matter I imagine

No, you can't send emails in django without EMAIL_HOST_PASSWORD, because you need to authenticate to server(for example: smtp server) in order to successfully send emails.
You can use env file to store your those values and create environment variable in settings.py and these variable will get their value from env file.
This way you will be able to send emails in django without mentioning those values in settings.py and keep env secure and out of git(or any SCM/VCS sotfware you are using.)

If you are running internally, say from a company's server, you can use their SMTP server and send emails without a password. Just change the EMAIL_PORT to the needed email port, adjust the EMAIL_HOST to the company's SMTP server, and change the EMAIL_HOST_USER to the email being used.

Related

How to send email on smtp server in django?

here is m settings.py file
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAL_HOST_USER = 'test.mailed.login#gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
Google used to have a less-secure apps settings now that it is removed, I am using this alternatively
I obviously went on a google and created a new app and filled it on the EMAIL_HOST_PASSWORD
I also enabled two step authentication
Obviously i changed my app password on the above code so no one can see it
I imported send_mail and run the function but it still sending me this error
error details
This is exactly what I have done nothing more or nothing less
Every youtube video that I check it is working for all of them, Am I missing an earlier step
I think you need to generate app password from google app password generator.
after generating the password add the password in SMTP E-Mail settings.
You can generate password from here

Sending Emails in Production Environment

I have a web app I'm hosting on Digital ocean using nginx and gunicorn. I recently tried to add password reset capabilities for my users as well as a contact form. When I ran and tested on my local machine everything worked fine, but now that I've moved to production I get a 500 error when I try to send a password reset email, and my contact form is not generating any email message.
Is there some additional set up related to digital ocean, or nginx that needs to be done to allow emails to be sent?
my settings.py is set up as follows:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'admin#programaticlearning.com'
EMAIL_HOST_PASSWORD = 'CorrectPassword'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
I found the problem:
I needed to create the Site with the correct domain in order for Django's Authentication views to work properly. I was using a different database in production and hadn't yet created the proper domain in the Site table.

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 can I send emails in Django using smtp.EmailBackend without authenticating with the mail server

Is there a way to explicitly tell Django not to authenticate with the mail server when sending the emails.
I am currently using the following settings in my settings.py for sending emails.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'my-mail-server'
EMAIL_PORT = 25
Please not that I have no EMAIL_HOST_USER and EMAIL_HOST_PASSWORD and the smtp mail server I am using doesn't require clients to authenticate
Edit:
When I use those settings I get this error
smtp.SMTPSenderRefused: Client was not authenticated
If server does not require authentication, for example Gmail SMTP relay (authenticate by IP), you need to skip authentication step.
If you are using django.core.mail.backends.smtp.EmailBackend you can find in it's code:
if self.username and self.password:
self.connection.login(self.username, self.password)
It means if your username or password is empty, EmailBackend will skip authentication on the server.
So, you need to keep EMAIL_HOST_USER and EMAIL_HOST_PASSWORD empty or do not specify them at all.
You can use console banked.
It writes e-mails to standard out instead of sending them.
just refer this link
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
This backend is not intended for use in production – it is provided as a convenience that can be used during development.
Also python has this in-build smtp server here
In my case I was using a Gmail account. For that, had to add in your 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 = 'tiagomartinsperes#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
Then, needed to make sure that my Gmail account allows apps send emails on my behalf.
From Gmail, click in the upper right icon with the Picture
and then go to "Manage your Google Account".
In there, click in "App Passwords", click in the dropdown "Select app" and select "Other (Custom name).
Give the name of your app and this will generate a password. Copy that and paste in EMAIL_HOST_PASSWORD = 'password' in your settings.py.
And the app appears in the list then
You can delete this anytime you don't want anymore.
Then if you test again, the app should send emails just fine.
Even though I've used the specific case of Gmail here, similar procedure would have to be applied to other apps like Mailgun.
If you're running a development environment, you have other options, namely
save the file to console using EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'.
save the file to a specific folder using EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' and adding the path EMAIL_FILE_PATH = os.path.join(BASE_DIR,'static','media','email') (this will save in static/media/email).
save the file tp a special attribute of django.core.mail (which is what Djano's test runner automatically uses for testing) using EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
use dummy backend which does nothing to your messages using EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'.
Note that as we can read in the documentation, these options are
not intended for use in production – it is provided as a convenience
that can be used during development

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.