在Locallhost中使用Nodemailer发送电子邮件(ReferenceError:未定义require)

时间:2017-08-15 20:40:06

标签: node.js nodemailer

我想用Nodemailer发送此表单的数据。但是当我在控制台上单击浏览器中的enviar按钮(locallhost)时,我收到以下错误:ReferenceError: require is not defined。我的节点版本为v7.9.0

我应该澄清,当我从终端运行app.js(没有函数)function send () {}如果它发送邮件。

的index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Demo4</title>
    </head>
    <body>
        <h1>Formulario</h1>
        <select>
            <option>Vida</option>
            <option>Hogar</option>
            <option>Auto</option>
        </select>
        <br>
        <input type="email" value="email">
        <br><br>
        <input type="button" value="enviar" onclick="enviar()">
        
        <script src="app.js"></script>
    </body>
</html>

app.js

function enviar(){

    var nodemailer = require('nodemailer');
    let transporter = nodemailer.createTransport({
        service:'gmail',
        auth: {
            user: '****@gmail.com',
            pass: '******'
        }
    });

    let mailOptions = {
        from: '"****" <****@gmail.com>',
        to: '****@***.com',
        subject: 'Lorem ipsum',
        text: 'Lorem ipsum',
        html: '<b>Hello world ?</b>'
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message %s sent: %s', info.messageId, info.response);
    });
    
};

app.js 如果您发送邮件,则不使用函数function enviar(){}从终端进行测试。

    var nodemailer = require('nodemailer');
    let transporter = nodemailer.createTransport({
        service:'gmail',
        auth: {
            user: '****@gmail.com',
            pass: '******'
        }
    });

    let mailOptions = {
        from: '"****" <****@gmail.com>',
        to: '****@***.com',
        subject: 'Lorem ipsum',
        text: 'Lorem ipsum',
        html: '<b>Hello world ?</b>'
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message %s sent: %s', info.messageId, info.response);
    });

0 个答案:

没有答案