As Loops’ SMTP service requires sending an API-like email body rather than a full email, it’s not recommended to use Loops as the default SMTP service for your app in your settings file.
Instead, use a custom connection for each email request that you want to send through Loops.

Sending email from Django with Loops’ SMTP service is easy but there’s one gotcha: the email body needs to be an API-like payload.

This may seem strange at first but it allows you to use Loops’ WYSIWYG editor to craft your emails and keep email templating outside of your code repo.

We are using a custom connection for sending this email as typically only some emails in a project will be sent through Loops.

Add these settings to your project (e.g. in an .env file).

FieldValue
Hostsmtp.loops.so
Port number587
Usernameloops
PasswordAn API key copied from your API settings in Loops

Every email sent from Django over Loops SMTP requires a transactional email to be set up in your Loops account. Note the transactionalId value in the email payload.

from django.core.mail import send_mail, get_connection
import json
import os

with get_connection(
    host=os.environ['LOOPS_SMTP_HOST'],
    port=os.environ['LOOPS_SMTP_PORT'],
    username=os.environ['LOOPS_SMTP_USER'],
    password=os.environ['LOOPS_SMTP_PASSWORD'],
    use_tls=True # Has to be True
) as connection:

    email = '[email protected]'

    # This payload can be copied from a transactional email's 
    #  Publish page in Loops
    payload = {
        "transactionalId": "clomzp89u635xl30px7wrl0ri",
        "email": email,
        "dataVariables": {
            "buttonUrl": "https://myapp.com/login/",
            "userName": "Bob"
        }
    }

    send_mail(
        "Subject here", # Overwritten by Loops template
        json.dumps(payload), # Stringify the payload
        "[email protected]", # Overwritten by Loops template
        [email],
        fail_silently=False,
        connection=connection
    )

Was this page helpful?