2
votes

I am using Meteor and attempting to attach a pdf to an email. I currently have the pdf being returned to the client as a base64 string that opens in a new window and displays the pdf. I would like to attach the base64 as an email attachment in the form of a pdf.

Server Method for Mailing:

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
    });
  }
});

snippet that returns base64 string to client:

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;

Client side code that currently displays 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!");
                      }
          }
        });

What would I got about doing in order to attach the base64 string as a pdf attachment? I cant seem to get the data to send with Meteor mail as I get the error "expected String and got object".

Thanks,

1

1 Answers

0
votes

You can send Email directly from the server.

...
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
      }
    ]
});
...