From 7d13d9ece1df1d20f8e2b066ecc2519086a65cfa Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sat, 26 Aug 2017 15:45:50 +0530 Subject: [PATCH] feat(driver): add Ses driver --- .env.example | 2 ++ README.md | 6 +++++ package-lock.json | 8 +++--- package.json | 5 +++- src/Mail/Drivers/Ses.js | 54 +++++++++++++++++++++++++++++++++++++++ src/Mail/Drivers/index.js | 3 ++- test/ses.spec.js | 41 +++++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 src/Mail/Drivers/Ses.js create mode 100644 test/ses.spec.js diff --git a/.env.example b/.env.example index 25da5f7..4124d14 100644 --- a/.env.example +++ b/.env.example @@ -10,3 +10,5 @@ SPARKPOST_API_KEY= SPARKPOST_FROM_EMAIL= MAILGUN_API_KEY= MAILGUN_DOMAIN= +SES_KEY= +SES_SECRET= diff --git a/README.md b/README.md index b5098a3..9aa9f40 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,12 @@ Run the following command to see list of available npm scripts. npm run ``` +## Environment Variables + +There is a `.env.example` file in the project root, rename it as `.env` and add values for all services to run tests on your local. + +DO MAKE SURE TO NOT COMMIT THE `.env` FILE. + ### Tests & Linting 1. Lint your code using standardJs. Run `npm run lint` command to check if there are any linting errors. diff --git a/package-lock.json b/package-lock.json index 69b12a4..7e10127 100644 --- a/package-lock.json +++ b/package-lock.json @@ -179,9 +179,9 @@ "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "aws-sdk": { - "version": "2.102.0", - "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.102.0.tgz", - "integrity": "sha1-cOUqjv9j3kxo4dCMXDfbVWEyQ0A=", + "version": "2.104.0", + "resolved": "https://registry.npmjs.org/aws-sdk/-/aws-sdk-2.104.0.tgz", + "integrity": "sha1-M14FUzgMCujdQ9Ebdtc9+5JvYzM=", "dev": true, "requires": { "buffer": "4.9.1", @@ -1960,7 +1960,7 @@ "integrity": "sha1-3AWYwb9T6GUuYy6PMWks4CLX3qk=", "dev": true, "requires": { - "aws-sdk": "2.102.0" + "aws-sdk": "2.104.0" } }, "number-is-nan": { diff --git a/package.json b/package.json index 929c3ef..e3ef268 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "devDependencies": { "@adonisjs/fold": "^4.0.2", "@adonisjs/sink": "^1.0.13", + "aws-sdk": "^2.104.0", "coveralls": "^2.13.1", "cz-conventional-changelog": "^2.0.0", "dotenv": "^4.0.0", @@ -47,7 +48,9 @@ ] }, "standard": { - "globals": ["use"] + "globals": [ + "use" + ] }, "dependencies": { "@adonisjs/generic-exceptions": "^1.0.0", diff --git a/src/Mail/Drivers/Ses.js b/src/Mail/Drivers/Ses.js new file mode 100644 index 0000000..3c47160 --- /dev/null +++ b/src/Mail/Drivers/Ses.js @@ -0,0 +1,54 @@ +'use strict' + +/* + * adonis-mail + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +const nodemailer = require('nodemailer') + +class SesDriver { + /** + * This method is called by mail manager automatically + * and passes the config object + * + * @method setConfig + * + * @param {Object} config + */ + setConfig (config) { + this.transporter = nodemailer.createTransport({ + SES: new (require('aws-sdk')).SES(config) + }) + } + + /** + * Send a message via message object + * + * @method send + * @async + * + * @param {Object} message + * + * @return {Object} + * + * @throws {Error} If promise rejects + */ + send (message) { + return new Promise((resolve, reject) => { + this.transporter.sendMail(message, (error, result) => { + if (error) { + reject(error) + } else { + resolve(result) + } + }) + }) + } +} + +module.exports = SesDriver diff --git a/src/Mail/Drivers/index.js b/src/Mail/Drivers/index.js index 1328ef2..f4e6493 100644 --- a/src/Mail/Drivers/index.js +++ b/src/Mail/Drivers/index.js @@ -12,5 +12,6 @@ module.exports = { smtp: require('./Smtp'), sparkpost: require('./SparkPost'), - mailgun: require('./Mailgun') + mailgun: require('./Mailgun'), + ses: require('./Ses') } diff --git a/test/ses.spec.js b/test/ses.spec.js new file mode 100644 index 0000000..cb47c7f --- /dev/null +++ b/test/ses.spec.js @@ -0,0 +1,41 @@ +'use strict' + +/* + * adonis-mail + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +require('dotenv').load() +const test = require('japa') +const { ses: Ses } = require('../src/Mail/Drivers') + +test.group('Ses', () => { + test('send email', async (assert) => { + const config = { + apiVersion: '2010-12-01', + accessKeyId: process.env.SES_KEY, + secretAccessKey: process.env.SES_SECRET, + region: 'us-west-2' + } + + const ses = new Ses() + ses.setConfig(config) + + const response = await ses.send({ + from: [process.env.SMTP_TO_EMAIL], + to: [{ name: 'virk', address: process.env.SMTP_TO_EMAIL }], + subject: 'Plain email', + html: '

Hello

', + attachments: [{ + filename: 'sample.txt', + content: 'Hello world' + }] + }) + + assert.isDefined(response.messageId) + }).timeout(0) +})