Accessing environment variables in production in Django deployed to Heroku - django

I am developing a web-app in Django that needs to send automated emails. The username and password are saved locally in my Windows 10 environment variables. The app is able to send emails locally, but in proudction once deployed to Heroku, the following error is raised:
530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError y7sm6660123qtn.11 - gsmtp', 'webmaster#localhost'
My guess is that the environment variables are not being accessed which is why this error is thrown.
Here is my code in settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = os.environ.get('NAHE-USER')
EMAIL_HOST_PASSWORD = os.environ.get('NAHE-PASS')

You can define the environment variables (NAHE-USER, NAHE-PASS) in Heroku using Config Vars.
There are injected at runtime and will be accessed by your code with os.environ.get('NAHE-USER')

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.

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

Send emails to the users without requiring EMAIL_HOST_PASSWORD in 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.

Sending SMTP email via Django in production environment

I am using Django-Registration for my website on a linode ubuntu
virtual. I get connection problem sending activation email when users
sign up.
Here is my settings:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True
I've tested locally and it works. I tested interactively via python
manage.py shell on the host and it works when I logged in as root. I
think there is a problem with permission since Apache is running under
www-data. Do you have any ideas how to fix this?
EDIT:
Someone on Linode IRC suggests that I install ssmtp package...I followed the instruction found here http://tombuntu.com/index.php/2008/10/21/sending-email-from-your-system-with-ssmtp/ and reboot the server...everything works great now.
Thank you
Try setting
EMAIL_USE_TLS = 1
rather than EMAIL_USE_TLS = True. That's my guess. If that doesn't work, try
EMAIL_DEBUG = False
although honestly I'm not sure what that one does.