Skip to content

Commit

Permalink
feat(driver): add Ses driver
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 26, 2017
1 parent 99ff8ff commit 7d13d9e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ SPARKPOST_API_KEY=
SPARKPOST_FROM_EMAIL=
MAILGUN_API_KEY=
MAILGUN_DOMAIN=
SES_KEY=
SES_SECRET=
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -47,7 +48,9 @@
]
},
"standard": {
"globals": ["use"]
"globals": [
"use"
]
},
"dependencies": {
"@adonisjs/generic-exceptions": "^1.0.0",
Expand Down
54 changes: 54 additions & 0 deletions src/Mail/Drivers/Ses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict'

/*
* adonis-mail
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* 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
3 changes: 2 additions & 1 deletion src/Mail/Drivers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
module.exports = {
smtp: require('./Smtp'),
sparkpost: require('./SparkPost'),
mailgun: require('./Mailgun')
mailgun: require('./Mailgun'),
ses: require('./Ses')
}
41 changes: 41 additions & 0 deletions test/ses.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

/*
* adonis-mail
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* 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: '<h2> Hello </h2>',
attachments: [{
filename: 'sample.txt',
content: 'Hello world'
}]
})

assert.isDefined(response.messageId)
}).timeout(0)
})

0 comments on commit 7d13d9e

Please sign in to comment.