# sails-hook-email

> Email hook for Sails

Latest version **0.12.1** (published 2015-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install sails-hook-email
pnpm add sails-hook-email
yarn add sails-hook-email
bun add sails-hook-email
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.12.1 |
| Published | 2015-08-18 |
| First published | 2014-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+20 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 66 |
| Author | Balderdash |
| Maintainers | sgress454, balderdashy, particlebanana |

## Links

- npm: https://www.npmjs.com/package/sails-hook-email
- Repository: https://github.com/balderdashy/sails-hook-email
- Homepage: https://github.com/balderdashy/sails-hook-email#readme
- Issues: https://github.com/balderdashy/sails-hook-email/issues
- npm.io page: https://npm.io/package/sails-hook-email

## Dependencies (6)

- [ejs](https://npm.io/package/ejs.md) ^2.3.1
- [async](https://npm.io/package/async.md) 0.9.0
- [lodash](https://npm.io/package/lodash.md) 3.2.0
- [nodemailer](https://npm.io/package/nodemailer.md) ^1.3.4
- [nodemailer-smtp-pool](https://npm.io/package/nodemailer-smtp-pool.md) ^1.1.1
- [nodemailer-html-to-text](https://npm.io/package/nodemailer-html-to-text.md) ^1.0.1

## Recent versions

- 0.12.1 (latest) — 2015-08-18
- 0.12.0 — 2015-07-08
- 0.11.0 — 2015-06-16
- 0.10.3 — 2014-10-28
- 0.10.2 — 2014-09-23
- 0.10.1 — 2014-09-22
- 0.10.0 — 2014-09-22

## README

# sails-hook-email

[![Dependency Status](https://david-dm.org/balderdashy/sails-hook-email.svg)](https://david-dm.org/balderdashy/sails-hook-email)

Email hook for [Sails JS](http://sailsjs.org), using [Nodemailer](https://github.com/andris9/Nodemailer/blob/v1.3.4/README.md)

*Note: This requires Sails v0.10.6+.*

### Installation

`npm install sails-hook-email`

### Usage

`sails.hooks.email.send(template, data, options, cb)`

Parameter      | Type                | Details
-------------- | ------------------- |:---------------------------------
template       | ((string))          | Relative path from `templateDir` (see "Configuration" below) to a folder containing email templates.
data           | ((object))          | Data to use to replace template tokens
options        | ((object))          | Email sending options (see [Nodemailer docs](https://github.com/andris9/Nodemailer/blob/v1.3.4/README.md#e-mail-message-fields))
cb             | ((function))        | Callback to be run after the email sends (or if an error occurs).

### Configuration

By default, configuration lives in `sails.config.email`.  The configuration key (`email`) can be changed by setting `sails.config.hooks['sails-hook-email'].configKey`.

Parameter      | Type                | Details
-------------- | ------------------- |:---------------------------------
service        | ((string)) | A "well-known service" that Nodemailer knows how to communicate with (see [this list of services](https://github.com/andris9/nodemailer-wellknown/blob/v0.1.5/README.md#supported-services))
auth | ((object)) | Authentication object as `{user:"...", pass:"..."}`
transporter | ((object)) | Custom transporter passed directly to nodemailer.createTransport (overrides service/auth) (see [Available Transports](https://github.com/andris9/Nodemailer/blob/v1.3.4/README.md#available-transports))
templateDir | ((string)) | Path to view templates relative to `sails.config.appPath` (defaults to `views/emailTemplates`)
from | ((string)) | Default `from` email address
testMode | ((boolean)) | Flag indicating whether the hook is in "test mode".  In test mode, email options and contents are written to a `.tmp/email.txt` file instead of being actually sent.  Defaults to `true`.
alwaysSendTo | ((string)) | If set, all emails will be sent to this address regardless of the `to` option specified.  Good for testing live emails without worrying about accidentally spamming people.

### Templates

Templates are generated using your configured Sails [View Engine](http://sailsjs.org/#!/documentation/concepts/Views/ViewEngines.html), allowing for multiple template engines and layouts.  If Sails Views are disabled, will fallback to EJS templates. To define a new email template, create a new folder with the template name inside your `templateDir` directory, and add an **html.ejs** file inside the folder (substituting .ejs for your template engine).  You may also add an optional `text.ejs` file; if none is provided, Nodemailer will attempt to create a text version of the email based on the html version.

### Example

Given the following **html.ejs** file contained in the folder **views/emailTemplates/testEmail**:

```
<p>Dear <%=recipientName%>,</p>
<br/>
<p><em>Thank you</em> for being a friend.</p>
<p>Love,<br/><%=senderName%></p>
```

executing the following command (after [configuring for your email service](https://github.com/balderdashy/sails-hook-email/#configuration) and turning off test mode) :

```
sails.hooks.email.send(
  "testEmail",
  {
    recipientName: "Joe",
    senderName: "Sue"
  },
  {
    to: "joe@example.com",
    subject: "Hi there"
  },
  function(err) {console.log(err || "It worked!");}
)
```

will result in the following email being sent to `joe@example.com`

> Dear Joe,
>
> *Thank you* for being a friend.
>
> Love,
>
> Sue

with an error being printed to the console if one occurred, otherwise "It worked!".

---
_Source: https://npm.io/package/sails-hook-email · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
