How to include this nodemailer function inside this controller function?

后端 未结 4 1308
-上瘾入骨i
-上瘾入骨i 2021-01-28 11:55

I am trying to create a form that will send data to the Mongo DB first then will send that data through the email by Nodemailer. Here are the 2 functions:

contro

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 12:27

    Create separate mail.js or anyname.js

    var config  = require('../config/config.js');
    var nodemailer = require('nodemailer');
    
    var smtpTransport = nodemailer.createTransport({
        service :"gmail",
        host: "smtp.gmail.com",
        auth :
        {
            user: config.email,
            pass: config.password
        }
    });
    
    
    // setup email data with unicode symbols
    var mailOptions = {
        from: config.email,
        to: 'user to send',
        subject :'message',
        text :' "Hi",\n You have successfully created an account"',
        html: 'Welcome?' // html body
    };
    
    // sends mail
    module.exports.sendMail  = function()
    {
     // send mail with defined transport object
     smtpTransport.sendMail(mailOptions, (error, info) => {
        if (error)
        {
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);});
    }
    

    now import this file in controller js file

    var mailer = require('./mail.js');
    

    and use it like below

    mailer.sendMail()
    

    you can pass values or params inside sendMail function and access them in mail.js file to create custom message or title or name any

提交回复
热议问题