BoxLang 🚀 A New JVM Dynamic Language Learn More...

MailjetProtocol

v1.2.0 Modules

Mailjet Protocol

Build Status

A cbmailservices protocol for sending email via Mailjet.

Based on the Send Grid Protocol (https://github.com/elpete/send-grid-protocol) from Eric Peterson

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 Mailjet 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 = "mailjetprotocol.models.protocols.MailjetProtocol",
		properties = {
			apiKey = application.system.getProperty( "MAILJET_KEY" ),
			apiSecret = application.system.getProperty( "MAILJET_SECRET" )
		}
	}
};

Template Emails

To send an email using a Mailjet 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 Mailjet:

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 @placeholders@, 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" ] );

v1.1.0

12 Jan 2022

  • cbmailservices 2.* compatibility

v1.0.0

19 Oct 2021

  • initial Release

$ box install mailjet-protocol

No collaborators yet.
     
  • {{ getFullDate("2021-10-20T13:25:34Z") }}
  • {{ getFullDate("2022-01-11T23:31:48Z") }}
  • 1,111
  • 5