Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrated nodemailer-sendgrid-transport as a package #706

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ test/config.js
*.log
.vscode/
prism_darwin_amd64
docs/
docs/
.idea/
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
"url": "git://github.com/sendgrid/sendgrid-nodejs.git"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"chai": "^4.0.1",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"eslint": "^4.19.1",
"istanbul": "^1.0.0-alpha.2",
"eslint": "^5.2.0",
"istanbul": "^0.4.5",
"lerna": "^2.0.0-rc.5",
"mocha": "^3.4.2",
"mocha": "^5.2.0",
"mocha-clean": "^1.0.0",
"mocha-sinon": "^2.0.0",
"mocha-sinon": "^2.1.0",
"moment": "^2.19.3",
"sinon": "^2.3.2",
"sinon-chai": "^2.10.0",
"sinon": "^6.1.4",
"sinon-chai": "^3.2.0",
"typescript": "~2.4.2"
},
"scripts": {
Expand Down Expand Up @@ -51,7 +51,7 @@
"test": "test"
},
"dependencies": {
"chai": "^4.0.1",
"chai": "^4.1.2",
"esdoc": "^1.0.3",
"esdoc-coverage-plugin": "^1.1.0",
"esdoc-type-inference-plugin": "^1.0.1"
Expand Down
134 changes: 134 additions & 0 deletions packages/nodemailer-transport/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
[![BuildStatus](https://travis-ci.org/sendgrid/sendgrid-nodejs.svg?branch=master)](https://travis-ci.org/sendgrid/sendgrid-nodejs)
[![npm version](https://badge.fury.io/js/%40sendgrid%2Fclient.svg)](https://www.npmjs.com/org/sendgrid)
[![Email Notifications Badge](https://dx.sendgrid.com/badge/nodejs)](https://dx.sendgrid.com/newsletter/nodejs)

**This package is part of a monorepo, please see [this README](https://github.com/sendgrid/sendgrid-nodejs/blob/master/README.md) for details.**

# Nodemailer Transport Service for the Sendgrid v3 Web API
This is a dedicated transport service for interaction with the nodemailer service of the [Sendgrid v3 API](https://sendgrid.com/docs/API_Reference/api_v3.html).

To be notified when this package is updated, please subscribe to email [notifications](https://dx.sendgrid.com/newsletter/nodejs) for releases and breaking changes.

# Installation

## Prerequisites

- Node.js version 6, 7 or 8
- A SendGrid account, [sign up for free](https://sendgrid.com/free?source=sendgrid-nodejs) to send up to 40,000 emails for the first 30 days or check out [our pricing](https://sendgrid.com/pricing?source=sendgrid-nodejs).
- Nodemailer

## Obtain an API Key

Grab your API Key from the [SendGrid UI](https://app.sendgrid.com/settings/api_keys).

## Setup Environment Variables

Do not hard code your [SendGrid API Key](https://app.sendgrid.com/settings/api_keys) into your code. Instead, use an environment variable or some other secure means of protecting your SendGrid API Key. Following is an example of using an environment variable.

Update the development environment with your [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys), for example:

```bash
echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
```

## Install Package

The following recommended installation requires [npm](https://npmjs.org/). If you are unfamiliar with npm, see the [npm docs](https://npmjs.org/doc/). Npm comes installed with Node.js since node version 0.8.x therefore you likely already have it.

```sh
npm install --save @sendgrid/nodemailer-transport
```

You may also use [yarn](https://yarnpkg.com/en/) to install.

```sh
yarn add @sendgrid/nodemailer-transport
```

<a name="quick-start"></a>
# Quick Start, Hello Email

The following is the minimum needed code to send an simple email. Use this example, and modify the `to` and `from` variables:

For more complex use cases, please see [USE_CASES.md](https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/USE_CASES.md).

```js
const nodemailer = require('nodemailer');
const sgTransport = require('../src/sendgrid-transport.js');

let options = {
auth: {
api_user: process.env['SENDGRID_USERNAME'],
api_key: process.env['SENDGRID_PASSWORD']
}
}

let mailer = nodemailer.createTransport(sgTransport(options));

let email = {
to: ['[email protected]', '[email protected]'],
from: '[email protected]',
subject: 'Hi there',
text: 'Awesome sauce',
html: '<b>Awesome sauce</b>',
attachments: [
{
filename: 'test.txt',
path: __dirname + '/test.txt'
}
]
};

mailer.sendMail(email, function(err, res) {
if (err) {
console.log(err)
}
console.log(res);
});
```
*test.txt*:
```text
Hello text
```

After executing the above code, you should have an email in the inbox of the to recipient. You can check the status of your email [in the UI](https://app.sendgrid.com/email_activity?). Alternatively, we can post events to a URL of your choice using our [Event Webhook](https://sendgrid.com/docs/API_Reference/Webhooks/event.html). This gives you data about the events that occur as SendGrid processes your email.

<a name="troubleshooting"></a>
# Troubleshooting

Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-nodejs/blob/master/TROUBLESHOOTING.md) for common library issues.

<a name="announcements"></a>
# Announcements

All updates to this library are documented in our [CHANGELOG](https://github.com/sendgrid/sendgrid-nodejs/blob/master/CHANGELOG.md) and [releases](https://github.com/sendgrid/sendgrid-nodejs/releases). You may also subscribe to email [release notifications](https://dx.sendgrid.com/newsletter/nodejs) for releases and breaking changes.

<a name="roadmap"></a>
# Roadmap

If you are interested in the future direction of this project, please take a look at our open [issues](https://github.com/sendgrid/sendgrid-nodejs/issues) and [pull requests](https://github.com/sendgrid/sendgrid-nodejs/pulls). We would love to hear your feedback!

<a name="contribute"></a>
# How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our [CONTRIBUTING](https://github.com/sendgrid/sendgrid-nodejs/blob/master/CONTRIBUTING.md) guide for details.

* [Feature Request](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#feature-request)
* [Bug Reports](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#submit-a-bug-report)
* [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-nodejs/tree/master/CONTRIBUTING.md#improvements-to-the-codebase)

<a name="troubleshooting"></a>
# Troubleshooting

Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-nodejs/blob/master/TROUBLESHOOTING.md) for common library issues.

<a name="about"></a>
# About

@sendgrid/nodemailer-transport is guided and supported by the SendGrid [Developer Experience Team](mailto:[email protected]).

@sendgrid/nodemailer-transport is maintained and funded by SendGrid, Inc. The names and logos for @sendgrid/nodemailer-transport are trademarks of SendGrid, Inc.

![SendGrid Logo](https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)
63 changes: 63 additions & 0 deletions packages/nodemailer-transport/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## Usage
Install via npm.

npm install nodemailer-sendgrid-transport

Require the module and initialize it with your SendGrid credentials.

```javascript
var nodemailer = require('nodemailer');
var sgTransport = require('nodemailer-sendgrid-transport');

// api key https://sendgrid.com/docs/Classroom/Send/api_keys.html
var options = {
auth: {
api_key: 'SENDGRID_APIKEY'
}
}

// or

// username + password
var options = {
auth: {
api_user: 'SENDGRID_USERNAME',
api_key: 'SENDGRID_PASSWORD'
}
}

var mailer = nodemailer.createTransport(sgTransport(options));
```

Note: We suggest storing your SendGrid username and password as enviroment variables.

Create an email and send it off!

```javascript
var email = {
to: ['[email protected]', '[email protected]'],
from: '[email protected]',
subject: 'Hi there',
text: 'Awesome sauce',
html: '<b>Awesome sauce</b>'
};

mailer.sendMail(email, function(err, res) {
if (err) {
console.log(err)
}
console.log(res);
});
```

<a name="deploying"></a>
## Deploying

* Confirm tests pass
* Bump the version in `README.md`, `package.json`, `test/sendgrid-transport-test.js`
* Update `CHANGELOG.md`
* Confirm tests pass
* Commit `Version bump vX.X.X`
* `npm publish`
* Push changes to GitHub
* Release tag on GitHub `vX.X.X`
2 changes: 2 additions & 0 deletions packages/nodemailer-transport/USE_CASES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1. Using the Sendgrid service via NodeMailer without having to set that up such that it would remove some snippet of code and keep projects as DRY-friendly as possible.
In other words, using a sendgrid package specialised in NodeMailer.
3 changes: 3 additions & 0 deletions packages/nodemailer-transport/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import NodemailerTransport = require("@sendgrid/nodemailer-transport/src/sendgrid-transport");

export = NodemailerTransport;
5 changes: 5 additions & 0 deletions packages/nodemailer-transport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const sgTransport = require('./src/sendgrid-transport');

module.exports = sgTransport;
40 changes: 40 additions & 0 deletions packages/nodemailer-transport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@sendgrid/nodemailer-transport",
"description": "SendGrid transport for Nodemailer",
"version": "0.3.0",
"author": "SendGrid <[email protected]> (sendgrid.com)",
"contributors": [
"Eddie Zaneski <[email protected]",
"Will Smidlein <[email protected]>"
],
"license": "MIT",
"homepage": "https://sendgrid.com",
"repository": {
"type": "git",
"url": "git://github.com/sendgrid/sendgrid-nodejs.git"
},
"main": "src/sendgrid-transport.js",
"engines": {
"node": ">=6.0.0"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"Nodemailer",
"transport",
"SendGrid"
],
"dependencies": {
"chain": "^0.1.3",
"@sendgrid/mail": "^6.3.1"
},
"tags": [
"http",
"rest",
"api",
"transport",
"nodemailer",
"sendgrid"
]
}
Loading