Sending emails on Django using SMTP and Gmail - django

I have the following problem with gmail and smtp, i allowed access to less secure apps on my gmail account and i'm able to send emails on my local computer, however when i try to send emails on the remote server that's hosting my application i keep getting SMTPAuthenticationError. I'm not sure what else do i need to do in order for this to work both locally and on the remote server. Anybody have any ideas?
I'm using Django 1.11.15 and Python 2.7.
These are the settings i have on my application:
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "myemail#gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"

The password is your gmail's login password? Visit https://myaccount.google.com/security, On the left navigation panel, click Security. In Signing in to Google panel, click App passwords, to get a password. It work for me.
If it not work, check Django SMTPAuthenticationError.

Related

How can I send emails with Django to my gmail account

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.

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.

Django Sending Email with Office365 on Pythonanywhere

I have deployed my web project on Pythonanywhere. Im sending email with gmail SMTP but i want to send with Office365 (company account). I used many options but i didnt figure it out. It exists in whitelist of Pythonanywhere
It throws that error:
Expection value : [Errno 101] Network is unreachable
in my settings.py like that:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.office365.com'
EMAIL_HOST_USER = 'xyz#domainname.com'
EMAIL_HOST_PASSWORD = '########'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'xyz#domainname.com'
SERVER_EMAIL = 'xyz#domainname.com'
Thank you all.
Free accounts on PythonAnywhere can only connect to the whitelisted external servers using HTTP or HTTPS; you're trying to connect using SMTP, which is a different protocol. If you want to send email from a free account, you need to either use GMail, or use an HTTPS-based email solution like MailGun or SendGrid. Alternatively, you could upgrade to a paid account, which would allow you to use SMTP to other servers.
There's more information on this PythonAnywhere help page.

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.

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