Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass local values to email template use Express-mailer

I want to send a thank you email after user sign up but i don't know how to pass username to my email template. I have the following code:

app.mailer.send('email', {
  to: '[email protected]', // REQUIRED. This can be a comma delimited string just like a normal email to field. 
  subject: 'Test Email', // REQUIRED.
  user: req.body.user, // All additional properties are also passed to the template as local variables.
}, function (err) {
  if (err) {
    // handle error
    console.log(err);
    res.send('There was an error sending the email');
    return;
  }
  res.send('Email Sent');
});

email template:

<html>
<head>
    <title>{{subject}}</title>
</head>
<body>
    Thank you {{user}}
</body>
</html>
like image 843
Khang Nguyen Avatar asked Oct 30 '22 18:10

Khang Nguyen


1 Answers

You can pass properties to the request.

{ to: '[email protected]', // REQUIRED. This can be a comma delimited string just like a normal email to field. subject: 'Test Email', // REQUIRED. username: req.body.username, myVar: 'username' }

The variable myVar is passed as local variable to the template

like image 147
Evers Avatar answered Nov 15 '22 05:11

Evers