DEV Community

loading...
Cover image for How to send email using SMTP server in django?

How to send email using SMTP server in django?

Shubham Singh Kshatriya
Ideas have always excited me. The fact that we could dream of something and bring it to reality fascinates me and Software Engineering provides a window for me to do so.
・2 min read

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

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

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

Configuring urls.py:

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.

Adios.

Discussion (0)