DEV Community

Cover image for How to send email using SMTP server in django?
Shubham Singh Kshatriya
Shubham Singh Kshatriya

Posted on

How to send email using SMTP server in django?

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.
Alt Text


In your file, add the following configurations.

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'your gmail account'
EMAIL_HOST_PASSWORD = 'your account’s password'
Enter fullscreen mode Exit fullscreen mode

Let’s understand these terminologies:

This setting specifies the backend that we will use for sending an email in Django.

This setting is to specify the email service provider.

This setting specifies whether the Email uses a TLS connection or not. It is True for Gmail.

This is the default setting for Gmail. It is the port used by the SMTP server.

The name of the email account which will be used.

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.


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.

It contains the subject of the email

It contains the body of the email

It is the sender’s mail address.

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')
Enter fullscreen mode Exit fullscreen mode


urlpatterns = [
    path('mail', views.mail, name ='mail'),
Enter fullscreen mode Exit fullscreen mode

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.


Top comments (2)

thekaranatic profile image
Karan Kakati

Thanks for the information but Google has disabled this setting. What can be done now?

shubhamkshatriya25 profile image
Shubham Singh Kshatriya

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.

  1. Turn on 2-Step Verification in your google account.
  2. Go to generate apps password and generate a password for your app.
  3. Use this password as EMAIL_HOST_PASSWORD.

I have tested it and it works. Let me know if you still face any issue.