nodemailer不会停止发送邮件

时间:2014-05-05 08:44:42

标签: node.js email nodemailer

我有一个简单的nodemailer设置,可以毫无问题地发送电子邮件。我唯一能弄清楚的是如何阻止它一旦开始就发送。我点击发送,它发送一封电子邮件,但页面一直在加载,很快我收到另一封电子邮件等。当然它应该只发送一封电子邮件并关闭该过程。

有人能看到我在这里做错的事吗?

var smtpTransport = nodemailer.createTransport('SMTP', {
    host: 'mail.francotanzarella.com',
    secureConnection: false,
    port: 587,
    auth: {
        user: 'contact@francotanzarella.com',
        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: 'noreply@francotanzarella.com',
        to: 'contact@francotanzarella.com',
        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>

1 个答案:

答案 0 :(得分:5)

好的我修好了。不确定它是否是正确的方法,但它的工作原理。当电子邮件成功发送时,我基本上将浏览器传递给ok(200)。

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