Blog

Eric Peterson

December 14, 2017

Spread the word


Share your thoughts

ForgeBox is your first stop for when you need some new functionality for your application, be that a utility library, an SDK, a framework, or something else. Today we'll take a look at one of the tools for helping manage sending emails — [cbmailservices](https://www.forgebox.io/view/cbmailservices).

cbmailservices

cbmailservices provides a protocol-based service to send emails as well as mail merge functionality to easily customize emails.

But what about good 'ole cfmail? Well, you can still use it! cbmailservices ships with a CFMailProtocol which will delegate back to cfmail for sending your emails.

You might wonder why you would even use this module? Let me give you two reasons:

Mail Merge

Mail merging lets you specify an email template with tokens that can be replaced per email. This lets you tailor your emails to each user, company, or use case you find.

Here's an example of the mail merging capabilitys of cbmailservices:

var mailService = getInstance( "mailService@cbmailservices" );
var mail = mailService.newMail(
    to = "john@example.com",
    subject = "Mail Services Rock",
    bodyTokens = {
        user = "John",
        product = "cbmailservices",
        link = event.buildLink( "view.cbmailservices" )
    },
    body = "
        

Dear @user@,

Thank you for downloading @product@. Have a great day!

@link@

" ); mailService.send( mail );

This will send the following email:

<p>Dear John,</p>
<p>Thank you for downloading cbmailservices.  Have a great day!</p>
<p><a href="https://www.forgebox.io/view/cbmailservices">https://www.forgebox.io/view/cbmailservices</a></p>

Protocols

A cbmailservices protocol defines how to send an email. This can be through good 'ole cfmail, third party mail services like Postmark and SendGrid, or even just locally to your file system or the console output.

Even better, with ColdBox's environment detection, you can set different protocols for each environment. Here's what a sample config/ColdBox.cfc file might look like:

function configure() {
    mailsettings = {
        tokenMarker = "@",
        protocol = {
            class = "cbmailservices.models.protocols.PostmarkProtocol",
            properties = {
                APIKey = getSystemSetting( "POSTMARK_API_KEY" )
            }
        }
    };
}

function development() {
    mailsettings.protocol = {
        class = "cbmailservices.models.protocols.FileProtocol",
        properties = {
            filePath = "logs",
            autoExpand = true
        }
    }
}

This configuration will log emails locally to the file system in development and use Postmark to send emails for real in production. Nice!

You can write your own protocols as well and even publish them on ForgeBox like the SendGrid protocol for your favorite email service.

Wrap Up

So head on over to your local CommandBox terminal and give cbmailservices a go!

Add Your Comment

Recent Entries

ColdBox 7.2.0 Released

ColdBox 7.2.0 Released

ColdBox, a widely used development platform for ColdFusion (CFML), has unveiled version 7.2. Packed with compelling new features, bug fixes, and enhancements, this release is designed to empower developers by boosting productivity, refining scheduled task capabilities, and enhancing the overall reliability and efficiency of application development. This article will delve into the key highlights of ColdBox 7.2 and elucidate how these advancements can positively impact developers in their daily coding endeavors.

Luis Majano
Luis Majano
November 20, 2023
Into the Box 2023 Series on CFCast

Into the Box 2023 Series on CFCast

Excitement is in the air as we unleash the highly anticipated ITB 2023 series exclusively for our valued CFCast subscribers – and the best part? It's FREE for CFCast members! Now is the perfect time if you haven't joined the CFCast community yet. Plus, we've got an incredible End-of-Year deal that's too good to miss

Maria Jose Herrera
Maria Jose Herrera
November 20, 2023
Ortus Deals are Finally Here!

Ortus Deals are Finally Here!

The much-anticipated Ortus End-of-the-Year Sale has arrived, and it's time to elevate your development experience! Whether you're a seasoned developer, a tech enthusiast, or someone on the lookout for top-notch projects, Ortus has something special in store for you. Brace yourself for incredible discounts across a wide array of products and services, including Ortus annual events, books, cutting-edge services, and more.

Maria Jose Herrera
Maria Jose Herrera
November 15, 2023