extensions icon indicating copy to clipboard operation
extensions copied to clipboard

[QUESTION] Using templates with Trigger Email extension

Open NLegendre opened this issue 7 years ago • 4 comments

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

NLegendre avatar Oct 11 '19 09:10 NLegendre

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.

Ehesp avatar Oct 14 '19 12:10 Ehesp

That's perfect ! Thank you very much !

NLegendre avatar Oct 14 '19 12:10 NLegendre

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?

thebeingapp avatar May 05 '20 13:05 thebeingapp

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.

tylerreece22 avatar Jul 29 '20 02:07 tylerreece22