Django Mail Setup

Sending email using Django is pretty easy and require less configuration. In this tutorial, we will send email to provided email.

For this purpose, we will use Google's SMTP and a Gmail account to set sender.

Django provides built-in mail library django.core.mail to send email.

Before sending email, we need to make some changes in Gmail account because for security reasons Google does not allow direct access (login) by any application. So, login to the Gmail account and follow the urls. It will redirect to the Gmail account settings where we need to allow less secure apps but toggle the button. See the below screenshot.

https://myaccount.google.com/lesssecureapps login Gmail account and follow the urls

After that follow this url that is a additional security check to verify the make security constraint.

https://accounts.google.com/DisplayUnlockCaptcha security check to verify the make security constraint

Click on continue and all is setup.

Django Configuration

Provide the smtp and Gmail account details into the settings.py file. For example

snippet
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'irfan.iit003@gmail.com'
EMAIL_HOST_PASSWORD = '*********'

Import Mail Library

snippet
from django.core.mail import send_mail

Now, write a view function that uses built-in mail function to send mail. See the example

Django Email Example

This example contains the following files.

// views.py

snippet
from django.http import HttpResponse
from djangpapp import settings
from django.core.mail import send_mail


def mail(request):
    subject = "Greetings"
    msg     = "Congratulations for your success"
    to      = "irfan.sssit@gmail.com"
    res     = send_mail(subject, msg, settings.EMAIL_HOST_USER, [to])
    if(res == 1):
        msg = "Mail Sent Successfuly"
    else:
        msg = "Mail could not sent"
    return HttpResponse(msg)

// urls.py

Put following url into urls.py file.

snippet
path('mail',views.mail)

Run Server and access it at browser, see the output.

Run Server and access it at browser output

Here, the both email ids are mine, so I can verify the email by login to the account.

And after login, here we go!! I got the mail.

After login to the account

Well, same like, we can send mail using other smtp server configurations if we have.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents