我无法通过电子邮件发送电子邮件

时间:2019-04-03 11:30:04

标签: javascript node.js

我无法发送电子邮件至。注册时,必须向邮件发送一封信件以确认。

我收到一个错误:Callback must be a function at maybeCallback

            const fs = require('fs');
            const path = require('path');

            let path_file = path.join(__dirname, '/views/email_templates/confirm_email.html');
            let html_file = await fs.readFile(path_file, { encoding: 'utf-8' });
            let html = await html_file(path_file);
            let template = handlebars.compile(html);
            let replacements = {
                target_link: link,
                password: merchant_password,
            };
            let htmlToSend = template(replacements);
            let mailOptions = {
                from: config.transport.user,
                to : merchant_email,
                subject : 'Please confirm your Email account and login password',
                html : htmlToSend,
            };
            smtpTransport.sendMail(mailOptions);

我怎么了?

(node:14300) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
    at maybeCallback (fs.js:128:9)
    at Object.readFile (fs.js:277:14)
    at readFile (C:\Users\User\Documents\backend\src\controllers/merchants.js:299:38)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:14300) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14300) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

2 个答案:

答案 0 :(得分:1)

错误非常明显。您必须通过回调函数来发送方法

smtpTransport.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log('error occurred while sending');
            console.log(error);
            return;
        }
        console.log('Message sent:');
});

答案 1 :(得分:1)

fs.readFileSync(...)

尝试使用同步方式读取文件。