当用户注册我的网站时,我正在尝试发送电子邮件。我决定使用Nodemailer和MailGun,并意识到没有太多的教程或优秀的文档可以协同工作。对于其余的服务,例如MailGun,似乎是一个类似的问题。下面是我的代码,试图使其正常工作。我看了一个使它工作的教程,但似乎无法弄清楚。我认为代码是正确的,但是auth可能是错误的?或者,我可能两者都没有。我在我的开发环境中使用cloud9.io,所以也许是问题所在?还是我的域尚未验证?

路线:

app.post('/signup',
    setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
    isUnauthenticated,
    registrations.postSignup,
    (req, res) =>{
      const output = `
        <h1>Your email is: ${req.body.email}</h1>
         `;

      // create reusable transporter object using the default SMTP transport
      let transporter = nodemailer.createTransport({
        host: 'postmaster@MYSUBDOMAIN',
        port: 587,
        secure: false, // true for 465, false for other ports
        auth: {
            user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
            pass: 'MYPASSWORD'  // generated ethereal password
        },
        tls:{
          rejectUnauthorized:false
        }
      });

      // setup email data with unicode symbols
      let mailOptions = {
          from: '"Nodemailer Contact" <[email protected]>', // sender address
          to: '`${req.body.email}`', // list of receivers
          subject: 'Node Contact Request', // Subject line
          text: 'Hello world?', // plain text body
          html: output // html body
      };

      // send mail with defined transport object
      transporter.sendMail(mailOptions, (error, info) => {
          if (error) {
              return console.log(error);
          }
          console.log('Message sent: %s', info.messageId);
          console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

          // render view here and pass in data/message
      });
    });


然后来自mailgun.com的域信息:

State
Unverified
IP Address
IPASSIGNED Manage IPs
SMTP Hostname
smtp.mailgun.org
Default SMTP Login
postmaster@MYSUBDOMAIN
API Base URL
https://api.mailgun.net/v3/MYSUBDOMAIN
Default Password
MYPASSWORD SMTP credentials
API Key
MYKEY

最佳答案

想通我应该更新此答案以显示差异。我将端口从587更改为2525,因为我使用的是托管在Google服务器上的cloud 9,它们阻塞了端口587,但允许2525用于SMTP。其次,我将to电子邮件更改为我设置为req.body.email的变量,这是新用户的电子邮件。我的表格没有名字,因为它是针对a)公司b)进行注册时会遇到更大的摩擦,并且转换率会稍有降低。如果您的表单具有其名称,请在我再次使用其电子邮件的模板字符串中使用该名称。

app.post('/signup',
    setRedirect({auth: '/dashboard', success: '/dashboard', failure: '/signup'}),
    isUnauthenticated,
    registrations.postSignup,
    (req, res) =>{
      var newUserEmail = req.body.email;
      const output = `
        <h1>Your email is: ${req.body.email}</h1>
      `;

      // create reusable transporter object using the default SMTP transport
      let transporter = nodemailer.createTransport({
        host: 'smtp.mailgun.org',
        port: 2525,
        secure: false, // true for 465, false for other ports
        auth: {
            user: 'postmaster@MYSUBDOMAIN', // generated ethereal user
            pass: 'MYPASSWORD'  // generated ethereal password
        },
        tls:{
          rejectUnauthorized:false
        }
      });

      // setup email data with unicode symbols
      let mailOptions = {
          from: '"Nodemailer Contact" <[email protected]>', // sender address
          to: newUserEmail, // list of receivers
          subject: 'Node Contact Request', // Subject line
          text: 'Hello world?', // plain text body
          html: output // html body
      };

      // send mail with defined transport object
      transporter.sendMail(mailOptions, (error, info) => {
          if (error) {
              return console.log(error);
          }
          console.log('Message sent: %s', info.messageId);

          // render view here and pass in data/message
      });
    });

关于node.js - Nodemailer与Mailgun,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46877779/

10-12 15:46