-
-
Save subversivo58/4c0b87ffc6f3dbf7c2066abd2d6d9346 to your computer and use it in GitHub Desktop.
Simple wrapper to use "googleapis" in Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HOST=mehost.com | |
APP_MAIL="Site Name" <memail@mehost.com> | |
GMAIL_APITOKEN={JSON API TOKEN} | |
GMAIL_APISECRET={JSON API SECRET} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @licence The MIT License (MIT) | |
* @copyright Copyright (C) 2017 Lauro Moraes [https://subversivo58.github.com/] | |
*/ | |
const google = require('googleapis') | |
const googleAuth = require('google-auth-library') | |
const gmail = google.gmail('v1') | |
/** | |
* Create RAW email content. Gmail API requires compatible RFC 2822 MIME and encoded as base64 strings | |
* Gmail API reference @see{https://developers.google.com/gmail/api/guides/sending#creating_messages} | |
* RFC 2822 @see{https://tools.ietf.org/html/rfc2822} | |
* @return {string} - base64 encode email | |
* @api - private | |
*/ | |
const RAWMAIL = function(to, from, subject, template) { | |
let content = [ | |
'Content-Type: text/html; charset=\'UTF-8\'\n', | |
'MIME-Version: 1.0\n', | |
'Content-Transfer-Encoding: 7bit\n', | |
'to: ', to, '\n', | |
'from: ', from, '\n', | |
'subject: ', subject, '\n\n', | |
template | |
].join('') | |
let encodedMail = new Buffer(content).toString('base64').replace(/\+/g, '-').replace(/\//g, '_') | |
return encodedMail | |
} | |
/** | |
* Send | |
* @return Promise | |
* @api - private | |
*/ | |
const sendMail = function(opts) { | |
return new Promise((resolve, reject) => { | |
let require = { | |
auth: opts.auth, | |
userId: 'me', | |
resource: { | |
raw: opts.raw | |
} | |
} | |
gmail.users.messages.send(require, (err, response) => { | |
if ( !!err ) { | |
reject({ | |
code: 1, | |
message: err | |
}) | |
} | |
resolve(response) | |
}) | |
}) | |
} | |
class Gmail { | |
constructor(opts) { | |
this.credentials = (!!process.env.GMAIL_APISECRET) ? JSON.parse(process.env.GMAIL_APISECRET) : false | |
this.token = (!!process.env.GMAIL_APITOKEN) ? JSON.parse(process.env.GMAIL_APITOKEN) : false | |
if ( !this.credentials || !this.token ) { | |
this.enabled = false | |
} else { | |
this.enabled = true | |
} | |
} | |
/** | |
* Send function | |
* @return Promise | |
* @api - public | |
*/ | |
send(opts) { | |
return new Promise((resolve, reject) => { | |
if ( !!this.enabled ) { | |
// | |
if ( !opts.to || !opts.template || !opts.subject ) { | |
reject({ | |
code: 0, | |
message: 'No have one or more properties "to" (email target), "template" or "subject"' | |
}) | |
} else { | |
// define OAuth2 | |
let auth = new googleAuth() | |
let oauth2Client = new auth.OAuth2( | |
this.credentials.installed.client_id, | |
this.credentials.installed.client_secret, | |
this.credentials.installed.redirect_uris[0] | |
) | |
oauth2Client.credentials = this.token | |
// define sender (fallback to host) | |
let sender = (!!opts.from) ? opts.from : (!!process.env.APP_MAIL) ? process.env.APP_MAIL : process.env.HOST | |
// make email | |
let raw = RAWMAIL( | |
opts.to, | |
sender, | |
opts.subject, | |
opts.template // with body HTML content | |
) | |
sendMail({ | |
auth: oauth2Client, | |
raw: raw | |
}).then((response) => { | |
resolve(response) | |
}).catch((e) => { | |
reject(e) | |
}) | |
} | |
} else { | |
reject({ | |
code: 0, | |
message: 'No have "Gmail API Credentials"' | |
}) | |
} | |
}) | |
} | |
} | |
module.exports = new Gmail() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gmail.send({ | |
to: 'your_email_target@domain.com', | |
subject: 'Awesome subject', | |
template: '<h1>Exmple of HTML</h1>' | |
}).then((response) => { | |
//... | |
}).catch((e) => { | |
//... | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment