HTML2Canvas then email as attachment

First time caller, long time listener… I’m an iOS dev so please be gentle.

I’m trying to convert my HTML to an image and send an email with the image as an attachment. Seems like it should be easy but i want to put my fist into my screen. Please help!


Client side:

Template.report.events({
  'click #send_as_email': function() {
    html2canvas($('#full_report').get(0), {
      onrendered: function(canvas) {
        var image = canvas.toDataURL("image/png");

        ReportImages.insert(image, function(err, fileObj) {
          if (err) {
            console.log('Error writing to DB: ' + err.description);
          } else {
            Meteor.call('emailReport', 'to@to.com', 'from@from.com', 'Subject', 'Body', fileObj._id); 
          }
        });
      }
    });
  }
});

Server side:

Meteor.methods({
  emailReport: function(to, from, subject, body, imageId) {
    var doc = ReportImages.findOne({ _id: imageId });
    console.log(doc);
    Email.send({
      to: to,
      from: from,
      subject: subject,
      attachments: [doc]
    });
  }
});

I just get an email with no attachment… Help!

You need to make sure that your doc conforms to the mailcomposer doc. Looking at your code I think it’s just a blob.

Note especially:

One of contents, filePath or streamSource must be specified, if none is present, the attachment will be discarded. Other fields are optional.

You are a life saver. Stupid question #2, my var doc is an FS.FIle. How do I get this back into an image? I’ve tried:

   var attachment = {
      fileName: 'YourImage.png',
      filePath: doc.url
    };

But I get a blank attachment.

Thinking I need to stream it from Mongo.

var doc = ReportImages.findOne({ _id: imageId });
var readStream = doc.createReadStream();
var buffer = new Buffer(0);
readStream.on('readable', function() {
    buffer = Buffer.concat([buffer, readStream.read()]);
});
readStream.on('end', function() {
     console.log(buffer.toString('base64'));
     var attachment = {
       fileName: 'YourRetirementReport.png',
       filePath: buffer
     };

     Email.send({
       to: to,
       from: from,
       subject: subject,
       attachments: [attachment]
     });
});

But this gives me an error:

W20160217-17:04:16.048(-8)? (STDERR) /Users/username/.meteor/packages/cfs_gridfs/.0.0.33.19yqyp++os.osx.x86_64+web.browser+web.cordova/npm/node_modules/mongodb/lib/mongodb/connection/base.js:246
W20160217-17:04:16.049(-8)? (STDERR) throw message;
W20160217-17:04:16.051(-8)? (STDERR) ^
W20160217-17:04:16.051(-8)? (STDERR) Error: 56c5188f5288e97d0e9ab498 does not exist