Send Email from Django using Gmail SMTP

For small applications that shoots only two or three emails per day, SES is an overmuch. This tutorial shows how to use Gmail SMTP to send emails from Django.

Send Email from Django using Gmail SMTP

In the last post, we learned to send emails in Django using Amazon SES. It was not a difficult task and suits for many medium to large-scale applications.

For small applications that shoots only two or three emails per day, SES is an overmuch. This tutorial shows how to use Gmail SMTP to send emails from Django.

Allow less secure apps

By default, Gmail blocks less secure apps from sending emails to protect your account. We should turn this off using this link to send emails from our Gmail account.

Send email using Gmail SMTP

Create a new Django project (You might have done already) if you have not created a project.

pip istall django
django-admin startproject emailapp
cd emailapp
python manage.py startapp gmails

Open settings.py and add these lines to it.

EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'yourmail@gmail.com'
EMAIL_HOST_PASSWORD = 'gmail-password'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

These are the basic email configuration settings.

  • EMAIL_HOST: Email host or the server name. Default is localhost.
  • EMAIL_PORT: Email server port.
  • EMAIL_HOST_USER: Your username to login to the account. It will be your Email ID if you are using Gmail or Outlook.
  • EMAIL_HOST_PASSWORD: Email account or SMTP password.
  • EMAIL_USE_TLS and EMAIL_USE_SSL: Boolean values that tells whether to use an implicit TLS (secure) connection when talking to the SMTP server.

Open Views.py in gmails app folder and add this code. In this example, Iā€™m using send_mail() method, which is the easiest way to send emails using Django.

from django.shortcuts import render, HttpResponse
from django.core.mail import send_mail as sm

def send_mail(request):
    res = sm(
        subject = 'Subject here',
        message = 'Here is the message.',
        from_email = 'mail@gmail.com',
        recipient_list = ['someone@example.com'],
        fail_silently=False,
    )    

    return HttpResponse(f"Email sent to {res} members")
    #return HttpResponse("Email sent to "+ res +" members")

fail_silently expects a Boolean value. If it is set to False, the send_mail()function will raise smtplib.SMTPException if an error occurs while sending the email.

Create urls.py under gmails app folder and create the following routes.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.send_mail, name='sendmail'),
]

Open urls.py in the project folder and modify it as:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('gmails.urls')),
    path('admin/', admin.site.urls),
]

Then run the project with

python manage.py runserver

If email is sent successfully, the browser will show Email sent to 1 members.

Sometimes, we may to test email functionality in development or production environment of our application. Testing emails with real users or customers can result in bad reputation or spamming.

Here comes the use of tools like Mailtrap. It can create a fake SMTP server designed to test, view, and share emails sent from the development and staging environments without spamming real customers or flooding your own inboxes.

Happy coding šŸ‘.


Share Tweet Send
0 Comments
Loading...