2

我有一个简单的 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();
    });
});

联系.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>
4

1 回答 1

6

好的,我解决了这个问题。不确定这是否是正确的方法,但它有效。当电子邮件成功发送时,我基本上通过浏览器确定(200)。

if(error) {
    res.send(500);
} else {
    res.send(200);
}
于 2014-05-06T16:11:37.867 回答