Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a stored template and also specify reply_to, from.name and from.email?

Here it is explained how to specify these properties when sending inline content, but when sending a stored template it is said that they are forbidden.

I tried to send an email by specifying the ID of the template and also the forbidden properties: reply_to, from.name and from.email, and the forbidden properties were ignored.

I see setting the Reply-To header as something normal, not unusual, when sending a transactional email, and I find it strange that I cannot do this when sending a stored template. Setting the From name and email from code and not from the SparkPost template editor also seems a good functionality to have.

My code looks like this (it uses the SparkPost NodeJS API and the emails with their substitution data are successfully sent with this code so the problem is not in the substitution_data, recipients or in the callback parts of this code):

client.transmissions.send({
    transmissionBody: {
        content: {
            template_id: 'my-first-email',
            reply_to: '[email protected]', // example email address
            from: {
                name: 'My Name',
                email: '[email protected]'
            }
        },
        substitution_data: { /* ... */ },
        recipients: [ /* ... */ ]
    }
}, function (err, res) { /* ... */ });

Update: I found this question in the SparkPost Support Center but it does not help me.

Update 2: I also found this support question which may help a little but I still need a way to set the Reply-To header and I am not sure yet if the From email address (not the From name, about which I am sure from the linked article that it can do this) can use dynamic substitution data.

Update 3: I sent an email to SparkPost Support and received the following answer:

The Product Manager relayed that we do not have a time frame of when this feature will be in the product. Please keep an eye on our website and slack channel for updates.

As I have tested and accepted an answer for this question, I think they did not understand me well. But it is a happy end, after all.

like image 642
silviubogan Avatar asked Apr 15 '16 15:04

silviubogan


2 Answers

If you specify template_id, you can't specify any other options in the content object. So if you want to customize the template, you'll need to add substitution variables in your template.

One thing to note is that if you use substitution variables in your From: header, that will mean you cannot edit that template using the UI, since there is a hard requirement (in the UI) that you use a verified sending domain.

like image 127
Dave Gray Avatar answered Oct 16 '22 17:10

Dave Gray


As Dave Gray mentions above, you can use substitution variables in your template to set custom 'From name', 'From email' and 'Reply-To' fields.

Here's an example template showing how that looks:

{
  "content": {
    "from": {
      "name": "{{fromName}}",
      "email": "{{fromEmail}}"
    },
    "subject": "{{subject}}",
    "html": "Hi! I am an HTML part.",
    "text": "I am a text part.",
    "reply_to": "{{replyTo}}"
  }
}

As Dave also points out, you'll need to use the API to update your stored template with these fields. Here's a gist with some JS to do that.

Then you can set fromName, fromEmail and replyTo in your transmission:

{
  "recipients": [
    "..."
  ],
  "content": {
    "template_id": "your-dynamic-template"
  },
  "substitution_data": {
    "fromName": "Your Name",
    "fromEmail": "[email protected]",
    "replyTo": "[email protected]"
  }
}
like image 24
Ewan Dennis Avatar answered Oct 16 '22 18:10

Ewan Dennis