FORGEBOX Enterprise 🚀 - Take your ColdFusion (CFML) Development to Modern Times! Learn More...

SendGridProtocol

Send Grid Protocol

Build Status

A cbmailservices protocol for sending email via Send Grid

In cbmailservices parlance, a protocol is a method of sending an email. Protocols can be switched out based on environment settings making it easy to log a mail to a file in development, send it to an in-memory store for asserting against in tests, and sending to real services in production.

Configuration

You can configure SendGrid as your protocol inside your mailsettings in your config/ColdBox.cfc. It is recommended you store your API key outside version control in either server ENV settings or Java properties. This approach also lets you easily swap out dev and production keys based on the environment.

mailsettings = {
	from = "[email protected]",
	tokenMarker = "@",
	protocol = {
		class = "sendgridprotocol.models.protocols.SendGridProtocol",
		properties = {
			apiKey = application.system.getProperty( "SEND_GRID_KEY" )
		}
	}
};

Template Emails

To send an email using a SendGrid template, you need to set the mail type to template:

var mail = mailService.newMail(
    to = user.getEmail(),
    subject = "Welcome to my site!",
    type = "template"
);

Then set the body to the template id in SendGrid:

mail.setBody( templateId );

Any tokens to be parsed by the template can be set as normal:

mail.setBodyTokens( {
    "[%username%]" = interceptData.user.getUsername()
} );

Plain Emails

To send a plain text email, set the type to plain:

var mail = mailService.newMail(
    to = user.getEmail(),
    subject = "Welcome to my site!",
    type = "plain"
);

The body of the email is what will be sent.

mail.setBody( "My plain text email here.  I can still use @[email protected], of course." );

Categories

You can attach a list or array of categories for your email by setting them on the additionalInfo.categories field of the mail.

mail.setAdditionalInfoItem( "categories", "marketing" );
mail.setAdditionalInfoItem( "categories", "lists,of,categories" );
mail.setAdditionalInfoItem( "categories", [ "or", "as", "an", "array" ] );

Custom Arguments

You can attach a struct of custom_args by setting them on the additionalInfo.customArgs field of the mail.

mail.setAdditionalInfoItem( "customArgs", {
    "orderNumber" = toString( orderNumber )
} );

Note: Due to a bug in SendGrid, numeric values are not allowed. Convert any numeric values to strings (toString) to avoid errors.

v2.0.1

07 Feb 2018 — 12:47: 35 UTC

fix

  • api: Fix for cc and bcc and return messageid (#4) (ef87657)

v2.0.0

06 Feb 2018 — 03:50: 57 UTC

BREAKING

  • build: Remove ACF10 support (7ba4ba2)

chore

  • build: Add semantic release (74e00da)

feat

  • api: Add cc, bcc, and replyto properties (0b6f6bb)

fix

  • compat: Fix Adobe compatibility (8010118)

Here are all the versions for this package. Please note that you can leverage CommandBox package versioning to install any package you like. Please refer to our managing package version guide for more information.

Version Created Last Update Published By Stable Download
2.0.1 Feb 07 2018 06:47 AM Feb 07 2018 06:47 AM Eric Peterson (elpete)
2.0.0 Feb 05 2018 09:49 PM Feb 05 2018 09:49 PM Eric Peterson (elpete)
1.5.0 Dec 11 2017 04:57 PM Dec 11 2017 04:57 PM Eric Peterson (elpete)
1.3.0 Dec 01 2017 10:49 PM Dec 01 2017 10:49 PM Eric Peterson (elpete)
1.2.1 Aug 21 2017 10:24 AM Aug 21 2017 10:24 AM Eric Peterson (elpete)
1.2.0 Jul 31 2017 08:56 PM Jul 31 2017 08:56 PM Eric Peterson (elpete)
1.1.0 Mar 09 2017 09:53 PM Mar 09 2017 09:53 PM Eric Peterson (elpete)
1.0.0 Sep 08 2016 05:16 PM Sep 08 2016 05:17 PM Eric Peterson (elpete)

 

No collaborators yet.
     
  • Sep 08 2016 05:16 PM
  • Feb 07 2018 06:47 AM
  • 962
  • 13
  • 2596