【问题标题】:how to include an HTML page in nodemailer in Node JS如何在 Node JS 的 nodemailer 中包含 HTML 页面
【发布时间】:2018-07-29 10:54:08
【问题描述】:

我想在电子邮件中使用 nodemailer 发送 Html 页面。但我不能包含我的 html 页面。这怎么可能?请帮助并给我一个解决方案...

这是我的 EmailSendHtml.js 文件

 var nodemailer=require('nodemailer');
 var fs=require('fs');

 var d;

 fs.readFile('index.html','UTF-8',function(err,data)
 {
     d=data;
 });

 var transporter=nodemailer.createTransport(
 {
 service:'gmail',
 auth:
 {
  user:'Me@gmail.com',
  pass:'********'
 }
 });

var a='<h1>Welcome</h1><p>That was easy!</p>';

 var mailOptions={
   from:'Me@gmail.com',
   to:'ToMyFriend@gmail.com',
   subject:'Sending HTML page using Node JS',
   html:d
 };

 transporter.sendMail(mailOptions,function(error,info)
 {
    if(error)
    {
        console.log(error);
    }
    else
    {
        console.log('Email Sent: '+info.response);
    }
 });

这是我想要在电子邮件中包含并显示的 html 页面。

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link type="text/css" rel="stylesheet" href="style.css"/>
    </head>
    <body>
        <h1>TODO write content</h1>
    </body>
</html>

这是我的 style.css 文件

/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
/* 
    Created on : Feb 19, 2018, 2:03:50 PM
    Author     : Lenovo
*/

h1
{
    color:red;
}

【问题讨论】:

  • fs.readFile() 是异步的。

标签: javascript jquery node.js


【解决方案1】:
  • 在 HTML 中使用内联 css 和
  • 如下使用文件读取流
  • 将文件读取流传递给sendmail // { html }
  • 不要忘记显式关闭文件流

代码示例

var htmlstream = fs.createReadStream("index.html");

transport.sendMail({ html: htmlstream }, function(err) {
    if (err) {
        // check if htmlstream is still open and close it to clean up
    }
});

【讨论】:

    【解决方案2】:

    只需将您的代码更改如下:

    var nodemailer=require('nodemailer');
    var fs=require('fs');
    
    require.extensions['.html'] = function (module, filename) {
        module.exports = fs.readFileSync(filename, 'utf8');
    };
    
     var data = require('index.html'); // path to your HTML template
    
     var transporter=nodemailer.createTransport(
     {
     service:'gmail',
     auth:
     {
      user:'Me@gmail.com',
      pass:'********'
     }
     });
    
    var a='<h1>Welcome</h1><p>That was easy!</p>';
    
     var mailOptions={
       from:'Me@gmail.com',
       to:'ToMyFriend@gmail.com',
       subject:'Sending HTML page using Node JS',
       html:data
     };
    
     transporter.sendMail(mailOptions,function(error,info)
     {
        if(error)
        {
            console.log(error);
        }
        else
        {
            console.log('Email Sent: '+info.response);
        }
     });
    

    【讨论】:

    • 我在电子邮件中看不到 html 页面
    猜你喜欢
    • 2018-09-30
    • 2020-09-23
    • 2014-11-18
    • 1970-01-01
    • 2014-05-11
    • 2013-04-25
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多