我正在使用Meteor并尝试将pdf附加到电子邮件中。我目前将pdf作为base64字符串返回给客户端,该字符串在新窗口中打开并显示pdf。我想以PDF格式附加base64作为电子邮件附件。
邮件服务器方法:
Meteor.methods({
sendEmail: function (to, from, subject, html,attachment ) {
check([to, from, subject, html,attachment], [String]);
// Let other method calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();
Email.send({
to: to,
from: from,
subject: subject,
html: html,
attachment:attachment
});
}
});
将base64字符串返回给客户端的代码段
webshot(html_string, fileName, options, function(err) {
fs.readFile(fileName, function (err, data) {
if (err) {
return console.log(err);
}
fs.unlinkSync(fileName);
fut.return(data);
});
});
console.log("------------Waiting till PDF generated-----------");
pdfData = fut.wait();
base64String = new Buffer(pdfData).toString('base64');
console.log("------------Return result-----------");
return base64String;
当前显示pdf的客户端代码:
Meteor.call('screenshot',html,style,function(err, res) {
if (err) {
console.error(err);
} else if (res) {
window.open("data:application/pdf;base64, " + res);//view PDF result
if(localbool===true) {
Meteor.call('sendEmail',
'rambat1010@gmail.com',//to
'courtsec@courtsec.com',//from
'Hello from Meteor!',//subject
'Sample HTML'//html
**What do I put here to attach base64 PDF**
);//close call for email send
alert("email sent!");
}
}
});
为了将base64字符串作为pdf附件附加,我会做些什么?我似乎无法通过Meteor邮件发送数据,因为我收到了错误"期望的字符串并得到了对象"。
谢谢,
答案 0 :(得分:0)
您可以直接从服务器发送电子邮件。
...
pdfData = fut.wait();
base64String = new Buffer(pdfData).toString('base64');
let fileName = 'screenshot.pdf'
fs.writeFile(fileName, base64String, 'base64', function (err, res) {
if (err) {console.log('Err ', err); }
});
Email.send({
to: to,
from: from,
subject: subject,
html: html,
attachments: [
{
filePath: fileName
}
]
});
...