npm install @sendgrid/mail

Twilio SendGrid NodeJS mail service

About @sendgrid/mail

The npm package "@sendgrid/mail" serves as a powerful tool for integrating email functionalities seamlessly into Node.js applications, leveraging the robust infrastructure provided by Twilio SendGrid. This node module simplifies the process of sending emails, managing templates, and handling transactional email operations with ease. Developers benefit from features like detailed analytics, which track email opens, clicks, and deliveries, helping to refine marketing strategies and improve communication effectiveness. Additionally, the package ensures high deliverability rates, making it an essential tool for businesses aiming to maintain strong customer relationships through reliable email communication.

To begin using this essential email solution in your Node.js projects, executing 'npm install @sendgrid/mail' is the first step. This command installs the package and all necessary dependencies, setting up a developer-friendly environment for email integration. The "@sendgrid/mail" package supports various email features, including attachments, multiple recipients, and custom headers, providing flexibility to meet diverse project requirements. By using this npm package, developers can also access Twilio SendGrid's powerful Web API for advanced email activities, making it a comprehensive choice for modern email solutions in application development.

With "@sendgrid/mail", developers can send bulk emails efficiently, which is crucial for applications like newsletters, promotional campaigns, and personalized customer updates. The integration of this package into Node.js applications not only enhances the functionality but also elevates the user experience by ensuring timely and relevant communication. This leads to improved user engagement and customer retention, underlining the importance of a dependable email service in today's digital landscape.

More from sendgrid

sendgrid npm packages

Find the best node modules for your project.

Search npm

@sendgrid/helpers

Twilio SendGrid NodeJS internal...

Read more
,

@sendgrid/client

Twilio SendGrid NodeJS API...

Read more
,

@sendgrid/mail

Twilio SendGrid NodeJS mail...

Read more
,

@sendgrid/eventwebhook

Twilio SendGrid NodeJS Event...

Read more
,

@sendgrid/inbound-mail-parser

Twilio SendGrid NodeJS inbound mail...

Read more
,

@sendgrid/contact-importer

Twilio SendGrid NodeJS contact...

Read more

Dependencies

Core dependencies of this npm package and its dev dependencies.

@babel/cli, @babel/core, @babel/node, chai, chai-as-promised, dirty-chai, eslint, istanbul, lerna, mocha, mocha-clean, mocha-sinon, moment, sinon, sinon-chai, typescript

Documentation

A README file for the @sendgrid/mail code repository. View Code

BuildStatus npm version

This package is part of a monorepo, please see this README for details.

Mail Service for the SendGrid v3 Web API

This is a dedicated service for interaction with the mail endpoint of the SendGrid v3 API.

Installation

Prerequisites

Obtain an API Key

Grab your API Key from the Twilio SendGrid UI.

Setup Environment Variables

Do not hardcode your Twilio SendGrid API Key into your code. Instead, use an environment variable or some other secure means of protecting your Twilio SendGrid API Key. Following is an example of using an environment variable.

Update the development environment with your SENDGRID_API_KEY, for example:

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. If you are unfamiliar with npm, see the npm docs. Npm comes installed with Node.js since node version 0.8.x, therefore, you likely already have it.

npm install --save @sendgrid/mail

You may also use yarn to install.

yarn add @sendgrid/mail

Verify Sender Identity

Verify an email address or domain in the Sender Authentication tab. Without this you will receive a 403 Forbidden response when trying to send mail.

Quick Start, Hello Email

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

For more complex use cases, please see USE_CASES.md.

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com', // Use the email address or domain you verified above
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
//ES6
sgMail
  .send(msg)
  .then(() => {}, error => {
    console.error(error);

    if (error.response) {
      console.error(error.response.body)
    }
  });
//ES8
(async () => {
  try {
    await sgMail.send(msg);
  } catch (error) {
    console.error(error);

    if (error.response) {
      console.error(error.response.body)
    }
  }
})();

After executing the above code, you should have an email in the inbox of the recipient. You can check the status of your email in the UI. Alternatively, we can post events to a URL of your choice using our Event Webhook. This gives you data about the events that occur as Twilio SendGrid processes your email.

Troubleshooting

Please see our troubleshooting guide for common library issues.

Announcements

All updates to this library are documented in our CHANGELOG and releases.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

About

@sendgrid/mail is maintained and funded by Twilio SendGrid, Inc. The names and logos for @sendgrid/mail are trademarks of Twilio SendGrid, Inc.

If you need help installing or using the library, please check the Twilio SendGrid Support Help Center.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

Twilio SendGrid Logo