【问题标题】:nodemailer doesn't stop sending mailsnodemailer 不会停止发送邮件
【发布时间】:2014-05-28 11:43:25
【问题描述】:

我有一个简单的 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>

【问题讨论】:

  • 每次您向/contact 发帖时都会发送一封电子邮件。您是否确认是发送多封电子邮件的节点邮件程序还是您只是多次发布?
  • 我不确定如何测试是节点邮件程序还是我多次发布。我添加了我的contact.ejs,希望那里能提示我做错了什么,但这是非常标准的东西。
  • 用 chrome 开发工具检查一下,在网络标签上你可以跟踪你的应用的网络活动。
  • 在 POST 请求的函数处理程序中使用 console.log()。然后,查看控制台以识别它是 node-mailer 还是多个 POST 请求。
  • 当我检查 chrome 的开发工具时,它只会说(待处理),直到我收到 2 封电子邮件然后它超时。我应该用 console.log() 记录什么?我尝试了 smtpTransport 和 app.post 但我没有看到任何有用的东西。对不起,如果这很明显,但我正在学习。

标签: node.js email nodemailer


【解决方案1】:

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

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

【讨论】:

  • .send() 现在已弃用。请改用.sendStatus()
  • .sendStatus() 现在在 Express 中使用,但不在开箱即用的节点中使用。对我有用的是response.statusCode = &lt;500 or 200 here&gt;res.end()ifelse 中。对此可能有更优雅的解决方案,但不幸的是我不知道。
猜你喜欢
  • 1970-01-01
  • 2022-07-18
  • 1970-01-01
  • 2019-10-06
  • 2017-12-22
  • 2013-07-01
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多