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!
" ); 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