我正在使用 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 邮件发送的数据,因为我收到错误“预期字符串并获取对象”。
谢谢
最佳答案
您可以直接从服务器发送电子邮件。
...
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
}
]
});
...
关于javascript - 在 Meteor 中附加 base64 PDF 作为电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37232114/