2015-06-29 111 views
0

我一直在嘗試nodemailer。我被困。 它運行控制檯日誌,我沒有得到任何錯誤或任何東西。 req.body充滿了數據。Nodemailer沒有收到郵件

var nodemailer = require('nodemailer'); 
var transporter = nodemailer.createTransport(); 

    app.post('/contact-form', sendMail = function (req, res) { 

     transporter.sendMail({ 
      from: 'req.body.contactEmail', 
      to: '[email protected]', 
      subject: 'Message from ' + req.body.contactEmail, 
      text: req.body.contactMsg + 'my contact information: ' + req.body.contactEmail + " " + req.body.contactNummer 

     }),function(error, response) { 
      if (error) { 
       console.log(error); 
      } else { 
       console.log("Message sent: " + response.message); 
      } 
     } 
     console.log(req.body.contactMsg); 
    }); 
+0

你在哪裏設置你的傳輸參數?主機名,端口,權威性等? –

+0

我使用直接傳輸,我想我應該使用一個SMTP服務提供商... _使用直接傳輸是不可靠的,因爲使用的傳出端口25通常會被默認阻止,此外,從動態地址發送的郵件通常會被標記爲垃圾郵件。真的考慮使用SMTP提供商._ –

回答

0

使它更容易去這個,並繼續。 https://myaccount.google.com/security

向下滾動到此頁面,您將獲得允許安全性較低的應用程序:開啓,剛剛開啓,它將起作用。這裏是完整的代碼 -

var nodemailer = require('nodemailer'); var smtpTransport = require('nodemailer-smtp-transport');

//聯繫接觸的我們 app.post( '/' 接觸,功能(REQ,RES){

 var mailOpts, smtpTrans; 

     //Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems. 
     smtpTrans = nodemailer.createTransport(smtpTransport({ 
      service: 'gmail', 
      // host:'smtp.gmail.com', 
      // port:465, 
      // secure:true, 
      auth: { 
       user: "[email protected]", 
       pass: "xxxxxxx" 
      } 
     })); 
     var mailoutput = "<html>\n\ 
         <body>\n\ 
         <table>\n\ 
         <tr>\n\ 
         <td>Name: </td>" + req.body.form_name + "<td></td>\n\ 
         </tr>\n\ 
         <tr>\n\ 
         <td>Email: </td><td>" + req.body.form_email + "</td>\n\ 
         </tr>\n\ 
         <tr>\n\ 
         <td>MN: </td>" + req.body.form_phone + "<td></td>\n\ 
         </tr>\n\ 
         <tr>\n\ 
         <td>Messge: </td>" + req.body.form_message + "<td></td>\n\ 
         </tr>\n\ 
         </table></body></html>"; 
     //Mail options 
     mailOpts = { 
      to: "NameOfYourWebsite <[email protected]>", 
      subject: req.body.form_subject, 
      html: mailoutput 
     }; 

     smtpTrans.sendMail(mailOpts, function (error, res) { 
      if (error) { 
       // res.send("Email could not send due to error" +error); 
       return console.log(error); 
      } 
     }); 
     //console.log('Message sent successfully!'); 
      res.render('contact.ejs'); 
    }); 
    //console.log(query.sql); 

});