Sendgrid-nodejs: Sending transactional template discards subject specified via data object

Created on 31 Oct 2018  ·  19Comments  ·  Source: sendgrid/sendgrid-nodejs

Issue Summary

When using the sendgridMail.send function to send a Transactional Template, any subject line added to the data object which is passed sendgridMail.send (via data.subject or data.personalizations[someIndex].subject) is discarded and the subject line that is specified on the actual Transactional Template itself is used.

The behaviour that I expect is that the fields specified on the data object would override any existing template fields, but this does not seem to be the case. Looking at the documentation for the v3 Mail Send API, the template_id field's description states:

The id of a template that you would like to use. If you use a template that contains a subject and content (either text or html), you do not need to specify those at the personalizations nor message level.

To me, "you do not need to specify those at the personalizations nor message level" implies that you can override the template subject. If that is not the case, the description should be changed to "you cannot specify those at the personalizations nor message level".

Example

  • I created a transactional template with the subject "My Email Subject"
  • I tried to send an email using sendgridMail.send where I specified data.subject to be "[TEST] My Email Subject"
  • The email I received had the subject line "My Email Subject"

Steps to Reproduce

  1. Create a transactional template on https://sendgrid.com that has a subject specified
  2. Use the sendgridMail.send function to send the template. Also make sure to specify either data.subject or data.personalizations[someIndex].subject.
  3. Confirm that email is received with the original subject line, not the one passed to the API

Technical details:

  • sendgrid-nodejs Version: 6.3.1
  • Node.js Version: v8.11.3
non-library issue

Most helpful comment

Doh! I found where the {{subject}} needs to be set. In case anyone else has this issue, go in to edit your template, then click "Settings" on the left. There will be a space to enter your Subject. Enter {{subject}} here.

All 19 comments

Hello @10kc-zack,

I believe in the transactional template you would set the subject to be {{ subject }}, then in your code you set data.subject to be "[TEST] My Email Subject".

Does that work for you?

With Best Regards,

Elmer

Thanks for the reply @thinkingserious and your suggestion!

While this does get the job done (and is the work-around we are currently using), I would much prefer being able to set the subject as part of our API call body. Are you able to confirm that it is in fact the case that the subject provided to the API is discarded and that that information is missing from the API documentation?

Hello @10kc-zack,

I believe in the transactional template you would set the subject to be {{ subject }}, then in your code you set data.subject to be "[TEST] My Email Subject".

Does that work for you?

With Best Regards,

Elmer

As @10kc-zack stated this workaround does work but I've also noticed the template is converting some characters such as ' apostrophy.. ie "Someone's" to html entities. My email arrives with the subject Someone's

I'm having trouble setting the subject line using the SendMail Nuget package methods. Here's my code, which I've tried to update using info above:

    public async Task SendAsyncTemplate(string FromAddress, string FromName, string ToAddress, string ToName, string Subject, string LinkText, string LinkUrl)
    {
        _logger.LogInformation($"Sending mail to: {ToAddress}, with subject: {Subject}");

        var apiKey = _sendGridOptions.SendGridAPI;
        var client = new SendGridClient(apiKey);

        var from = new EmailAddress(FromAddress, FromName);
        var to = new EmailAddress(ToAddress, ToName);
        var msg = new SendGridMessage();
        var templateData = new ExampleTemplateData
        {
            Subject = Subject,
            Name = ToName,
            LinkText = LinkText,
            LinkUrl = LinkUrl
        };
        msg.Subject = "{{subject}}";
        msg.SetFrom(from);
        msg.AddTo(to);
        msg.AddCc(from);
        msg.SetTemplateId("supersecret");
        msg.SetTemplateData(templateData);

        var response = await client.SendEmailAsync(msg);

        _logger.LogInformation($"SendGrid Response Status Code: {response.StatusCode}");
    }
    private class ExampleTemplateData
    {
        [JsonProperty("subject")]
        public string Subject { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("linkText")]
        public string LinkText { get; set; }
        [JsonProperty("linkUrl")]
        public string LinkUrl { get; set; }
    }

I've run in debugging mode and values are getting set properly. What I'm getting as a subject line is a hyphen. Any ideas?

Doh! I found where the {{subject}} needs to be set. In case anyone else has this issue, go in to edit your template, then click "Settings" on the left. There will be a space to enter your Subject. Enter {{subject}} here.

@smschick,

In your case, you would do {{{ subject }}}.

@10kc-zack,

I have a request open to make that change the documentation. I'm assuming you are referring to this documentation, correct?

With Best Regards,

Elmer

@thinkingserious That is indeed the documentation I was referring to :+1:

are there any plans to be able to set the subject via the API without using dynamic_template_data?

Yes, it's rather confusing that you can't pass the subject via personalizations when using dynamic template data, you have to pass it in the data it seems.

@mou01 Simple example here for how to set the subject: https://github.com/sendgrid/sendgrid-nodejs/blob/master/docs/use-cases/single-email-single-recipient.md

does this work with dynamic templates?

I think not, I think the idea behind the dynamic templates is that you set the subject in the template set up, and you get to use dynamic data in the subject. The trade off is that you have to pass it in dynamic data if you want it to be fully flexible.

if anyone having this issue:
go to your template and click the "Settings" with a gear icon and put this on the subject field {{subject}}
and finally when sending your payload "dynamic_template_data" should have the subject value.
"dynamic_template_data":{ "subject": "My Subject Value here" }

Closing this issue as the docs change is not something that can be fixed in this repo. I've submitted a request to the docs team (reference title: _Updates to mail-send template_id description_) and will circle back here with any updates.

Docs have been updated.

Thank you @childish-sambino

The triple brackets is still working, is that still the official solution?
Thanks!

image

@tcukanoot Yes.

Was this page helpful?
0 / 5 - 0 ratings