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

12 Days of BoxLang - Day 4: TestBox

12 Days of BoxLang - Day 4: TestBox

Today we’re celebrating one of the most exciting new additions to the BoxLang ecosystem:

the TestBox BoxLang CLI Runner — a fast, native way to run your TestBox tests directly through the BoxLang Runtime. ⚡

No server required. No CommandBox needed. Just pure, ultra-fast BoxLang-powered testing from the command lineon Windows, Mac, and Linux.

If you’re building modern applications with BoxLang — web apps, CLIs, serverless functions, Android apps, or OS-level utilities — this new feature gives you a unified, flexible testing workflow you can run anywhere.

Victor Campos
Victor Campos
December 13, 2025
12 days of BoxLang - Day 3: SocketBox!

12 days of BoxLang - Day 3: SocketBox!

As BoxLang continues evolving into a modern, high-performance, JVM-based runtime, real-time communication becomes essential for the applications we all want to build: dashboards, collaboration tools, notifications, live feeds, multiplayer features, and more.

That’s where SocketBox steps in — the WebSocket upgrade listener built to work seamlessly with CommandBox and the BoxLang MiniServer. ⚡

Today, for Day 3, we’re highlighting how SocketBox supercharges BoxLang development by giving you fast, flexible, and framework-agnostic WebSocket capabilities.

Maria Jose Herrera
Maria Jose Herrera
December 12, 2025
12 Days of BoxLang - Day 2: CommandBox

12 Days of BoxLang - Day 2: CommandBox

BoxLang + CommandBox: The Enterprise Engine Behind Your Deployments

For Day 2 of our 12 Days of Christmas series, we’re diving into one of the most powerful parts of the BoxLang ecosystem: CommandBox the defacto enterprise servlet deployment platform for BoxLang.

If BoxLang is the language powering your applications, CommandBox is the engine room behind it all. ⚙️

Victor Campos
Victor Campos
December 11, 2025