使用nodemailer发送ejs模板 [英] Sending ejs template using nodemailer

查看:54
本文介绍了使用nodemailer发送ejs模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用express.js构建一个Web应用程序.在我的应用程序中,我正在使用nodemailer发送邮件.如果我只是用它来发送基本邮件,它就可以正常工作;但是,当我尝试使用nodemailer发送呈现的ejs文件时,收件人只会收到一个空邮件.因此,这里是我的代码的详细信息:

I'm trying to build a web application using express.js. In my application, I'm using nodemailer for sending mail. If I just use it to send basic mail it work ok; however, when I try to use nodemailer to send a rendered ejs file, the recipient only receive an empty mail. So here the details of my code:

var transporter = nodemailer.createTransport({
                host: 'smtp.zoho.com',
                port: 465,
                secure: true, // use SSL
                auth: {
                    user: 'testmail@zoho.com',
                    pass: '123456'
                }
            });
fs.readFile('/test.ejs', 'utf8', function (err, data) {
                if (err) {
                    return console.log(err);
                }
                var mainOptions = {
                    from: '"Tester" testmail@zoho.com',
                    to: email,
                    subject: 'Hello, world'
                    html: ejs.render(data, {name: 'Stranger'});
                };
                console.log(mainOptions.html);
});
transporter.sendMail(mainOptions, function (err, info) {
                            if (err) {
                                console.log(err);
                            } else {
                                console.log('Message sent: ' + info.response);
                            }
                        });

在这里,test.ejs(也可以是结果console.log(mainOptions.html)很好,因为它可以正确打印出呈现的ejs文件的字符串(<%= name%>替换为'Stranger')

Here the test.ejs (also, the result console.log(mainOptions.html) is fine because it print out the string of the rendered ejs file correctly (<%= name %> replaced with 'Stranger')

<style type="text/css">
  .header {
    background: #8a8a8a;
  }
  .header .columns {
    padding-bottom: 0;
  }
  .header p {
    color: #fff;
    padding-top: 15px;
  }
  .header .wrapper-inner {
    padding: 20px;
  }
  .header .container {
    background: transparent;
  }
  table.button.facebook table td {
    background: #3B5998 !important;
    border-color: #3B5998;
  }
  table.button.twitter table td {
    background: #1daced !important;
    border-color: #1daced;
  }
  table.button.google table td {
    background: #DB4A39 !important;
    border-color: #DB4A39;
  }
  .wrapper.secondary {
    background: #f3f3f3;
  }
</style>



<wrapper class="header">
  <container>
    <row class="collapse">
      <columns small="6">
        <img src="http://placehold.it/200x50/663399">
      </columns>
      <columns small="6">
        <p class="text-right">BASIC</p>
      </columns>
    </row>
  </container>
</wrapper>

<container>

  <spacer size="16"></spacer>

  <row>
    <columns small="12">

      <h1>Hi, <%= name %></h1>
      <p class="lead">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magni, iste, amet consequatur a veniam.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut optio nulla et, fugiat. Maiores accusantium nostrum asperiores provident, quam modi ex inventore dolores id aspernatur architecto odio minima perferendis, explicabo. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima quos quasi itaque beatae natus fugit provident delectus, magnam laudantium odio corrupti sit quam. Optio aut ut repudiandae velit distinctio asperiores?</p>
      <callout class="primary">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit repellendus natus, sint ea optio dignissimos asperiores inventore a molestiae dolorum placeat repellat excepturi mollitia ducimus unde doloremque ad, alias eos!</p>
      </callout>
    </columns>
  </row>
  <wrapper class="secondary">

    <spacer size="16"></spacer>

    <row>
      <columns large="6">
        <h5>Connect With Us:</h5>
        <button class="facebook expand" href="http://zurb.com">Facebook</button>
        <button class="twitter expand" href="http://zurb.com">Twitter</button>
        <button class="google expand" href="http://zurb.com">Google+</button>
      </columns>
      <columns large="6">
        <h5>Contact Info:</h5>
        <p>Phone: 408-341-0600</p>
        <p>Email: <a href="mailto:foundation@zurb.com">foundation@zurb.com</a></p>
      </columns>
    </row>
  </wrapper>
</container>

如果我将mainOptions.html替换为简单的内容,例如:< b> Hello,world!</b> ,则收件人将准确地收到内容.但是,如果我使用上面的代码,则收件人只会收到一封内容为空的电子邮件(收件人仍正确接收发件人,主题和其他信息).我尝试用文本替换html,以将呈现的字符串作为纯文本而不是html发送,但是收到的邮件仍为空.目前,我可以提供我问题的所有详细信息.因此,如果有人知道我的代码有什么问题,请为我指出.

If I replace the mainOptions.html with simple content, for instance: <b>Hello, world!</b> the recipient will receive the content accurately. However, if I use the above code, the recipient will only receive an email with empty content (the recipient still receive sender, subject and other informations correctly). I have try to replace the html with text to send the rendered string as plain text instead of html but the received mail still have empty content. That all the details of my problem I can provide at the moment. So if anyone know what is wrong with my code please point it out for me.

在此先感谢您能提供的帮助.

Thanks in advance for any help you are able to provide.

推荐答案

问题是 sendMail fs.readFile 完成之前得到执行.

The problem is sendMail gets executed before the fs.readFile gets completed.

实际上,readFile可以替换为 ejs.renderFile ,后者可以读取文件并呈现HTML字符串.请尝试下面的重构代码.

Actually, the readFile can be replaced with ejs.renderFile which reads file and renders the HTML string. Please try the below refactored code.

var fs = require("fs");
var nodemailer = require("nodemailer");
var ejs = require("ejs");
var transporter = nodemailer.createTransport({
    host: 'smtp.zoho.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: 'testmail@zoho.com',
        pass: '123456'
    }
});

ejs.renderFile(__dirname + "/test.ejs", { name: 'Stranger' }, function (err, data) {
if (err) {
    console.log(err);
} else {
    var mainOptions = {
        from: '"Tester" testmail@zoho.com',
        to: "totest@zoho.com",
        subject: 'Hello, world',
        html: data
    };
    console.log("html data ======================>", mainOptions.html);
    transporter.sendMail(mainOptions, function (err, info) {
        if (err) {
            console.log(err);
        } else {
            console.log('Message sent: ' + info.response);
        }
    });
}

});

这篇关于使用nodemailer发送ejs模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆