Firebase Functions Typescript - Sending Emails with html templates

I am trying to read html file with fs.readFile('../emailTemplates/template.html') from index.ts

my folders :

--src
  --index.ts
  --UsersFunctions
    --UserFunctions.ts
  --emailTemplates
    --template.html

from index.ts :

import * as UsersFunctions from './UsersFunctions/UsersFunctions';
export const newUserRegister = UsersFunctions.onCreateNewUser;

from UsersFunctions.ts

 export const onCreateNewUser = functions.firestore.document('users/allUsers/usersData/{userId}').onCreate(async (snap, context) => {
  fs.readFile("../emailTemplates/template.html", {encoding: 'utf-8'}, (err, html) => {
    if (err) {
        console.log(err);

    }
    else {

    }
  });       
 }

i am getting an error from firebase functions :

Error: ENOENT: no such file or directory, open '../emailTemplates/template.html'
at Error (native)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'user_code/lib/emailTemplates/newUserRegisterHtmlTemplate.html'

I just want to send an email with html template maybe there is another way to do it ? i tired to search but found nothing regarding email templates with frirebase functions with typescript

1 answer

  • answered 2018-06-23 16:40 Doug Stevenson

    The problem is that your path to the template doesn't match your project files organization. I suggest that you only put TypeScript source code in src and put all other files you want to read and write under your functions folder at the same level as src. So, you could organize your files like this:

    /functions
        /src
            /index.ts
            /other source files
        /emailTemplates
            /template.html
    

    Then, in index.ts, you could read template.html using the same path ../emailTemplates/template.html.