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

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Unfortunately, we often hear this message from clients who thought it would never happen to them... until it did. Some believed they could delay the expense of Implementing ColdFusion security best practices for one year, while others were tempted to put it off for just a few months. However, in today's rapidly evolving digital landscape, the security of web applications, including ColdFusio...

Cristobal Escobar
Cristobal Escobar
April 16, 2024
Ortus March Newsletter

Ortus March Newsletter

Welcome to Ortus Solutions’ monthly roundup, where we're thrilled to showcase cutting-edge advancements, product updates, and exciting events! Join us as we delve into the latest innovations shaping the future of technology.

Maria Jose Herrera
Maria Jose Herrera
April 01, 2024
Into the Box 2024 Last Early Bird Days!

Into the Box 2024 Last Early Bird Days!

Time is ticking, with less than 60 days remaining until the excitement of Into the Box 2024 unfolds! Don't let this golden opportunity slip away; our exclusive Early Bird Pricing is here for a limited time only, available until March 31st. Why wait? Secure your seat now and take advantage of this steal!

Maria Jose Herrera
Maria Jose Herrera
March 20, 2024