Thursday 24 January 2019

Sending Emails with Gmail API

I'm currently working on hooking into the Gmail API using NodeJS. I have a working connection and can access my messages and such, but I'm having difficulty actually sending email with the API. Here is what I have below:/** * Send Message. * * @param {String} userId User's email address. The special value 'me' * can be used to indicate the authenticated user. * @param {String} email RFC 5322 formatted String. * @param {Function} callback Function to call when the request is complete. */ function sendMessage(userId, email, callback, auth) { // Using the js-base64 library for encoding: // https://www.npmjs.com/package/js-base64 //var base64EncodedEmail = Base64.encodeURI(email); var base64EncodedEmail = Buffer.from(email).toString('base64'); var request = gapi.client.gmail.users.messages.send({ 'userId': userId, 'resource': { 'raw': base64EncodedEmail } }); request.execute(callback); } This is from the official documentation (here), for the most part. I'm calling the function as such:authorize(JSON.parse(content), sendMessage('me', btoa('This is a test'))); The error I'm getting is this:var request = gapi.client.gmail.users.messages.send({ ^ReferenceError: gapi is not defined at sendMessage (D:\Documents\Web Programming\React\neis-guy-painting\src\Server\Node\gmail.js:148:17) at fs.readFile (D:\Documents\Web Programming\React\neis-guy-painting\src\Server\Node\gmail.js:23:34) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)I realize that this is because I don't have gapi defined, but I can't seem to figure out what that actually should be.What am I missing here?

Submitted January 24, 2019 at 08:17PM by Onikouzou

No comments:

Post a Comment