在NodeEmailer中使用CSS作为电子邮件内容的外部HTML文件
有人知道如何告诉Node.js模块NodeEmailer使用外部HTML文件(带有样式表链接)作为电子邮件内容吗 我能够键入HTML标记作为电子邮件内容,但我更喜欢加载完整的HTML文件。 我正在使用NodEmailer1.3.0 不幸的是,对于实际的nodeEmailer版本,我找不到任何涉及此问题的示例在NodeEmailer中使用CSS作为电子邮件内容的外部HTML文件,html,node.js,nodemailer,Html,Node.js,Nodemailer,有人知道如何告诉Node.js模块NodeEmailer使用外部HTML文件(带有样式表链接)作为电子邮件内容吗 我能够键入HTML标记作为电子邮件内容,但我更喜欢加载完整的HTML文件。 我正在使用NodEmailer1.3.0 不幸的是,对于实际的nodeEmailer版本,我找不到任何涉及此问题的示例 想法?我建议使用fs模块的readFile函数读取HTML文件,然后在回调函数中通过电子邮件发送 readFile教程:您应该使用内联css,而不是单独的文件,因为头元素的HTML呈现是由提
想法?我建议使用
fs
模块的readFile
函数读取HTML文件,然后在回调函数中通过电子邮件发送
readFile教程:您应该使用内联css,而不是单独的文件,因为头元素的HTML呈现是由提供者而不是节点收信人定义的 我建议,这很方便,只需设置
html: '<div style="font - family: verdana; max-width:500px; margin-left">' + 'more string and string'
html:“+”更多字符串和字符串”
使用
它是一个node.js包,允许您内联css,这样您就可以使用nodeEmailer发送完整的HTML电子邮件
它还可以预先添加/更新电子邮件中的链接,使它们不再是相对链接。(即,您可以将类似“coverage/local.html”的链接更改为“\myhostname\afolder\coverage\local.html”
这里有一个粗略的脚本,我通过电子邮件发送伊斯坦布尔代码覆盖率的输出
/*
* Use this script to send Istanbul code coverage results via email.
*
* NPM REQUIREMENTS:
* The following packages need to be installed:
* - npm install styliner
* - npm install nodemailer
* - npm install nodemailer-smtp-transport
*
* Usage: node sendNodeJSCoverageEmail.js <origfile> <basedir> <receipient>
*
* <origfile> : This is the original Istanbul index.html from the code coverage report
* <basedir> : TeamCity directory where the index.html resides. Must contain "DevTC" folder (see below)
* <recipient> : Email address to send the results to.
*
* What it does:
* Istanbul creates an html report for code coverage. It is a complete web page, and has an index.html,
* some CSS style sheets, and relative links to other coverage.
*
* This script takes the index.html, reads the referenced .css files, and inlines the css, so that external
* files are no longer needed. (this is because you can't send multiple files for an html email).
* Also, the relative links are prepended with the UNC path to the files: this means that the recipients
* can click on the links in the html email and they will work.
*
* NOTE: it assumes the TeamCity folder where the coverage reports reside is shared, and also contains the
* folder DevTC (it looks for this folder to do the substitution). All build machines have this
* folder structure.
*
*/
var nodemailer = require('nodemailer');
var os = require("os");
var hostname = os.hostname();
var originalFile = process.argv[2].toString();
var baseDir = process.argv[3].toString();
var recipient = process.argv[4].toString();
var Styliner = require('styliner');
var uncDrive = '\\\\' + hostname + '\\DevTC';
var uncPath = baseDir.replace(/.*DevTC/gi, uncDrive);
// prependUNCPath is a function called by Styliner for every
// link that is found in the HTML.
function prependUNCPath(path, type) {
return uncPath + path;
}
// See http://styliner.slaks.net/#about for Styliner options
var options = { url : prependUNCPath, noCSS : true };
var styliner = new Styliner(baseDir, options);
function sendEmail(source) {
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
// create reusable transporter object using SMTP transport
var transport = nodemailer.createTransport(smtpTransport({
host: 'your-smtp-server.local', // FIXME: Change this!
port: 25,
}));
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'TeamCity <teamcity@company.com>', // sender address
to: recipient, // list of receivers
subject: 'Code Coverage results', // Subject line
// text: 'Hello world ?', // plaintext body, not used
html: source // html body
};
// send mail with defined transport object
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Message sent: ' + info.response);
}
});
}
var fs = require('fs')
// Do the reading of the original index.html, and kick everything off.
fs.readFile(originalFile, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
styliner.processHTML(data)
.then(function (source)
{
sendEmail(source);
fs.writeFile("newindex.html", source, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
);
});
/*
*使用此脚本通过电子邮件发送伊斯坦布尔代码覆盖率结果。
*
*NPM要求:
*需要安装以下软件包:
*-npm安装样式器
*-npm安装nodeEmailer
*-npm安装nodeEmailer smtp传输
*
*用法:node sendNodeJSCoverageMail.js
*
*:这是代码覆盖率报告中的原始伊斯坦布尔index.html
*:index.html所在的TeamCity目录。必须包含“DevTC”文件夹(见下文)
*:将结果发送到的电子邮件地址。
*
*它的作用是:
*伊斯坦布尔为代码覆盖率创建一个html报告。它是一个完整的网页,并且有一个index.html,
*一些CSS样式表,以及与其他内容的相关链接。
*
*此脚本获取index.html,读取引用的.css文件,并将css内联,以便
*不再需要文件。(这是因为不能为html电子邮件发送多个文件)。
*此外,相对链接前面有文件的UNC路径:这意味着收件人
*可以点击html电子邮件中的链接,它们就会工作。
*
*注意:它假定覆盖率报告所在的TeamCity文件夹是共享的,并且还包含
*文件夹DevTC(它查找此文件夹以进行替换)。所有生成计算机都有此文件夹
*文件夹结构。
*
*/
var nodeEmailer=require('nodeEmailer');
var os=要求(“os”);
var hostname=os.hostname();
var originalFile=process.argv[2].toString();
var baseDir=process.argv[3].toString();
var recipient=process.argv[4].toString();
var Styliner=require('Styliner');
变量uncDrive='\\'+hostname+'\\DevTC';
var uncPath=baseDir.replace(/.*DevTC/gi,uncDrive);
//prependUNCPath是Stylener为每个
//在HTML中找到的链接。
函数prependUNCPath(路径,类型){
返回路径+路径;
}
//看http://styliner.slaks.net/#about 用于样式器选项
var options={url:prependUNCPath,noCSS:true};
var styliner=新styliner(baseDir,选项);
发送电子邮件功能(来源){
var nodeEmailer=require('nodeEmailer');
var smtpTransport=require('nodemailer-smtp-transport');
//使用SMTP传输创建可重用的传输对象
var transport=nodeEmailer.createTransport(smtpTransport({
主机:'您的smtp服务器。本地',//修复:更改此!
港口:25,
}));
//使用unicode符号设置电子邮件数据
var mailpoptions={
发件人:'团队城市',//发件人地址
收件人://收件人列表
主题:'代码覆盖率结果',//主题行
//text:“Hello world?”,//纯文本正文,未使用
html:source//html正文
};
//使用定义的传输对象发送邮件
transport.sendMail(邮件选项,函数(错误,信息){
如果(错误){
console.log(错误);
}否则{
console.log('发送的消息:'+信息响应);
}
});
}
var fs=require('fs')
//阅读原始index.html,然后启动所有内容。
fs.readFile(原始文件'utf8',函数(错误,数据){
如果(错误){
返回console.log(err);
}
stylener.processHTML(数据)
.then(函数(源)
{
发送电子邮件(来源);
writeFile(“newindex.html”、源、函数(err){
如果(错误){
返回console.log(err);
}
log(“文件已保存!”);
});
}
);
});
我只能将HTML文件作为附件获取,但不能将其内容用作电子邮件内容。我尝试了您描述的方式。使用fs,我可以将HTML文档的内容存储在变量中。但我无法将此变量分配给NodeEmailer的mailOptions。HTML:HTMLContent不起作用。下一步该怎么办?它应该可以正常工作。您是否重复了-检查HTMLContent变量是否包含正确的内容?并且您正在将其放在mailOptions的“html”属性中,而不是“text”属性中,对吗?如果您确定这些内容并且它仍然不起作用,最后一个努力是尝试在mailOptions中设置{“content type”:“text/html”}标题(有关详细信息,请参阅文档)“headers”选项以获取更多信息)并查看是否有帮助。我尝试了许多选项,但都没有成功。但是我不知道你的意思,我应该在我的邮件选项中设置{“Content type”:“text/html”}的标题。你有具体的例子吗?顺便问一下,“headers”的文档在哪里“您提到的选项?有链接吗?mailOptions是传递给sendMail()函数的对象的示例名称。”