node.js - nodemailer 不会停止发送邮件

标签 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();
    });
});

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

最佳答案

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

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

关于node.js - nodemailer 不会停止发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23468340/

相关文章:

node.js - Microsoft Graph API - 修补用户(应用程序 token 的权限问题)

html - 在 Go 中从命令行发送电子邮件时编写 html 的 CSS 规则

.net - System.Net.Mail.MailAddress'解析器接受的格式是什么?

javascript - 当调用 'this' 的函数位于子对象的对象内部时,您将如何引用 'this'?

javascript - 使用 Node.js 将文件上传到 Firebase 存储

python - 如何在 python 中从电子邮件中检索超链接 URL?

google-api - 使用 OAuth2 和 nodemailer 从我的 gmail 帐户发送电子邮件

node.js - Nodemailer 的邮件在 Azure 服务器上无法工作

javascript - 使用议程安排电子邮件

node.js - 安装 ursa 时出现 fatal error C1083