Setting up email functionality in Django is essential for user authentication, notifications, and transactional emails. One of the most efficient ways to handle email delivery is by using AWS Simple Email Service (SES), a scalable and cost-effective solution for sending emails.

Prerequisites

Before configuring email in Django using AWS SES, ensure the following:

  • An AWS account with SES activated in the preferred region.
  • Django installed in the project environment.
  • Verified email addresses in AWS SES for sending emails.
  • AWS credentials with the necessary permissions.

Step 1: Install Required Packages

To integrate AWS SES with Django, install boto3, Amazon’s SDK for Python:

pip install boto3

Step 2: Configure AWS SES

  1. Verify Email Address
    • Navigate to AWS SES ConsoleEmail AddressesVerify a New Email Address.
    • Follow the verification email instructions.
  2. Move Out of Sandbox Mode
    • By default, AWS SES operates in a sandbox environment, allowing emails only to verified addresses.
    • To send emails to any address, request production access via AWS Support.

Step 3: Update Django Settings

Modify the settings.py file to configure AWS SES as the email backend:

EMAIL_BACKEND = ‘django.core.mail.backends.smtp.EmailBackend’

EMAIL_HOST = ’email-smtp.<your-region>.amazonaws.com’  # Replace with AWS SES region

EMAIL_PORT = 587  # Use 465 for SSL or 25 as an alternative

EMAIL_USE_TLS = True

EMAIL_HOST_USER = ‘your-smtp-username’  # AWS SES SMTP credentials

EMAIL_HOST_PASSWORD = ‘your-smtp-password’

DEFAULT_FROM_EMAIL = ‘your-verified-email@example.com’

Step 4: Sending Emails in Django

Django’s built-in send_mail function can be used to send emails:

from django.core.mail import send_mail

def send_welcome_email(user_email):

    subject = “Welcome to Our Platform”

    message = “Thank you for signing up. We are excited to have you!”

    from_email = “your-verified-email@example.com”

    recipient_list = [user_email]

    send_mail(subject, message, from_email, recipient_list)

Step 5: Test the Configuration

Run the Django shell and execute the function:

python manage.py shell

from your_app.utils import send_welcome_email

send_welcome_email(“recipient@example.com”)

Check the AWS SES console for email logs to ensure successful delivery.

Common Issues and Troubleshooting

  • Authentication Errors: Double-check AWS SMTP credentials.
  • Email Not Delivered: Verify that AWS SES is in production mode.
  • Rate Limits Exceeded: Review AWS SES sending quotas and request an increase if necessary.

Conclusion

Using AWS SES with Django ensures scalable and cost-effective email delivery. By following the above steps, emails can be configured effortlessly, providing a seamless user experience.