从网站调用Nodemailer脚本时不执行

时间:2018-06-20 00:39:26

标签: javascript html node.js forms nodemailer

我试图通过单击Web表单上的“提交”来使用Nodemailer发送电子邮件。将打印嵌入式脚本的测试字符串,但此后脚本将失败,并在控制台中显示以下消息:

ReferenceError:未定义require

有趣的是,当我直接从终端运行该脚本时,我会收到来自该脚本的电子邮件,但是当我尝试从Web表单中触发它时,会出现此错误。

我看过的几乎所有地方的人们都将脚本(例如下面的脚本)作为使用Nodemailer发送电子邮件所需的唯一内容。 Nodemailer网站和W3Schools均未引用任何其他内容。我想念什么?

感谢您的时间,

-乔尔

<script>
  const myForm = document.getElementById('myForm');
  console.log("Testing!");
  myForm.addEventListener("click", () => {
    var nodemailer = require('nodemailer');

    var transporter = nodemailer.createTransport({

	 service: 'gmail',
	 auth: {
	        user: 'angleritemailer@gmail.com',
	        pass: 'hammertime80'
	    }
	});


	const mailOptions = {
		from: 'angleritemailer@gmail.com',
		to: 'joelnashjobs@yahoo.com',
		subject: 'hey',
		text : 'text',
		html: '<p>Your html here</p>'
	}

    transporter.sendMail(mailOptions, function(err, info) {
      if (err)
        console.log(err)
      else
        console.log(info);
    });
  });
</script>

<form" class = "col-sm-6" action = "/" method = "POST" id = "myForm"> 
                        <div class = "row form-row form-group">

                            <div class = "col-sm-5">
                                <input type = "text" class = "form-control-plaintext full-width" id = "name" placeholder = "Your name here">
                            </div>

                            <div class = "col-sm-2">
                            </div>

                            <div class = "col-sm-5 widen-slightly">
                                <input type = "text" class = "form-control-plaintext full-width" id = "staticEmail" placeholder = "email@example.com">
                            </div>
                        </div>

                        <div class = "row form-row form-group">

                            <div class = "col-sm-12 widen-slightly">
                                <input type = "text" class = "form-control-plaintext full-width" id = "phone" placeholder = "Phone number with area code">
                            </div>

                        </div>

                        <div class = "row form-row form-group">

                                <div class = "col-sm-12 widen-slightly">
                                    <input type = "text" class = "form-control-plaintext full-width" id = "subject" placeholder = "Subject">
                                </div>
                        </div>

                        <div class = "row form-row form-group">

                                <div class = "col-sm-12 widen-slightly">
                                    <textarea rows = "6" class = "form-control-plaintext full-width" id = "Description" placeholder = "Description"></textarea>
                                </div>
                        </div>

                        <div class = "row form-row form-group">
                            <label  class = "col-sm-3 col-form-label" >Any Photos</label>



                            <div class = "col-sm-7">
                                <input type = "file" class = "form-control-file full-width" id = "">
                            </div>

                            <div class = "cl-sm-2"> 
                                <button type="submit" class="btn btn-default" id = "submit">Submit</button>
                            </div>
                        </div>




                    </form>

0 个答案:

没有答案