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', 'EXAMPLE@FROM.com', 'EXAMPLE@TO.NET'
text_content = 'This is an important message.'
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to,],
headers=header,
)
msg.send()
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
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.