Remember when you we signed-up to an application and received a welcome email or you once forgot your password and got an email with password reset link. This small things adds weight to User Experience.
In this article, we will see how to send emails to our users using Django framework and SMTP server. Without wasting any minute let’s jump to the interesting part.
Setting up Gmail for django mail API:
In order to use Google SMTP, you should have a gmail account. Next thing you need is to enable Allow less secure app feature under account security settings. This feature is used for securing your google account from the apps that are less secure to use it in order to prevent hackers from muddle into your account.
Configuring settings.py:
In your settings.py file, add the following configurations.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your gmail account'
EMAIL_HOST_PASSWORD = 'your account’s password'
EMAIL_USE_SSL = False
Let’s understand these terminologies:
EMAIL_BACKEND:
This setting specifies the backend that we will use for sending an email in Django.
EMAIL_HOST:
This setting is to specify the email service provider.
EMAIL_USE_TLS:
This setting specifies whether the Email uses a TLS connection or not. It is True for Gmail.
EMAIL_PORT:
This is the default setting for Gmail. It is the port used by the SMTP server.
EMAIL_HOST_USER:
The name of the email account which will be used.
EMAIL_HOST_PASSWORD:
The password of the email account which will be used.
EMAIL_USE_SSL: False for Gmail.
Note: Your password is visible here so before deploying it encrypt your password or place in a file or server, where only have access.
Configuring views.py:
Django provides a module named send_mail, that we will use to send email to the users. There are many options available for the send_mail but for simplicity we will use only 4 fields.
subject:
It contains the subject of the email
message:
It contains the body of the email
email_from:
It is the sender’s mail address.
recipient_list:
It is the receiver’s email address.
from django.core.mail import send_mail
from django.conf import settings
def mail(request):
...
subject = 'your subject'
message = 'your message'
email_from = settings.EMAIL_HOST_USER
recipient_list = ['receiver's mail address', ]
return render(request,'some_page.html')
Configuring urls.py:
urlpatterns = [
path('mail', views.mail, name ='mail'),
]
With this your code is ready for sending the email to the users. All you need now is to run the server and check whether your methods are functioning properly.
That’s was it for this article. Thanks for reading.
You can connect with me on Twitter for any discussions.
Adios.
Top comments (3)
Thanks for the information but Google has disabled this setting. What can be done now?
Hi Karan,
Instead of logging in with the account password, you'll now log in using a password generated for a specific app. You may follow the following steps.
EMAIL_HOST_PASSWORD
.I have tested it and it works. Let me know if you still face any issue.
I am facing an issue with this, when I send the email to my email or someone who is at my home(connected to my wifi network) it sends the email. But when I send it to someone who is not on my network it doesn't show any errors but no email is recieved by them. Why is that how to solve it?