2017-09-08 5 views
0

ノードエクスプレスアプリを使用して連絡先フォームを設定する際に問題が発生しました。私はすでにNodemailerをインストールしていますが、以前はNodemailerを使用していませんでした。ここで私は..................ノードを使用してHTML連絡フォームを送信するにはどうすればよいですか?

app.js

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


app.use(express.static(process.cwd() + '/public')); 
app.use(bodyParser.urlencoded({extended: true})); 

app.get('/', function(req, res){ 
    res.send(fs.readFileSync('./views/index.html', 'utf8')); 

}); 

app.post('/', function(req, res) { 
    if(req.body.mail == "" || req.body.subject == "") { 
     res.send("Error: Email & Subject should not be Blank"); 
     return false; 
    } 

var smtpTransport = nodemailer.createTransport("SMTP", { 
    host: "smtp.gmail.com", 
    secureConnection: true, 
    port: 465, 
     auth: { 
      user: '', 
      pass: '' 
     } 
}); 

var mailOptions = { 
    from: "Node Emailer - <[email protected]>", 
    to: req.body.email, 
    subject: req.body.subject + " -", 
    html: "<b>"+req.body.description+"<b>" 
} 
smtpTransport.sendMail(mailOptions, function(error, response) { 
    if (error) { 
     res.send("Email could not be sent due to error:" +error); 
    }else { 
     res.send("Email has been sent successfully"); 
    } 
}); 
}); 

app.listen(process.env.PORT || 3000, function() { 
    console.log("LISTENING!"); 
}); 

お問い合わせフォーム

<form action='/' method='post' class='contact-form commentsblock'> 

    <div> 
     <label for='g52-name' class='grunion-field-label name'>Name<span> 
     (required)</span></label> 

     <input type='text' name='g52-name' id='g52-name' value='' 
     class='name' 
     required aria-required='true'/> 

    </div> 

    <div> 
     <label for='g52-email' class='grunion-field-label email'>Email<span> 
     (required)</span></label> 
     <input type='email' name='g52-email' id='g52-email' value='' 
     class='email' required aria-required='true'/> 
    </div> 

    <div> 
     <label for='g52-website' class='grunion-field-label 
     url'>Website</label> 
     <input type='text' name='g52-website' id='g52-website' value='' 
     class='url' /> 
    </div> 

<div> 
     <label for='contact-form-comment-g52-comment' class='grunion-field-label textarea'>Comment<span>(required)</span></label> 
     <textarea name='g52-comment' id='contact-form-comment-g52-comment' rows='20' class='textarea' required aria-required='true'></textarea> 
    </div> 
    <p class='contact-submit'> 
     <input type='submit' value='Submit' class='pushbutton-wide'/> 
</form> 

I避難所、これまで持っているものですノードを使ってメールを送る経験は多かったです。私は1ページのサイトを持っています。私がしたいのは、電子メールを送信できることだけです。どのように私のフォームをNodeに接続するのですか?

+0

あなたが持っている問題やエラーを定義してください。 –

+0

問題は、app.postとNodemailerを使用してHTMLフォームをapp.jsファイルにどのように接続するのか分かりませんでした。すべてが今解決されました。 –

答えて

0

req.bodyで確認するすべてのフィールドは、<input><textarea> HTML要素のname=""属性と一致する必要があります。あなたのスクリプトでこれらの値を取得していないと思われますが、場合によってはreq.body.emailまたはreq.body.mailのいずれかを見ることもできます。

{ 
    "g52-name": "...", 
    "g52-email": "...", 
    "g52-website": "...", 
    "g52-comment": "..." 
} 

フォームのコンテンツがある場所です:あなたがconsole.log(req.body)に提出する場合は、むしろのようなものが表示されます。

app.post('/', function(req, res) { 

    var subject = req.body["g52-name"]; 
    var mail = req.body["g52-email"]; 
    var website = req.body["g52-website"]; 
    var description = req.body["g52-comment"]; 

    if (!subject || !mail) { 
    res.send("Error: Email & Subject should not be Blank"); 
    return false; 
    } 

    // rest of email-sending code here 
    // ... 

} 
+0

ありがとう!出来た! –

関連する問題