sklarsa / django-sendgrid-v5

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

How to use custom headers or unique args for email tracking? #116

Closed hernantz closed 1 year ago

hernantz commented 1 year ago

With the smtp backend, the X-SMTPAPI header was used to inject custom tracking metadata for each email. The web api uses a different method for this.

Here it is stated that a headers key can be used But here and here it is stated that the unique_args key should be used.

What would be the way to do it with django-sendgrid-v5?

sklarsa commented 1 year ago

That first article is from 2013, so there's a good chance that it's outdated.

If you want to use the unique_args, it looks like you first need to upgrade your API version to v3, and then use the official python client to send the args to their server. It looks like twilio is now actively developing the original sendgrid repo, so I'd suggest just using that in this case.

hernantz commented 1 year ago

Leaving this for future reference. Both custom_args and unique_args serve the same purpose: https://docs.sendgrid.com/for-developers/tracking-events/event#unique-arguments-and-custom-arguments

This snippet works fine and serves the same use case as unique_args:

from django.core.mail import EmailMessage

msg = EmailMessage(
  from_email='to@example.com',
  to=['to@example.com'],
  cc=['cc@example.com'],
  bcc=['bcc@example.com'],
)

msg.custom_args = {'arg1': 'value1', 'arg2': 'value2'}