Get API result when sending SparkPost email from Django?

119 views Asked by At

If I send a message via the SparkPost python API directly, it returns a result that looks like this:

{'total_rejected_recipients': 0, 'total_accepted_recipients': 1, 'id': '1234567890'}

Django has built-in integration support for sending mail via sparkpost, if you set MAIL_BACKEND in settings.py:

EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend'
SPARKPOST_API_KEY = 'something'

Does Django offer a way to access the API result when using the standard Django mail methods? (the send_mail() function or the EmailMessage class)

1

There are 1 answers

0
Hernán Alarcón On BEST ANSWER

Does Django offer a way to access the API result when using the standard Django mail methods? (the send_mail() function or the EmailMessage class)

It looks like it doesn't.

The source code shows that SparkPostEmailBackend.send_messages() obtains a response but does not store it anywhere:

def send_messages(self, email_messages):
    """
    Send emails, returns integer representing number of successful emails
    """
    success = 0
    for message in email_messages:
        try:
            response = self._send(SparkPostMessage(message))
            success += response['total_accepted_recipients']
        except Exception:
            if not self.fail_silently:
                raise
    return success

Maybe you can create your custom email backend that extends from SparkPostEmailBackend and overrides the send_messages().