如何从任何xml文件中获取邮件内容

时间:2017-04-10 09:37:26

标签: node.js xml email express nodemailer

我在我的平均堆栈应用程序中使用nodemailer。我正在使用它成功发送邮件。

现在我正在尝试根据情况发送邮件。为此,我将邮件内容存储在xml页面中,我需要根据nodemailer的html标记中的关键字获取该邮件内容。

例如:如果注册用户,则通过在xml文件中获取注册密钥来发送注册邮件。

XML文件如下所示

   <?xml version="1.0" standalone="yes"?>
<MailContent>
  <registration>
    <AfterSignUp>
     <html><body><p>
      Hello #FirstName #LastName
     </p>
      <p>
      <br/>
      You recently registered for #ProjectName.To complete your registration, please <a href="#URL">Click Here </a> to confirm 'your' account.
      </p> 
     <br/>
      <p>
      Warm Regards,
      </p>
      <br/>
      <p>
      System Administrator
      </p>
      <br/>
      <p>
      #ProjectName.
      </p></body></html></AfterSignUp>
    <SignUpSubject>Registration confirmation for #ProjectName</SignUpSubject>
  </registration>

  <ForgotPassword>
    <ForgetContent><html><body><p>
      Hello #FirstName #LastName
      </p>
      <p>
      You have requested to have your password reset for your account at #ProjectName.
      </p>
      <p>
      Click the below link to reset your account password
      </p>
      <p>
      <a href="#URL">Reset Password </a>
      </p>
      <br/>
       <p>
      Warm Regards,
      </p>
      <br/>
      <p>
      System Administrator
      </p>
      <br/>
      <p>
       #ProjectName.
      </p></body></html></ForgetContent>
    <ForgetSubject>#ProjectName password recovery.</ForgetSubject>
  </ForgotPassword>

</MailContent>

节点邮件程序邮件选项如下

    let mailOptions = {
    from: 'mailid', // sender address
    to: 'mailid', // list of receivers
    subject:, // Subject line

    html: '' // html body
};

以上如何在nodemailer选项中获取该xml文件内容

1 个答案:

答案 0 :(得分:0)

我通过使用把手和fs通过引用Nodemailer来呈现html页面来解决问题,以便快速解决。

我创建了registrationcontent.html并在节点邮件中将其呈现如下

 var readHTMLFile = function (path, callback) {
                        fs.readFile(path, { encoding: 'utf-8' }, function (err, html) {
                            if (err) {
                                throw err;
                                callback(err);
                            }
                            else {
                                callback(null, html);
                            }
                        });
                    };

                    console.log("Sending", user, done);
                    var smtpConfig = {
                        host: mailconfig.host,
                        port: mailconfig.port,
                        auth: {
                            user: mailconfig.USER,
                            pass: mailconfig.PASSWORD
                        }

                    };
                    console.log(smtpConfig);
        var transporter = nodemailer.createTransport(smtpConfig);

     readHTMLFile(__dirname + '/../MailContents/mail.html', function (err, html) {
                        var template = handlebars.compile(html);
                        var replacements = {
                            FirstName: user.FirstName,
                            LastName: user.LastName,
            URL :`http://sample.com`
                            };
                        var htmlToSend = template(replacements);
                        var mailOptions = {
                            to: user.Email,
                            from: 'mail',
                            subject: 'Registration confirmation',
                            html: htmlToSend
                        };
transporter.sendMail(mailOptions, (err) => {
     console.log("An e-mail has been sent to with further instructions.");
     res.json("Thank you for registering. An Email has been sent to you. Go to registered email inbox to complete the registration process.");
                            done(err);
                        });
                    });

对于快速解决方案,我已为每个邮件使用单独的html,但我只需要使用xml或html中的一个文件或任何其他包含一个文件中所有邮件正文的格式,并且可以通过关键字获取。

Any solution is highly appreciated.

由于