Transactional

Django Integration

Send emails from Django applications using Transactional. Drop-in replacement for Django's email backend.

10 minutes
Beginner

Prerequisites

  • Python 3.8+ installed
  • Django 4.0+ project
  • Transactional account with API key
QUICK START

Get Started in Minutes

1

Install the SDK

Add Transactional to your Django project.

pip install usetransactional[django]
2

Configure settings

Update your Django settings to use Transactional.

3

Send emails

Use Django's standard email functions.

CODE EXAMPLES

Full Implementation

Django Settings

settings.py
# Email configuration
EMAIL_BACKEND = 'usetransactional.django.EmailBackend'
TRANSACTIONAL_API_KEY = os.environ['TRANSACTIONAL_API_KEY']

# Default sender
DEFAULT_FROM_EMAIL = 'hello@yourdomain.com'

Send Email

views.py
from django.core.mail import send_mail

def contact_view(request):
    if request.method == 'POST':
        send_mail(
            subject='Contact Form Submission',
            message=request.POST['message'],
            from_email=None,  # Uses DEFAULT_FROM_EMAIL
            recipient_list=[request.POST['email']],
            html_message='<p>{}</p>'.format(request.POST['message']),
        )
        return redirect('thank-you')
    return render(request, 'contact.html')

Send Template Email

emails.py
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string

def send_welcome_email(user):
    subject = f'Welcome to our platform, {user.first_name}!'

    # Render templates
    text_content = render_to_string('emails/welcome.txt', {'user': user})
    html_content = render_to_string('emails/welcome.html', {'user': user})

    # Create email
    email = EmailMultiAlternatives(
        subject=subject,
        body=text_content,
        from_email='hello@yourdomain.com',
        to=[user.email],
    )
    email.attach_alternative(html_content, 'text/html')
    email.send()

Django Email Backend

The Transactional Django backend is a drop-in replacement for Django's SMTP backend. All existing code using send_mail(), EmailMessage, or EmailMultiAlternatives works without changes.

Advanced Configuration

# settings.py
 
# Required
EMAIL_BACKEND = 'usetransactional.django.EmailBackend'
TRANSACTIONAL_API_KEY = os.environ['TRANSACTIONAL_API_KEY']
 
# Optional
TRANSACTIONAL_TRACK_OPENS = True
TRANSACTIONAL_TRACK_CLICKS = True
TRANSACTIONAL_SANDBOX = False  # Set True for testing

Sending with Attachments

from django.core.mail import EmailMessage
 
email = EmailMessage(
    subject='Your Report',
    body='Please find the attached report.',
    from_email='reports@yourdomain.com',
    to=['user@example.com'],
)
 
# Attach a file from disk
email.attach_file('/path/to/report.pdf')
 
# Or attach content directly
email.attach('data.csv', csv_content, 'text/csv')
 
email.send()

Async Support with Django 4.2+

For async views, use the async email functions:

from django.core.mail import send_mail
from asgiref.sync import sync_to_async
 
async def async_view(request):
    await sync_to_async(send_mail)(
        subject='Async Email',
        message='Sent from an async view!',
        from_email='hello@yourdomain.com',
        recipient_list=['user@example.com'],
    )
    return JsonResponse({'status': 'sent'})

Testing Emails

In development, use sandbox mode to prevent sending real emails:

# settings_dev.py
TRANSACTIONAL_SANDBOX = True

Or use Django's console backend for local development:

# settings_local.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Ready to Get Started?

Create your free account and start sending emails today.