简体   繁体   English

如何在带有节点js的html文件中传递变量以发送邮件

[英]how can pass variable in html file with node js for sending mail

I am new to node.js.By using this code i am able to receive email. 我是node.js的新手,通过使用此代码,我可以接收电子邮件。 I don't know how to pass that "name" variable to html and get value of that variable in email. 我不知道如何将“名称”变量传递给html并在电子邮件中获取该变量的值。 Error is : Can't set headers after they are sent. 错误是:发送标头后无法设置标头。 app.js file app.js文件

mailer.extend(app, {
    from: 'mail@mail.com',
  host: 'smtp.gmail.com', // hostname 
  secureConnection: true, // use SSL 
  port: 465, // port for secure SMTP 
  transportMethod: 'SMTP', // default is SMTP. Accepts anything that nodemailer accepts 
  auth: {
    user: 'xxxxxxx',
    pass: 'xxxxxxx'
  }
});
app.engine('html', cons.swig)
app.set('views', __dirname + '/views');
app.set('view engine', 'html');

Rout.js file Rout.js文件

app.post("/registration", function(req, res) {
res.render('register', { name: firstName });
app.mailer.send('register', {
                    to: email, // REQUIRED. This can be a comma delimited string just like a normal email to field.  
                    subject: 'Test Email', // REQUIRED. 
                    otherProperty: 'Other Property' // All additional properties are also passed to the template as local variables. 

                },
                function (err) {
                        if (err) {

                          console.log(err);
                          res.send('There was an error sending the email');
                          return;
                        }


                        res.send({"code":1,"message":"Regestration Successfully."});
                        res.end();
                    });

register.html file register.html文件

<body>
Dear {{ name }}
</body>

First I'd try Octavio's suggestion. 首先,我会尝试Octavio的建议。

I personally use either React or express-handlebars to get data onto the page. 我个人使用React或express-handbars来获取数据到页面上。 If you're saving this particular piece of data in a database you could always use an ajax call or perhaps just get the axios package if you don't want to deal with importing the entire jquery library. 如果要将这些特定的数据保存在数据库中,则可以始终使用ajax调用,或者如果不想处理整个jquery库的导入,则可以仅获取axios包。

I'd say check these sources out for more info. 我想说说这些信息以获取更多信息。

It looks like you're using express already, so express-handlebars is probably the fastest way to go. 看来您已经在使用Express,所以Express-Handlebars可能是最快的方法。

You can read the html file and apply a string replace, assuming has a static "flags" like {{ name }} 您可以读取html文件并应用字符串替换,假设其中有一个像{{name}}这样的静态“标志”

js code: js代码:

const _fs = require('fs');
const _path = require('path');

var composeWithBasicTemplate = function (emailData, next) {
    var path = _path.join(__dirname, "email_templates/");
    _fs.readFile(path + 'basic.html', 'utf8', function (err, data) {
        if (err) {
            _console.log(err, 'error');

            return null;
        };

        data = data.replace('{TITLE}', emailData.ComposedTitle);
        data = data.replace('{TITLE}', emailData.ComposedTitle);
        data = data.replace('{BODY}', emailData.ComposedBody);
        data = data.replace('{URLACTION}', emailData.ComposedUrlAction);
        data = data.replace('{TEXTACTION}', emailData.ComposedTextAction);
        data = data.replace('{COPYRIGHTDATE}', (new Date()).getFullYear());

        next(data);
    });
}

html file: html文件:

...
<table width="100%" cellpadding="0" cellspacing="0" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
                                <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
                                    <td class="content-block" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 22px; text-align:center; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                        {TITLE}
                                    </td>
                                </tr>
                                <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
                                    <td class="content-block" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; text-align:center; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                        {BODY}
                                    </td>
                                </tr>
                                <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
                                    <td class="content-block" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; text-align:center; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
                                        <a href="{URLACTION}" class="btn-primary" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; color: #fff; text-decoration: none; line-height: 2; font-weight: bold; text-align: center; cursor: pointer; display: inline-block; border-radius: 0px; text-transform: capitalize; background: #d9534f; margin: 0; padding: 0; border-color: #d9534f; border-style: solid; border-width: 10px 20px;">{TEXTACTION}</a>
                                    </td>
                                </tr>
                                <tr style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; padding: 0;">
                                    <td class="content-block" style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px; text-align:center;">
                                        <br /><br />
                                        <p class="MsoNormal" align="center"><a href="{URLCONFIG}"><img src="{URLCONFIG}/content/img/Home/LARNIA_logo.png" width="280" /></a></p>
                                    </td>
                                </tr>
                            </table>
...

You set app.set('views', __dirname + '/views'); 您设置app.set('views', __dirname + '/views'); so make sure that the html file exists in this directory, and the syntax for variable template is <%=name%> , such as: 因此,请确保该目录中存在html文件,并且变量模板的语法为<%=name%> ,例如:

<body>
    Dear<%= name %>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM