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

BoxLang v1.8.0 : Revolutionary HTTP Client, SOAP Integration, and Production-Grade Stability

BoxLang v1.8.0 : Revolutionary HTTP Client, SOAP Integration, and Production-Grade Stability

The BoxLang team is excited to announce BoxLang 1.8.0, a massive release that revolutionizes HTTP capabilities, introduces comprehensive SOAP/WSDL integration, and delivers over 100 critical bug fixes for production-grade stability. This release focuses on modern web application development with fluent APIs, streaming support, persistent connection management, and extensive CFML compatibility improvements.

Luis Majano
Luis Majano
December 05, 2025
Ortus & BoxLang November Recap 2025

Ortus & BoxLang November Recap 2025

November 2025 was a big month at Ortus. BoxLang 1.7.0 arrived with real-time streaming, distributed caching, and faster compiler internals. ColdBox gained a cleaner debugging experience with full Whoops support, while CBWIRE 5 launched with stronger security, smarter lifecycles, and easier uploads.

Victor Campos
Victor Campos
December 02, 2025