Pass variable to html template in nodemailer

前端 未结 8 2368
礼貌的吻别
礼貌的吻别 2021-01-30 16:45

I want to send email with nodemailer using html template. In that template I need to inject some dynamically some variables and I really can\'t do that. My code:



        
8条回答
  •  你的背包
    2021-01-30 17:42

    String replace isn't a good idea because you'll have to restore old strings or create a backup file to be able to change them another time, also it won't be asynchrone and it will cause a problem in every way! you can do it much easier and more cleaner:

    just go to your mail options and add context with your variables:

    var mailOptions = {
      from: 'nginx-iwnl@gmail.com',
      to: 'username@gmail.com',
      subject: 'Sending email',
      template: 'yourTemplate',
      context: {                  // <=
        username: username,
        whatever: variable
      }
    };
    

    next thing to do is openning your html file and call your variables like:

    {{username}}

提交回复
热议问题