mailjet / mailjet-apiv3-dotnet

[API v3] Official Mailjet API v3 .NET wrapper
https://dev.mailjet.com
MIT License
76 stars 32 forks source link

Does personalization work for email subject? #114

Closed mikalai-sauchanka closed 1 year ago

mikalai-sauchanka commented 1 year ago

So far I managed to successfully got custom variables replaced in email body, but no in subject. I pass in "to[{index}].Vars".

Screenshot 2023-06-30 at 18 55 01
dmytrosvystun88 commented 1 year ago

Hi, @mikalai-sauchanka, since you've mentioned "to[{index}].Vars" I assume that you are using V3 api to send the email.

Enable template language to be able to replace custom variables in the subject. This code works for me:

MailjetRequest request = new MailjetRequest
            {
                Resource = Send.Resource,
            }
            .Property(Send.FromEmail, "john.doe@mail.com")
            .Property(Send.FromName, "Mailjet Pilot")
            .Property(Send.Subject, "Hello, {{var:RecipientFirstName}}")
            .Property(Send.TextPart, "Hello, {{var:RecipientFirstName}}")
            .Property(Send.HtmlPart, "Hello, {{var:RecipientFirstName}}")
            .Property(Send.Vars, new JObject {
                {"RecipientFirstName", "John Doe"}
                })
            .Property(Send.Recipients, new JArray {
                new JObject {
                 {"Email", "john.doe@mail.com"},
                 {"Name", "passenger 1"},
                 {"Vars", new JObject {
                  {"RecipientFirstName", "John Doe"}
                  }}
                 },
                new JObject {
                 {"Email", "john.doe@mail.com"},
                 {"Name", "passenger 2"}
                 }
                })
            .Property(Send.MjTemplateLanguage, "True");

            MailjetResponse response = await client.PostAsync(request);
dmytrosvystun88 commented 1 year ago

Documentation: https://dev.mailjet.com/email/guides/send-api-V3/#use-template-language

mikalai-sauchanka commented 1 year ago

Hi, @dmytrosvystun88. Thank you for your response!

Apparently I was using wrong syntax - square brackets instead of curly.