我在任何地方都找不到指导我在Angular4中设置nodemailer的教程。我甚至不确定将nodeemailer简介中的ts放在他们的网站上:

'use strict';
const nodemailer = require('nodemailer');

// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {

    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: 'smtp.ethereal.email',
        port: 587,
        secure: false, // true for 465, false for other ports
        auth: {
            user: account.user, // generated ethereal user
            pass: account.pass  // generated ethereal password
        }
    });

    // setup email data with unicode symbols
    let mailOptions = {
        from: '"Fred Foo 👻" <[email protected]>', // sender address
        to: '[email protected], [email protected]', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world?', // plain text body
        html: '<b>Hello world?</b>' // html body
    };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);
        // Preview only available when sending through an Ethereal account
        console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

        // Message sent: <[email protected]>
        // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
    });
});

这是一件复杂的事情,任何帮助都将不胜感激!如果我知道把代码放在哪里以及应该做什么,我就能把一些东西拼凑起来。

最佳答案

您混淆了客户端和服务器端代码。nodeemailer是express的服务器端包。Angular4将调用一个API来执行该代码。
如果您想让nodeemailer启动并运行,您需要在node中这样做。
你用的是快车这样的后端吗?

关于node.js - 如何使用Angular 4设置Nodemailer?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46099702/

10-13 08:03