[Django]-Django SendGrid how to pass unique_args in EmailMultiAlternatives mail object

7๐Ÿ‘

โœ…

ok, So finally I found the solution.
Finally, I figured out how to send unique_args to SendGrid with Django EmailMultiAlternatives. Here is my working solution:

    from django.core.mail import EmailMultiAlternatives
    from smtpapi import SMTPAPIHeader
    header = SMTPAPIHeader()
    header.set_unique_args({'customerAccountNumber': '12345','activationAttempt': '1'})

    subject, from_email, to = 'hello', 'EXAMPLE@FROM.com', 'EXAMPLE@TO.NET'
    text_content = 'This is an important message.'
    msg = EmailMultiAlternatives(
        subject,
        text_content,
        from_email,
        [to,],
        headers={'X-SMTPAPI': header.json_string()},
    )
    msg.send()

You also need to install a smtpapi package by pip install smtpapi

1๐Ÿ‘

    from django.core.mail import EmailMultiAlternatives
    from smtpapi import SMTPAPIHeader
    smtp_header = SMTPAPIHeader()
    smtp_header.set_unique_args({'customerAccountNumber': '12345','activationAttempt': '1'})

    subject, from_email, to = 'hello', 'hello@FROM.com', 'AABC@TO.NET'
    text_content = 'This is message.'
    msg = EmailMultiAlternatives(
        subject,
        text_content,
        from_email,
        [to,],
        headers={'X-SMTPAPI': smtp_header.json_string()},
    )
    msg.send()

Leave a comment