本文介绍了无法在 protractor.conf.js onComplete 中使用 nodemailer 发送电子邮件:function()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在 protractor.conf.js onComplete: function() 中使用 nodemailer 发送电子邮件.使用了下面的代码,它不执行 onComplete 块

Unable to send Email using nodemailer in protractor.conf.js onComplete: function(). Used the below code and it does not execute onComplete block

onComplete: function() {    
    var nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // use SSL
        auth: {
            user: 'email',
            pass: 'password'
        }
    });
    var mailOptions = {
            from: '"TestMail" <>', // sender address (who sends)
            to: 'receiver's email', // list of receivers (who receives)
            subject: 'Hello through conf', // Subject line
            text: 'Hello world ', // plaintext body
            html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js', // html body


    };

        transporter.sendMail(mailOptions, function(error, info){
            if(error){
                return console.log(error);
            }

            console.log('Message sent: ' + info.response);
        });

推荐答案

你需要返回一个 Promise.只有这样 onComplete() 才会等到 Promise 得到解决 - 邮件被触发.

You need to return a Promise. Only then onComplete() would wait till the Promise is resolved - mail is triggered.

测试完成后调用的回调函数.onComplete 可以
可选地返回一个承诺,量角器将在此之前等待
关闭网络驱动程序.此时,测试将完成,但全局对象仍然可用.onComplete?: () =>无效

一旦成功触发电子邮件,您需要将函数转换为返回 Promise.请参阅此精美教程.他们有一个很好的例子来转换 fs.readFile() 以返回一个 promise

You need to convert your function to return a Promise once an email is trigerred successfully. Refer this beautiful tutorial. They have a very good example on converting a fs.readFile() to return a promise

你可以这样做.

onComplete: function() {
    return new Promise(function (fulfill, reject){
        var transporter = nodemailer.createTransport({
            host: 'smtp.gmail.com',
            port: 465,
            secure: true, // use SSL
            auth: {
                user: 'email',
                pass: 'password'
            }
        });
        var mailOptions = {
            from: '"TestMail" <>', // sender address (who sends)
            to: 'receiver's email', // list of receivers (who receives)
            subject: 'Hello through conf', // Subject line
            text: 'Hello world ', // plaintext body
            html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js', // html body
    };
        transporter.sendMail(mailOptions, function(error, info){
            if(error){
                reject(err);
            }
            fulfill(info);
        });
    });
}

这篇关于无法在 protractor.conf.js onComplete 中使用 nodemailer 发送电子邮件:function()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 02:26