簡體   English   中英

如何使用 Nodemailer 從表單發送電子郵件

[英]How to send email from form using Nodemailer

讓我們先說我是 node 和 javascript 的初學者。 我有一個使用 HTML、CSS 和 ASP.NET 構建的網站。 我的網站上有一個電子郵件表單設置,我想將其從 ASP.NET 轉換為 Node.js 只是為了學習體驗。 我遵循了幾個不同的教程來嘗試讓這篇文章發揮作用。 我一直做空!

我已經成功地使我的 html 電子郵件表單接受數據並且節點接收了它。 然而,嘗試獲取節點並使用 nodemailer 以電子郵件的形式發送它,並不是那么多。 我注意到有幾個教程使用了“路由”,我想避免使用這個選項,因為它讓我難以理解。 如果有人可以解釋如何做到這一點,或者為什么不這樣做,或者為什么不能這樣做? 我將不勝感激。 這是我到目前為止的代碼:

Javascript/節點:

var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');
var express = require('express');
var app = express();

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

app.post('/contact', function(req, res) {
    var mailOpts, smtpConfig;
    //Setup Nodemailer transport, I chose gmail. Create an application-specific             password to avoid problems.
    smtpConfig = nodemailer.createTransport('SMTP', {
        service: 'Gmail',
        auth: {
            user: "<myUser>",
            pass: "<myPassword>"
        }
    });
    //Mail options
    mailOpts = {
        from: req.query.name + ' &lt;' + req.query.email + '&gt;',
        //grab form    data from the request body object
        to: '<other user>',
        subject: 'Website contact form',
        text: req.query.message
    };
    smtpConfig.sendMail(mailOpts, function(error, response) {
        //Email not sent
        if (error) {
            res.end("Email send failed");
            //res.render('contact', { title: 'Raging Flame Laboratory - Contact',   msg: 'Error occured, message not sent.', err: true, page: 'contact' })
            //console.log("error");
        }//Yay!! Email sent
        else {
            res.end("Email send successfully");
            //res.render('contact', { title: 'Raging Flame Laboratory - Contact',  msg: 'Message sent! Thank you.', err: false, page: 'contact' })
            //console.log("success");
        }
    });
});

app.listen(8081, function() {
    console.log('Server running at http://127.0.0.1:8081/');
});

HTML:

<form action="http://127.0.0.1:8087/contact" method="post">
    <b>send us a quote</b>
    </br>
    <input type="text" name="name" id="name" value="Name">
    </br>
    <!--input type="text" name="bname" id="bname" value="Business Name"></br>-->
    <input type="text" name="email" id="email" value="Email Address">
    </br>
    <textarea name="message" id="message" cols="30" rows="10">Enter detailed information here</textarea>
    </br>
    <input type="submit" name="Submit" id="Submit" value="send message">
</form>

你的要求有誤。 應該由 req.body 而不是 req.query

嘗試添加

app.use(bodyParser.json());

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

麥克風

var nodemailer = require('nodemailer');

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
    service: 'Hotmail',
    auth: {
        user: "username",
        pass: "password"
    }
  });

console.log('SMTP Configured');

// Message object
 var message = {

// sender info
from: from,

// Comma separated list of recipients
to: req.query.to ,

// Subject of the message
subject:req.query.subject //'Nodemailer is unicode friendly ✔', 

// plaintext body
text: req.query.text //'Hello to myself!',

// HTML body
 /*  html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
     '<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'*/
};

console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
  console.log('Error occured');
  console.log(error.message);
  return;
}
 console.log('Message sent successfully!');

 // if you don't want to use this transport object anymore,uncomment    
 //transport.close(); // close the connection pool
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM