使用nodemailer

时间:2017-09-07 20:26:10

标签: node.js firebase firebase-realtime-database google-cloud-functions

我遇到与性能相关的问题。我有一个预订系统,在预订被放入数据库后,onWrite云功能发送电子邮件nodemailer并删除事件,以便其他人无法保留。

问题: - 我不知道如何进行确认发送电子邮件的回调 - 发送电子邮件和删除事件甚至需要一分钟,这非常慢

我不想在客户端代码中显示如何从显而易见的原因中删除数据库中的事件。这里有什么其他选择?

exports.sendEmailConfirmation = functions.database.ref('/reservations/{reservation}').onWrite(event => {

    const moment = require('moment');
    moment.locale('pl');

    admin.database().ref('/users').orderByChild('place').equalTo(event.data.val().placeId).once('child_added', function(snapshot) {     

        // create reusable transporter object using the default SMTP transport
        const transporter = nodemailer.createTransport({
            host: '(...)',      
            port: 465,
            secure: true, // secure:true for port 465, secure:false for port 587
            auth: {
                user: '(...)', // TODO move to config()
                pass: '(...)' // TODO move to config()
            }
        });

        // setup email data with unicode symbols
        const mailOptions = {       
            from: 'whatever', // sender address - TODO make it dynamically populated
            //replyTo: 'put user's email here',
            to: snapshot.val().email,
            bcc: '(...)',
            subject: 'Nowa rezerwacja', // Subject line - TODO make it dynamically populated
            text: 'Nowa rezerwacja\n'
                + "\nImię i nazwisko: " + event.data.val().name
                + "\nTelefon: " + event.data.val().number
                + "\nEmail: " + event.data.val().email
                + "\nTermin: " + moment.utc(event.data.val().start).format("D MMMM YYYY [o godzinie] HH:mm")
                + "\nDo: " + event.data.val().scheduleName, // plain text body
            html: 'Nowa rezerwacja<br>'
                + "<br>Imię i nazwisko: " + event.data.val().name
                + "<br>Telefon: " + event.data.val().number
                + "<br>Email: " + event.data.val().email
                + "<br>Termin: " + moment.utc(event.data.val().start).format("D MMMM YYYY [o godzinie] HH:mm")
                + "<br>Do: " + event.data.val().scheduleName
        };

        // send mail with defined transport object
        transporter.sendMail(mailOptions, (error, info) => { // TODO should be returned
            if (error) {
                return console.log(error);
            }

            console.log('Message %s sent: %s', info.messageId, info.response);

            //move it elsewhere - maybe HTTP trigger?
            admin.database().ref('events/' + event.data.val().eventKey).remove();   

        });

    });

});

0 个答案:

没有答案