Send email using Amazon SES in Django
How To Django Python

Send email using Amazon SES in Django

Mishel Shaji
Mishel Shaji

To send Email using Amazon SES in Django, first we need to create SES SMTP credentials and then configure our Django project to use Amazon SES.

Create Amazon SES SMTP Credentials

To send mail using Amazon SES, we need to know our Amazon SES SMTP Credentials.

Follow these steps to create SMTP credentials.

  1. Verify a new domain in Amazon SES.
  2. Verify email address from which you are sending emails from.
  3. Sign in to the AWS Management Console and open the Amazon SES console at https://console.aws.amazon.com/ses/.
  4. In the navigation pane on the left side, choose SMTP Settings.
  5. Click Create My SMTP Credentials.
  6. Type a name for SMTP user of leave it as it is and click Create.
  7. Choose Show User SMTP Credentials to view your SMTP credentials. Store them in a safe place. You can also choose Download Credentials.
  8. Close the window.
Create Amazon SES SMTP Credentials

For more Information, check this link.

Obtaining Your Amazon SES SMTP Credentials - Amazon Simple Email Service
Get your Amazon SES SMTP user name and password so you can access the Amazon SES SMTP interface.

Send email from Django app

Now, create a new Django project (You might have done already) with these commands if you have not created a project.

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

Open settings.py and add these lines to it.

EMAIL_USE_TLS = True
EMAIL_HOST = 'amazon-server-name'
EMAIL_HOST_USER = 'amazon-ses-username-from-step-7'
EMAIL_HOST_PASSWORD = 'amazon-ses-password-from-step-7'

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 ses 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, to send emails from Amazon SES.

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 = 'verified-ses-email-from-step-2',
        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 ses 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('ses.urls')),
    path('admin/', admin.site.urls),
]

Then run the project with

python manage.py runserver

If an 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 👍.