1

i have developed REST services using node js. I'm using node mailer to send email and this is my example code:

var client = nodemailer.createTransport(sgTransport(options));

    var email = {
        from: 'noreply@website.com',
        to: user.email,
        subject: 'Registration successfully confirmed',
        text: 'Hi welcome to our group'
    };

    client.sendMail(email, function(err, json){
        if (err){
            return res.status(500).send({ msg: err.message });
        }
        else {
            res.status(200).send({status: 'ok', data: { msg: 'A verification email has been sent to ' + user.email + '.'}} )
        }
    });

Now i would like to send the mail text with translation when client send in POST request the national code. How i can store all text mail into config file and read it in my controller by config?

Thanks

dev_
  • 395
  • 1
  • 3
  • 16

1 Answers1

1

You can save it a json file and then require it from your controller.

Example emails.json file:

{
   "registration_confirmed": {
        "en": {
            "text": "Registration successfully confirmed", 
            "subject": "Hi welcome to our group" 
        },
        "it": {
            "text": "Registrazione confermata", 
            "subject": "Ciao, benvenuto nel nostro gruppo" 
        }
    }
 } 

Usage (in your controller):

const emails = require('./emails.json');

const national_code = req.body.national_code;
const email_to_send = {
    from: 'noreply@website.com',
    to: user.email,
    subject: email.registration_confirmed[national_code].subject,
    text: email.registration_confirmed[national_code].text
};

client.sendMail(email_to_send, ...)

Keep in mind that the required file path should be relative to your controller location and that an additional national code check should be added to avoid errors.

lifeisfoo
  • 15,478
  • 6
  • 74
  • 115