Reputation: 931
SendGrid provides the ability to pass unique_args with email so that to identify the email in the Event webhook. But the problem is I am not able to figure out how to send these unique_args with the email in Django. This is how I am currently attempting to do it:
from django.core.mail import EmailMultiAlternatives
header ={
"unique_args": {
"customerAccountNumber": "55555",
"activationAttempt": "1",
"New Argument 1": "New Value 1",
"New Argument 2": "New Value 2",
"New Argument 3": "New Value 3",
"New Argument 4": "New Value 4"
}
}
subject, from_email, to = 'hello', '[email protected]', '[email protected]'
text_content = 'This is an important message.'
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to,],
headers=header,
)
msg.send()
Upvotes: 3
Views: 359
Reputation: 4156
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', '[email protected]', '[email protected]'
text_content = 'This is message.'
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to,],
headers={'X-SMTPAPI': smtp_header.json_string()},
)
msg.send()
Upvotes: 1
Reputation: 931
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', '[email protected]', '[email protected]'
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
Upvotes: 7