2014-05-05 50 views
2

我有一個簡單的nodemailer設置,可以毫無問題地發送電子郵件。唯一無法弄清的是如何阻止它一旦開始發送。我點擊發送,它發送一封電子郵件,但頁面不斷加載,很快我收到另一封電子郵件等。當然,它應該只發送一封電子郵件並關閉過程。nodemailer不停止發送郵件

任何人都可以看到我在這裏做錯了嗎?

var smtpTransport = nodemailer.createTransport('SMTP', { 
    host: 'mail.francotanzarella.com', 
    secureConnection: false, 
    port: 587, 
    auth: { 
     user: '[email protected]', 
     pass: '***' 
    } 
}); 

app.post('/contact', function(req, res) { 
var htmlTpl = '<h4>Message from' + ' ' + req.body.name + '</h4><p><span>' + req.body.email + '</span</p><p>' + req.body.message + '</p>'; 
    mailOptions = { 
     from: '[email protected]', 
     to: '[email protected]', 
     subject: 'New message from francotanzarella.com', 
     html: htmlTpl, 
     debug: true 
    } 
    smtpTransport.sendMail(mailOptions, function(error, response) { 
     if(error) { 
      console.log(error) 
     } else { 
      console.log(response.message); 
     } 
     smtpTransport.close(); 
    }); 
}); 

contact.ejs

<form id="contact-form" method="POST" name="userForm" action="/contact" ng-submit="submitForm(userForm.$valid)" novalidate> 
     <div class="row"> 
      <div class="small-12 column"> 

       <div class="row" ng-class="{ 'error' : userForm.name.$invalid && !userForm.name.$pristine && submitted }"> 
        <div class="small-12 column"> 
         <label for="name" class="inline">Name</label> 
        </div> 
        <div class="small-12 column"> 
         <input type="text" id="name" name="name" placeholder="Your name" ng-model="name" required /> 
         <small class="error" ng-show="userForm.name.$invalid && !userForm.name.$pristine">Please enter your name</small> 
        </div> 
       </div> 

       <div class="row" ng-class="{ 'error' : userForm.email.$invalid && !userForm.email.$pristine && submitted }"> 
        <div class="small-12 column"> 
         <label for="email" class="inline">Email</label> 
        </div> 
        <div class="small-12 column"> 
         <input type="email" id="email" name="email" placeholder="Your email" ng-model="email" required /> 
         <small class="error" ng-show="userForm.email.$invalid && !userForm.email.$pristine">I need a valid email please</small> 
        </div> 
       </div> 

       <div class="row" ng-class="{ 'error' : userForm.message.$invalid && !userForm.message.$pristine && submitted }"> 
        <div class="small-12 column"> 
         <label for="message" class="inline">Message</label> 
        </div> 
        <div class="small-12 column"> 
         <textarea id="message" name="message" placeholder="Send me a message" ng-model="message" required></textarea> 
         <small class="error" ng-show="userForm.message.$invalid && !userForm.message.$pristine">What? No message?</small> 
        </div> 
       </div> 

       <div class="row"> 
        <div class="small-12 column"> 
         <button type="submit" class="button" ng-disabled="userForm.$invalid">send</button> 
        </div> 
       </div> 

      </div> 
     </div> 
     <div id="form-response-container"> 
      <div id="for-response-inner"> 
       <h3 id="form-response"></h3> 
      </div> 
     </div> 
    </form> 
+0

每次發佈到'/ contact'都會發送一封電子郵件。您是否驗證過是否發送多封電子郵件的節點郵件程序,或者您是否只是多次發佈郵件? – callmekatootie

+0

我不知道如何測試它是否是節點郵件或發佈多次的我。我添加了我的contact.ejs,希望有一點我在做什麼錯誤,但它是非常標準的東西。 –

+1

使用chrome開發工具進行檢查,在網絡選項卡上,您可以跟蹤應用程序的網絡活動。 – balazs

回答

4

好吧,我解決了這個問題。不知道這是否是正確的方法,但它的工作原理。當郵件發送成功時,我基本上通過瀏覽器(200)。

if(error) { 
    res.send(500); 
} else { 
    res.send(200); 
} 
+1

'.send()'現在已被棄用。改用'.sendStatus()'。 – Desmond

相關問題