Nodemailer发送相同的模板.hbs

时间:2020-03-29 22:26:44

标签: node.js handlebars.js nodemailer

我在使用Nodemailer和Handlebars时遇到问题,当在Firebase上触发我的功能时,Nodemailer发送相同的电子邮件,我已经指定了当band处于特定值时的路径,例如,我的后端结构如下:

+functions
  +EncontramosMotorista.js
  +send.js
  +Sendmailtomotorista.js 
+views
  +encontramosmotorista
    +index.hbs
  +imgs
  +pagacompra
    +index.hbs
  +startcompra
    +index.hbs
+index.js

send.js是发送邮件的人,是的,我所有的index.hbs都不一样。

这是send.js:

const path = require('path');
const hbs = require('nodemailer-express-handlebars');
const { encontramosMotorista } = require('./EncontramosMotorista')
const nodemailer = require('nodemailer')
const transport = nodemailer.createTransport({
    // Falta configurar esto, para crear un dominio 
    // ahorita en prueba ocupamos mi correo electronico.
    // Esto se puede convertir en un objeto.
    service: "Gmail",
    auth: {
        user: "****@gmail.com",
        pass: "*****"


    }
});


const send = async(data, admin) => {
    // band, mail, id, nombrecliente, nombreemp, mail2, prod, precio, liquidacion

    let band = data.band;
    let mail = data.correocliente;
    let id = data.id;
    let nombrecliente = data.nombrecliente;
    let nombreemp = data.nombreemp;
    let mail2 = data.correoempresa;
    let prod = data.des;
    let precio = data.preciototal;
    let liquidacion = data.precio;




    if (band === 0) {
        console.log("Compra creada.." + hbs.name);

        const handlebarOptions0 = {
            viewEngine: {
                extName: '.hbs',
                partialsDir: path.resolve(__dirname, '../views/startcompra'),
                layoutsDir: path.resolve(__dirname, '../views/startcompra/'),
                defaultLayout: ''

            },
            viewPath: path.resolve(__dirname, '../views/startcompra/'),
            extName: '.hbs',
        };

        console.log("HandlebarOptions #1", handlebarOptions0);


        return await transport.use('compile', hbs(handlebarOptions0)).sendMail({
            from: "*** <***@gmail.com>",
            to: mail,
            subject: "Orden:  #1" + id,
            template: 'index',
            context: {
                nombreCliente: nombrecliente,
                idCompra: id,
                NombreEmpresa: nombreemp,
                Total: precio
            },
            attachments: [{
                filename: 'logo.png',
                path: path.resolve(__dirname, '../views/img/logo.png'),
                cid: 'logo',

            }]


        }).then(r => {
            return r;
        }).
        catch(e => {
            console.log('ERRO:', e);
            return e;
        });

    }

    if (band === 2) {
        console.log("Compra Confirmada.." + hbs.name)
        const handlebarOptions2 = {
            viewEngine: {
                extName: '.hbs',
                partialsDir: path.resolve(__dirname, '../views/pagacompra/'),
                layoutsDir: path.resolve(__dirname, '../views/pagacompra/'),
                defaultLayout: ''

            },
            viewPath: path.resolve(__dirname, '../views/pagacompra/'),
            extName: '.hbs',
        };

        console.log("HandlebarOptions #2", handlebarOptions2);


        return await transport.use('compile', hbs(handlebarOptions2)).sendMail({
            from: "***<****@gmail.com>",
            to: mail2,

            subject: "Orden: #2  " + id,
            template: 'index',
            context: {
                nombreCliente: nombrecliente,
                idCompra: id,
                NombreEmpresa: nombreemp,
                Total: precio,
                Liquidacion: liquidacion

            },
            attachments: [

                {
                    filename: 'logo.png',
                    path: path.resolve(__dirname, '../views/img/logo.png'),
                    cid: 'logo',

                },
            ]

        }).then(r => {

            return r;
        }).
        catch(e => {
            console.log('ERRO:', e);
            return e;
        });




    }

    if (band === 3) {
        console.log("Motorista encontrado", hbs.name);

        const handlebarOptions3 = {
            viewEngine: {
                extName: '.hbs',
                partialsDir: path.resolve(__dirname, '../views/encontramosmotorista/'),
                layoutsDir: path.resolve(__dirname, '../views/encontramosmotorista/'),
                defaultLayout: ''

            },
            viewPath: path.resolve(__dirname, '../views/encontramosmotorista/'),
            extName: '.hbs',
        };

        console.log("HandlebarOptions #3", handlebarOptions3);

        return encontramosMotorista(data, admin).then(res => {
            return transport.use('compile', hbs(handlebarOptions3)).sendMail({
                from: "**** <***@gmail.com>",
                to: `${mail} , ${mail2}`,
                subject: "Orden:#3  " + id,
                template: 'index',
                context: {
                    nombreCliente: nombrecliente,
                    idCompra: id,
                    NombreEmpresa: nombreemp,
                    nombreMotorista: res.nombre,
                    placa: res.placa,
                    telefono: res.phone,

                },
                attachments: [

                    {
                        filename: 'logo.png',
                        path: path.resolve(__dirname, '../views/img/delivery.png'),
                        cid: 'logo',

                    },
                ]

            }).then(r => {
                    return r;

                }

            ).

            catch(e => {
                console.log('ERRO:', e);
                return e;
            })



        });









    }

    return false;


}

module.exports = {
    send
}




在index.js上,这是我称之为的代码的片段: 除了导入

const { send } = require('./functions/send')

exports.PreOrdenCorreo2 = functions.database.ref('/PreRoutes/{id}/{band}')
    .onUpdate((snapshot, context) => {

        const idcompra = context.params.id;
        const referencia2 = ref.child(idcompra);

        referencia2.once("value", function(snap) {


            return send(snap.val(), admin).then(res => {
                console.log("Respuesta server :", res);
                return res;
            }).catch(e => {
                console.log("Error server:", e);
                return e;
            });

        });


路径正确,但是发送的电子邮件相同。

0 个答案:

没有答案