使用NodeMailer的工作联系表

时间:2018-08-22 15:16:29

标签: javascript html node.js nodemailer

我正在尝试使用NodeMailer为我的网站创建一个有效的联系表。这是我尝试使用的代码:

app.use(bodyParser.urlencoded({extended: true}))


app.post('/contact', function (req, res) {
 let mailOpts, smtpTrans;
 smtpTrans = nodemailer.createTransport({
   host: 'smtp.gmail.com',
   port: 465,
   secure: true,
auth: {
  user: "...",
  pass: "..."
}, tls: {
  rejectUnauthorized: false
}
});
mailOpts = {
   from: req.body.name + ' <' + req.body.email + '>',
   to: "...",
   subject: 'New message from: ',
   text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
};
 smtpTrans.sendMail(mailOpts, function (error, response) {
   if (error) {
     console.log('contact-failure');
}
   else {
     console.log('contact-success');
}
});
}).listen(5500, function(){
    console.log("listening")
});

我正在使用一个名为live server的Visual Studio扩展,该扩展在端口5500上显示页面,这就是为什么我也使服务器在该端口上进行侦听。

我的HTML代码:

 <form  action ="/contact" method='POST' class='contact-form commentsblock'>
                    <div class="form-group">
                        <label for="name">Name *</label>

                        <input
                            id="name"
                            type="text"
                            name="name"
                            class="form-control"
                            placeholder="Please enter your full name *"
                            required="required"
                            data-error="Name is required."
                            autocorrect="off"
                               autocapitalize="words"/>

                        <div class="help-block with-errors"></div>
                    </div>

                    <div class="form-group">
                        <label for="email">Email *</label>

                        <input
                            id="email"
                            type="email"
                            name="email"
                            class="form-control"
                            placeholder="Please enter your email *"
                            required="required"
                            data-error="Valid email is required."
                            autocorrect="off"
                            autocapitalize="words">


                        <div class="help-block with-errors"></div>
                    </div>

                    <div class="form-group">
                        <label for="subject">Subject *</label>

                        <select
                            id="subject"
                            name="need"
                            class="form-control"
                            required="required"
                            data-error="Please specify the subject."
                        >
                            <option value=""></option>

                            <option value="Membership Information">Membership Information</option>

                            <option value="Event / ServiceInformation">Event / ServiceInformation</option>



                            <option value="Other">Other</option>
                        </select>

                        <div class="help-block with-errors"></div>
                    </div>

                    <div class="form-group">
                        <label for="message">Message *</label>

                        <textarea
                            id="message"
                            name="message"
                            class="form-control"
                            placeholder="Message for us *"
                            rows="4"
                            required="required"
                            data-error="Please, leave us a message."
                        ></textarea>

                        <div class="help-block with-errors"></div>
                    </div>

                    <input
                        type="submit"
                        class="btn btn-success btn-send sendbtn"
                        id="bton"
                        value="Send message"
                    />

                </form>

当我运行服务器时,加载页面,然后填写表格并按Submit,我收到405错误。我对使用Node和Express的经验很少,因此可以提供任何帮助!

0 个答案:

没有答案