Blog

Victor Campos

October 21, 2025

Spread the word


Share your thoughts

Using cbMailServices: Tokens, Protocols, and Logs

Introduction

In our last post, we walked through configuring email settings in ColdBox using cbMailServices. Now it’s time for the fun part—sending emails! ColdBox’s mail services are powerful, and with just a few lines of code, you can build emails with token replacements, send them using various protocols, and log them when needed.

Let’s explore how to send emails safely without the risk of accidentally overwhelming your mail server!

1. Injecting the Mail Service

To get started, you need to inject the mail service into your handlers, services, or wherever you need it in your ColdBox application.

Example of Injecting the Mail Service:

// Injection
property name="mailServices" inject="mailService@cbmailservices";

Alternatively, you can retrieve it via getInstance() if you prefer a more direct approach:

var mailServices = getInstance("mailService@cbmailservices");

2. Building a Mail Object

Once you have access to the mail service, you can build a new mail object. This is similar to creating a cfmail tag, but with added flexibility through ColdBox's mail service.

Example of Building an Email:

var oMail = mailServices.newMail(
    to      = rc.toMail,
    subject = "Mail Services Rock!",
    bodyTokens = {}
);

The newMail() function allows you to pass the same attributes as the cfmail tag, while also pulling from your config/ColdBox.cfc settings for defaults.

3. Using Body Tokens for Dynamic Content

cbMailServices also allows you to use body tokens to dynamically replace placeholders in your email content. This is perfect for personalized emails or creating dynamic content on the fly.

Example with Tokenized Email Content:

bodyTokens = {
    user    = "Luis",
    product = "ColdBox",
    link    = event.buildLink('home')
};

oMail.setBody("
    <p>Dear @user@,</p>
    <p>Thank you for downloading @product@, have a great day!</p>
    <p><a href='@link@'>@link@</a></p>
");

In this example, the @token@ placeholders in the email body are automatically replaced with the corresponding values in the bodyTokens struct.


4. Sending the Mail

Once the mail is built and the tokens are set, you can send it using the send() method. This method returns a struct containing the result of the email send process.

Example of Sending the Email:

var results = mailServices.send(oMail);

This is where ColdBox shines—whether you’re sending an email to an actual recipient or writing it to a log file (as in development mode), the process is seamless.

5. Logging Emails in Development Mode

In development, it’s often useful to log emails rather than send them. As configured earlier, cbMailServices allows you to easily write email data to log files for testing.

This ensures you can debug and test your mail service without causing havoc by accidentally sending hundreds of emails.

Conclusion

Using ColdBox's cbMailServices, you can take full control of your email-sending process. With built-in support for various protocols, tokenized content, and environment-specific behavior, sending emails becomes safe, efficient, and easy to manage. Whether you're building personalized emails for users or simply logging test emails, ColdBox has your back!

If you missed the first part of this series, make sure to check it out to get up to speed on configuring cbMailServices in your ColdBox applications. Happy coding!

Learn More ColdBox Tips and Tricks

Stay tuned for more ColdBox tips in our ongoing series. if you want to learn more than 10 free tips and tricks make sure to get your “ColdBox 102 tips and tricks book” We have a variety of formats you can choose from.

Get your Copy

Join the Ortus Community

Be part of the movement shaping the future of web development. Stay connected and receive the latest updates on, product launches, tool updates, promo services and much more.

Subscribe to our newsletter for exclusive content.

Follow Us on Social media and don’t miss any news and updates:

Add Your Comment

Recent Entries

Your ColdFusion Upgrade Passed. Did Your Business Workflows?

Your ColdFusion Upgrade Passed. Did Your Business Workflows?

The server restarted. The administrator shows the expected version. Your homepage loads, and a few familiar screens look fine.

Would you tell the business the upgrade is complete?

Consider an illustrative scenario: a customer submits an order after an upgrade. The application saves it and displays a confirmation page. But the confirmation email never arrives, and the scheduled process that sends the order to fulfillment has not completed.

From the browser, the transaction...

Maria Jose Herrera
Maria Jose Herrera
October 22, 2026
Updating Adobe ColdFusion isn't always just installing the latest patch

Updating Adobe ColdFusion isn't always just installing the latest patch

Keeping Adobe ColdFusion updated is an important part of maintaining a secure and reliable application environment.

But applying a ColdFusion update is not always as simple as installing the latest patch and restarting the server.

Updates can include security fixes, library upgrades, JVM-related changes, changes to the ColdFusion Administrator, updated packages, bug fixes, and changes in runtime behavior. Most updates will not cause problems for most applications. But when you'r...

Cristobal Escobar
Cristobal Escobar
October 21, 2026
BoxLang AI 3.6: Your Agent Now Has Eyes, Ears, and a Voice

BoxLang AI 3.6: Your Agent Now Has Eyes, Ears, and a Voice

Ortus Solutions today announces BoxLang AI 3.6, the release that connects the AI framework to the rest of the BoxLang productivity platform. Your agents can now drive a real browser via bx-playwright, speak and master audio via bx-audio, stream text-to-speech token by token, talk to any MCP server over stdio — and do all of it with the same fluent BoxLang syntax they always have.

Luis Majano
Luis Majano
October 20, 2026