Dar*_*ght 1 javascript pdf email node.js nodemailer
我有一个js包含 html 内容的文件。
.js 文件
const data = (data) => {
return `<h1> This is my pdf data </h1>`
}
export default data
Run Code Online (Sandbox Code Playgroud)
这是我的nodemailer功能
import template from "js_file_path"
const body = template(data);
const mail = mailcomposer({
from: "XXXX",
to: "XXXX",
subject: `Subject`,
attachments: [
{
filename: "Receipt.pdf",
content: body
}
]
});
// mail.build()
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。谁能建议我如何做到这一点?
小智 6
这是一个生成PDF文件的库吗?
从“js_file_path”导入模板
如果不是这种情况,您应该使用一个库,该库可以根据您传递给它的模板生成 pdf。
例如: https: //www.npmjs.com/package/pdfkit
代码示例:
import pdfGenerator from "pdgGeneratorLibrary"
import pdfTemplate from "pdfTemplate"
import nodemailer from "nodemailer"
(async () => {
try {
// build your template
const pdfBufferedFile = await pdfGenerator(pdfTemplate);
// set your transport
const transporter = nodemailer.createTransport({ ...});
// set your options
const mailOptions = {
from: "XXXX",
to: "XXXX",
subject: `Subject`,
attachments: [{
filename: "Receipt.pdf",
contentType: 'application/pdf', // <- You also can specify type of the document
content: pdfBufferedFile // <- Here comes the buffer of generated pdf file
}]
}
// Finally, send the email
await transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error)
} else {
console.log(info)
}
});
} catch (err) {
// to do handle error
}
})()
Run Code Online (Sandbox Code Playgroud)
我希望它对你有帮助。问候。
| 归档时间: |
|
| 查看次数: |
4210 次 |
| 最近记录: |