Extensions: [QUESTION] Using templates with Trigger Email extension

Created on 11 Oct 2019  路  4Comments  路  Source: firebase/extensions

Hi,

Can you provide and example of how to use templates with Trigger Email extension ?

What I'm not sure about :

  • how to format the "template" collection in firestore
  • how can I define variables in the template
  • is it possible to use logic in template (if, else, loop, ...) (you specified Handlebar so I imagine the answer is yes)
  • how to pass variables and template name when creating an email

Some code example would be great :)

Thanks a lot

Most helpful comment

Hi @NLegendre - this example may help you: https://github.com/firebase/extensions/blob/master/firestore-send-email/POSTINSTALL.md#using-templates

To summarise:

Create a new document in Firestore for your templates, for example for a template called "Following", create a new document in templates/following:

{
  subject: "@{{username}} is now following you!",
  html: "Just writing to let you know that <code>@{{username}}</code> ({{name}}) is now following you."
}

When adding a new document to your mail collection, pass in a template name with the data which will be injected:

  toUids: ['abc123'],
  template: {
    name: 'following',
    data: {
      username: 'ada',
      name: 'Ada Lovelace'
    }
  }

On the logical "if statement", it should work yes, however I'll check this myself too and make sure it is working.

All 4 comments

Hi @NLegendre - this example may help you: https://github.com/firebase/extensions/blob/master/firestore-send-email/POSTINSTALL.md#using-templates

To summarise:

Create a new document in Firestore for your templates, for example for a template called "Following", create a new document in templates/following:

{
  subject: "@{{username}} is now following you!",
  html: "Just writing to let you know that <code>@{{username}}</code> ({{name}}) is now following you."
}

When adding a new document to your mail collection, pass in a template name with the data which will be injected:

  toUids: ['abc123'],
  template: {
    name: 'following',
    data: {
      username: 'ada',
      name: 'Ada Lovelace'
    }
  }

On the logical "if statement", it should work yes, however I'll check this myself too and make sure it is working.

That's perfect !
Thank you very much !

tried sending an attachment using the array field 'attachments' in template collection. But the email got delivered with the attachment. Does anyone know if the extension is supposed to work with an attachment in the template?

For some reason my emails still cant pick up the templates. I have configured just like above templates collection is created and i have added the object.

db.collection('templates').add({
    toUids: ['abc123'],
    template: {
        name: 'following',
        data: {
            username: 'ada',
            name: 'Ada Lovelace'
        }
    }
})

When I try to add the below to mail I the this error.
image

{
        to: '[email protected]',
        message: {
             subject: 'Hi there!'
        },
        template: {
            name: 'following'
        }
    }

SOLVED: It does not look for template.name in the object you create the template document with as the documentation mislead me to think. The plugin actually looks for a matching document id for your template (i.e. template: name: 'oA3SXV3q3hU8imLqbet3'}). I hope this saves someone else 3 hours of playing around.

Was this page helpful?
0 / 5 - 0 ratings