Skip to content

Commit

Permalink
Mail: Pass configuration in transport instead of the Mail class
Browse files Browse the repository at this point in the history
Signed-off-by: Fadion Dashi <jonidashi@gmail.com>
  • Loading branch information
fadion committed Mar 20, 2018
1 parent cc86baf commit d60a393
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
14 changes: 7 additions & 7 deletions packages/mail/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ We'll start with an exhaustive example that includes pretty much every option.
```javascript
const { Mail, Transport } = require('@sapphirejs/mail')

const config = {/* nodemailer configuration */}
const mail = new Mail(config, new Transport.SMTP())
const config = { host: 'smtp.example.com' }
const mail = new Mail({}, new Transport.SMTP(config))
await mail.send('<p>Hi</p>', message => {
message
.from('from@domain.com')
Expand All @@ -40,10 +40,10 @@ await mail.send({ html: '<p>Hi</p>', text: 'Hi' }, /* rest of the message */)

### Global "from" Header

When the "from" header is passed in the config, it will be automatically included in every mail instance. Off course it also be overriden with the `from` function.
When the "from" header is passed as `Mail`'s config, it will be automatically included in every mail instance. Off course it also be overriden with the `from` function.

```javascript
let config = { from: 'from@domain.com', /* other config options */ }
const mail = new Mail({ from: 'from@domain.com' }, new Transport.SMTP(config))
```

### Name, Email Format
Expand Down Expand Up @@ -80,7 +80,7 @@ The same applies to `header`, `attachment`, and `alternative`.

```javascript
try {
const mail = new Mail(config, new Transport.SMTP())
const mail = new Mail({}, new Transport.SMTP(config))
const result = await mail
.send('<p>Hello</p>', message => {
message
Expand Down Expand Up @@ -108,7 +108,7 @@ const config = {
}
}

const mail = new Mail(config, new Transport.SMTP())
const mail = new Mail({}, new Transport.SMTP(config))
```

### SES Transport
Expand All @@ -122,5 +122,5 @@ const config = {{
region: 'us-east-1'
}

const mail = new Mail(config, new Transport.SES())
const mail = new Mail({}, new Transport.SES(config))
```
2 changes: 1 addition & 1 deletion packages/mail/lib/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Mail {
if (validationError)
throw new MissingMailParams(validationError)

return this._transport.send(this._config, messageWithBody)
return this._transport.send(messageWithBody)
.then(info => {
return info
})
Expand Down
11 changes: 9 additions & 2 deletions packages/mail/lib/transports/ses.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ const aws = require('aws-sdk')
* @class SES
*/
class SES {
/**
* @param {Object} config
*/
constructor(config) {
this._config = config
}

/**
* Sends the message.
*
Expand All @@ -15,9 +22,9 @@ class SES {
* @param {Object} message
* @returns {Prommise}
*/
send(config, message) {
send(message) {
const transport = nodemailer.createTransport({
SES: new aws.SES(config)
SES: new aws.SES(this._config)
})
return transport.sendMail(message)
}
Expand Down
12 changes: 9 additions & 3 deletions packages/mail/lib/transports/smtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ const nodemailer = require('nodemailer')
* @class SMTP
*/
class SMTP {
/**
* @param {Object} config
*/
constructor(config) {
this._config = config
}

/**
* Sends the message.
*
* @public
* @param {Object} config
* @param {Object} message
* @returns {Prommise}
*/
send(config, message) {
const transport = nodemailer.createTransport(config)
send(message) {
const transport = nodemailer.createTransport(this._config)
return transport.sendMail(message)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mail/test/mailer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MockTransport {
this._shouldErr = shouldErr
}

send(config, message) {
send(message) {
return new Promise((resolve, reject) => {
if (this._shouldErr) reject('error')
// Transport isn't supposed to return the message
Expand Down

0 comments on commit d60a393

Please sign in to comment.