sendgrid-mail

The easiest way to send mail with sendgrid.

Usage no npm install needed!

<script type="module">
  import sendgridMail from 'https://cdn.skypack.dev/sendgrid-mail';
</script>

README

Mail for Sendgrid

Main repo: https://github.com/huned/nodejs-sendgrid-mail

This is the simplest way to send mail with sendgrid in node.js.

Features:

  • Use as a library or as a command line utility
  • Command line utility has a mail style interface and sensible defaults
  • Supports sending plain text and html emails
  • No bloat, few dependencies

Installation

npm install sendgrid-mail

Usage

sgSendmail = require('sendgrid-mail')

sgSendmail({
  // Required SendGrid API key
  apikey: 'your sendgrid api key',

  // Optional to/cc/bcc fields.
  // Specify at least one email address in any of these fields.
  to: ['John Doe <john.doe@example.com>'],
  cc: [],
  bcc: [],

  // Optional from/replyTo fields.
  // The from address must be a verified sender in your SendGrid account.
  from: 'Jane Doe <jane.doe@example.com>',
  replyTo: 'noreply@example.com',

  // Optional subject of the email
  subject: 'test subject',

  // Optional body of the email
  body: 'plain text body',

  // Optional content type of email body.
  contentType: 'text/html',

  // Optionally avoid sending empty emails
  suppressEmpty: true,
})

Command Line Usage

$ echo 'hello, world.' | sendgrid-mail -k <sendgrid api key> -s 'Test Subject' --from=sender@example.com recipient@example.com

You can specify options (see sendgrid-mail --help for more details)

-E
--suppressEmpty
    OPTIONAL
    Do not send messages with an empty body.

-s <subject>
--subject=<subject>
    OPTIONAL
    Specify subject on command line. (Only the first argument after the
    -s flag is used as a subject; be careful to quote subjects containing
    spaces.)

-c <cc-addr>
--cc=<cc-addr>
    OPTIONAL
    Send carbon copies to cc-addr list of users. The cc-addr argument
    should be a comma-separated list of email addresses.

-b <bcc-addr>
--bcc=<bcc-addr>
    OPTIONAL
    Send blind carbon copies to bcc-addr list of users. The bcc-addr
    argument should be a comma-separated list of email addresses.

The following options are specific to this program and used when calling
SendGrid's API v3 /mail/send endpoint.

-k <api key>
--key=<api key>
    REQUIRED
    Set a SendGrid API key. Can also be set by defining SENDGRID_API_KEY.

--from=<from-addr>
    REQUIRED
    Set the from-addr for outgoing messages. Can also be set by defining
    SENDGRID_MAIL_FROM.

    **IMPORTANT** from-addr must be a verified sender for your SendGrid
    account.

--replyTo=<replyto-addr>
    OPTIONAL
    Set the reply-to field for outgoing messages. Can also be set by
    defining SENDGRID_MAIL_REPLYTO. If unset, defaults to $REPLYTO.

--html
    OPTIONAL
    Indicate that mail body content type is "text/html". If unset, mail
    content type is "text/plain".

Author(s)

License

MIT

TODOs

  • tests
  • multipart email