在Meteor中附加base64 PDF作为电子邮件附件
问题描述:
我正在使用Meteor并试图将pdf附加到电子邮件中。我现在将pdf作为base64字符串返回给客户端,并在新窗口中打开并显示pdf。我想将base64作为pdf格式的电子邮件附件。对于邮寄在Meteor中附加base64 PDF作为电子邮件附件
服务器方法:
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',
'[email protected]',//to
'[email protected]',//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附件,我会做些什么?我似乎无法得到与流星邮件发送的数据,因为我得到错误“预期的字符串和对象”。
谢谢,
答
您可以直接从服务器发送电子邮件。
...
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
}
]
});
...