Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Integration of nodemailer-sendgrid-transport
Browse files Browse the repository at this point in the history
I also refactored some of it such that it contains only the necessary
files, as well as follow the structure that the other sub-packages
follow.

This commit should help with the PRs:
- [NST#67](sendgrid/nodemailer-sendgrid-transport#67)
- [NST#64](sendgrid/nodemailer-sendgrid-transport#64)

And the following issues:
- [NST#65](sendgrid/nodemailer-sendgrid-transport#65)
- [NST#25](sendgrid/nodemailer-sendgrid-transport#25)

The problem I face despit this commit being here is that some
sub-packages require the old `sendgrid` package which works differently
then this one.
  • Loading branch information
Berkmann18 committed Jul 27, 2018
1 parent c7ffa40 commit 223b00d
Show file tree
Hide file tree
Showing 12 changed files with 1,001 additions and 0 deletions.
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('@sendgrid/nodemailer-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;
Loading

0 comments on commit 223b00d

Please sign in to comment.