node.js 发送SMTP邮件 封装函数



const nodemailer = require('nodemailer');

function mySend(sub,txt)
    {
        let transporter = nodemailer.createTransport({
            host: 'smtp.163.com',
            port: 465,
            secure: true, // true for 465, false for other ports
            auth: {
                user: 'XXXX@163.com', // generated ethereal user
                pass: 'XXXXXXXXXXXX' // generated ethereal password
            }
        });

        let mailOptions = {
            from: '"XXX"<XXXX@163.com>', // sender address
            to: '"XXX"<XXXX@163.com>', // list of receivers
            subject: sub, // Subject line
            text: txt, // plain text body
            //html: '<b>北京欢迎你</b>' // html body
        };

        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                return console.log(error);
            }
            console.log('Message sent: %s', info.messageId);

        });
    }


module.exports ={ mySend};