sklarsa / django-sendgrid-v5

An implementation of Django's EmailBackend compatible with sendgrid-python v5+
MIT License
318 stars 54 forks source link

asm substitution tags for dynamic templates #127

Closed mrocheleau closed 2 months ago

mrocheleau commented 2 months ago

Hi, firstly thanks for this as we use it for a drop in backend replacement via Django!

We currently use dynamic templates and have a bunch of things we send in that dict up to the backend for a SendGrid mail send, works great of course. But now we're trying to use the asm substitution tags - specifically: <%asm_preferences_raw_url%>, which requires the following additional dynamic template keys/values in the dict (group id obfuscated):

"asm": {
    "group_id": 11111,
    "groups_to_display": [
        11111
        ]
    }

If I use the template via curl, using the above additional keys/dict it works and remaps that asm tag to the unsubscribe preferences URL. When I add this above key set/dict to the existing "dynamic_template" data where the rest of our keys are held, the asm tag doesn't get substituted on the email.

I've confirmed at the Django backend side it's got the asm key etc. and in the sendgrid send to the api itself (in sendgrid_backend.mail.send_messages function if I log the data var that it sends, it has the asm bits as well. I can't see past that (or am unsure where to look and log past that at least).

Curious if this has come up before or if there's any clearly wonky with just adding in the asm required key (technically only group_id is required)...we do get the resulting mail just without the substituted link, so it seems to be accepted by the API...

sklarsa commented 2 months ago

Thanks! The backend just sends a raw HTTP call to the sendgrid/twilio servers, not much magic really. Sounds like you’re looking in the right place. I would try to curl the api directly and see if you get the intended result. If so, double check that data with the data in python and hopefully you’ll spot the difference.  Most of these problems have to do with strange behavior on their end On Jul 5, 2024, at 2:14 PM, mrocheleau @.***> wrote: Hi, firstly thanks for this as we use it for a drop in backend replacement via Django! We currently use dynamic templates and have a bunch of things we send in that dict up to the backend for a SendGrid mail send, works great of course. But now we're trying to use the asm substitution tags - specifically: <%asm_preferences_raw_url%>, which requires the following additional dynamic template keys/values in the dict (group id obfuscated): "asm": { "group_id": 11111, "groups_to_display": [ 11111 ] }

If I use the template via curl, using the above additional keys/dict it works and remaps that asm tag to the unsubscribe preferences URL. When I add this above key set/dict to the existing "dynamic_template" data where the rest of our keys are held, the asm tag doesn't get substituted on the email. I've confirmed at the Django backend side it's got the asm key etc. and in the sendgrid send to the api itself (in sendgrid_backend.mail.send_messages function if I log the data var that it sends, it has the asm bits as well. I can't see past that (or am unsure where to look and log past that at least). Curious if this has come up before or if there's any clearly wonky with just adding in the asm required key (technically only group_id is required)...we do get the resulting mail just without the substituted link, so it seems to be accepted by the API...

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

mrocheleau commented 2 months ago

Hi - thanks for the response! I managed to resolve this and it was caused by the placement of the asm dict itself.

I was addng it to the dynamic_template_data block originally along with our other dynamic stuff, but this added it on the payload to the personalizations area, where it can't exist. So the fix was defining a dict DJango setting of the asm: key's value (group_id, groups_to_display etc.) and adding that as a specific section of the message, ie "ASM_TAG" below:

    msg.template_id = settings.SENDGRID_STREAM_EMAIL_TEMPLATE_ID
    msg.asm = settings.ASM_TAG
    msg.dynamic_template_data = _create_dynamic_template_data(stuff here)
    msg.send(fail_silently=False)

This landed it properly higher up in the hierarchy and worked.