如何使用nodejs发送包含来自数据库的数据的邮件?

时间:2017-09-05 10:51:08

标签: node.js

这是我的代码:我在postman中获得了完美的输出。但我希望输出通过我的邮件发送。在此代码中var结果我收到错误我无法得到回复。

var startBroadcasting = function(req,res){

authToken = req.headers.authorization;
userAuthObj = JSON.parse(UserAuthServices.userAuthTokenValidator(authToken));
var todayDate = new Date();
var expireDate = new Date(userAuthObj.expire_date);
tokenOK = TokenValidator.validateToken(userAuthObj.user_id, authToken).then(function (userSessions) {
    if (userSessions.length === 1) {
        if (expireDate >= todayDate) {
            template_id = req.params.id;
             image_id = req.params.img_id;
            TemplateController.findById(template_id, {
                attributes: {
                    exclude: ['created_by', 'created_on', 'updated_by', 'updated_on']
                },
                include: [{
                    attributes: {
                        exclude: ['created_by', 'created_on', 'updated_by', 'updated_on']
                    },
                    model: Broadcasting,

                    where: {
                        id: image_id,

                    }
                },

            ]
            }

            ).then(function (templatefindByid) {



                BccSetting.findAll({
                    where: {
                        template_id: template_id
                    }
                }).then(bccSettings => {


                    res.status(200).json({
                        id: templatefindByid.id,
                        name: templatefindByid.name,
                     template_images: templatefindByid.template_images,
                        bcc_settings: bccSettings,


})
                }).catch(err => {
                    console.log(err);
                    res.status(404).json({
                        message: ' not  found...'
                    });
                });
            }).catch(function (err) {
                console.log(err);
                res.status(404).json({
                    message: 'not  found...'
                });
            });
        } else {
            res.status(401).json({
                message: 'Not ...'
            });
        }
    } else {
        res.status(401).json({
            message: 'Expired...'
        });
    }
}).catch(function (err) {
    res.status(401).json({
        message: 'Expired...'

    });
});

var result =“

您好”+ template_images.client_name +“!

我们很高兴地通知您,”+ template_images.client_name +“newschannel流媒体已成功停止在“+ template_images.destination_type +”

“+   “”+  “此电子邮件是从通知地址发送的,无法接收收到的电子邮件。请不要回复此邮件。”;

让transporter = nodemailer.createTransport({

host: 'smtp.gmail.com',
port: 465,
auth: true,
active: true,
secure: true,
requireTLS: true,
 auth: {
     user: 'trans@gmail.com',
    pass: 'wertyy'
}

});

让mailOptions = {

from: 'trans@gmail.com',
 to: 'wwww@gmail.com',
 bcc: bcc_setting.bcc_setting,// here i want bcc setting data
subject: 'yvnuyybymyed',
html: 'result'

};

transporter.sendMail(mailOptions,(error,info)=> {

if (error) {
    return console.log("mail not sent" + error.message);
}
console.log('success');

});

};

0 个答案:

没有答案