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

BoxLang 1.0.0 Beta 7 Launched

BoxLang 1.0.0 Beta 7 Launched

We are pleased to announce the release of BoxLang 1.0.0-Beta 7! This latest beta version includes improvements and essential bug fixes, but more importantly it certifies the execution of ColdBox HMVC and TestBox.

What is BoxLang?

BoxLang is a modern dynamic JVM language that can be deployed on multiple runtimes: operating system (Windows/Mac/*nix/Embedded), web server, lambda, iOS, android, web assembly, and more. BoxLang combines many features from different progr

Luis Majano
Luis Majano
July 26, 2024
New BoxLang Feature: Java Method References and Higher-Order Functions

New BoxLang Feature: Java Method References and Higher-Order Functions

We’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. CFML has never let you do these things, making Java Interop feel like a second-class citizen. But with BoxLang, we’re elevating Java integration to a new level.

Maria Jose Herrera
Maria Jose Herrera
July 26, 2024
Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Are you a CFML developer looking to take your skills to the next level? Look no further than the ColdBox from Zero to Hero Virtual Live Training! This intensive two-day course will equip you with the knowledge and expertise to build robust and scalable applications using ColdBox 7, the latest version of the most popular CFML MVC framework.

What You'll Learn:

  • Master the Fun...

Cristobal Escobar
Cristobal Escobar
July 24, 2024