Thursday 15 June 2017

Attaching a file to an email sent with googleapis

I've been scratching my head on this for the last couple of weeks, and I feel like it's something obvious that I'm not quite understanding. I've managed to compose and send an email with code I found on StackOverflow...function sendEmail (auth, sender, recipients, subject, body, options, callback) { var raw = makeBody(recipients.join(','), sender, subject, body) gmail.users.messages.send({ auth: auth, userId: 'me', resource: { raw: raw } }, function (err, response) { if (err) { console.error(err) } console.log(response) }) } /* http://ift.tt/2tsoANb */ function makeBody (to, from, subject, message) { var str = ['Content-Type: text/plain; charset="UTF-8"\n', 'MIME-Version: 1.0\n', 'Content-Transfer-Encoding: 7bit\n', 'to: ', to, '\n', 'from: ', from, '\n', 'subject: ', subject, '\n\n', message ].join('') return btoa(str) } ...but when I try to modify the first object in gmail.users.messages.send to include a media object that looks like so...media: { mimeType: 'text/plain', body: fs.createReadStream('someFile.csv') } ...I get an error in the callback saying text/plain isn't supported, and only message/rfc822 is. I have no idea how to make that happen, and I was relying on the part of the README that said readable streams could be used (though I suppose that's only for v3 of Drive).So I'm guessing I need to read in the file, encode it as a message/rfc822 in some way similar to makeBody works, and then insert that as media.body? If so, I'm at a loss for how. I'm getting lost reading the W3's documentation on message/rfc822.

Submitted June 15, 2017 at 11:17PM by pm_your_moneymaker

No comments:

Post a Comment