LogoLogo
HomeAPIBlog
2.5.0
2.5.0
  • INTRODUCTION
    • Getting Started
      • Running Local Development servers
      • Beginner Tutorial: Hello World
      • Beginner Tutorial: Hello Database
      • Tutorial: CFWheels, AJAX, and You
    • Frameworks and CFWheels
    • Requirements
    • Manual Installation
    • Upgrading
    • Screencasts
  • Command Line Tools
    • CLI Commands
    • wheels - commands
    • wheels generate - commands
    • wheels dbmigrate - commands
    • wheels plugins - commands
  • Working with CFWheels
    • Conventions
    • Configuration and Defaults
    • Directory Structure
    • Switching Environments
    • Testing Your Application
    • Contributing to CFWheels
    • Documenting your Code
  • Handling Requests with Controllers
    • Request Handling
    • Rendering Content
    • Redirecting Users
    • Sending Files
    • Sending Email
    • Responding with Multiple Formats
    • Using the Flash
    • Using Filters
    • Verification
    • Event Handlers
    • Routing
    • URL Rewriting
      • Apache
      • IIS
      • Tomcat
      • Nginx
    • Obfuscating URLs
    • Caching
    • Nesting Controllers
    • CORS Requests
  • Displaying Views to Users
    • Pages
    • Partials
    • Linking Pages
    • Layouts
    • Form Helpers and Showing Errors
    • Displaying Links for Pagination
    • Date, Media, and Text Helpers
    • Creating Custom View Helpers
    • Localization
  • Database Interaction Through Models
    • Object Relational Mapping
    • Creating Records
    • Reading Records
    • Updating Records
    • Deleting Records
    • Column Statistics
    • Dynamic Finders
    • Getting Paginated Data
    • Associations
    • Nested Properties
    • Object Validation
    • Object Callbacks
    • Calculated Properties
    • Transactions
    • Dirty Records
    • Soft Delete
    • Automatic Time Stamps
    • Database Migrations
      • Migrations In Production
    • Using Multiple Data Sources
  • Plugins
    • Installing and Using Plugins
    • Developing Plugins
    • Publishing Plugins
  • External Links
    • Source Code
    • Issue Tracker
    • Sponsor Us
    • Community
Powered by GitBook
LogoLogo
On this page
  • Establishing Mail Server and Account Information
  • Create an Email Template
  • Sending the Email
  • Multipart Emails
  • Attaching Files
  • Using Email Layouts
  • Multipart Email Layouts
  • Go Send Some Emails

Was this helpful?

Edit on GitHub
Export as PDF
  1. Handling Requests with Controllers

Sending Email

Use CFWheels to simplify the task of setting up automated emails in your application.

PreviousSending FilesNextResponding with Multiple Formats

Last updated 1 year ago

Was this helpful?

Sending emails in CFWheels is done from your controller files with the function. It's basically a wrapper around cfmail, but it has some smart functionality and a more CFWheels-like approach in general.

Getting this to work in CFWheels can be broken down in 3 steps. We'll walk you through them.

Establishing Mail Server and Account Information

We recommend using CFWheels ability to set global defaults for so that you don't have to specify more arguments than necessary in your controller files. Because it's likely that you will use the same mail server across your application, this makes it worthwhile to set a global default for it.

This setting should be done in the config/settings.cfm file and can look something like this:

Example
set(
    functionName="sendEmail",
    server="yourServer",
    username="yourUsername",
    password="yourPassword"
);

By specifying these values here, these arguments can be omitted from all function calls, thus providing cleaner, less cluttered code.

But you are not limited to setting only these 3 variables. In fact, you can set a global default for any optional argument to and since it accepts the same arguments that cfmail does. That's quite a few.

Alternatively, most modern CFML engines allow setting SMTP information directly within the application configuration. So you can actually add this in /config/app.cfm: here's an example configuration:

config/app.cfm
// Lucee:
this.tag.mail.server="smtp.mydomain.com";
this.tag.mail.username="mySMTPUsername";
this.tag.mail.password="mySMTPPassword";
this.tag.mail.port=587; // Optional port
this.tag.mail.usetls=true; // Optional TLS

// Adobe ColdFusion
this.smtpServersettings = {
    'server' : 'smtp.thedomain.com:2525',
    'username' : 'xxxxxxxx',
    'password' : 'xxxxxxx'
};

Create an Email Template

Consider this example scenario:

Controller:
Email Template:

Membership

/views/membership/myemailtemplate.cfm

Multiple templates may be stored within this directory should there be a need.

The content of the template is simple: simply output the content and any expected variables.

Here's an example for myemailtemplate.cfm, which will contain HTML content.

Example
<cfoutput>
<p>Hi #recipientName#,</p>
<p>
    We wanted to take a moment to thank you for joining our service
    and to confirm your start date.
</p>
<p>
    Our records indicate that your membership will begin on
    <strong>#DateFormat(startDate, "dddd, mmmm, d, yyyy")#</strong>.
</p>
</cfoutput>

Sending the Email

Consider the following example:

Example
member = model("member").findByKey(newMember.id);
sendEmail(
    from="service@yourwebsite.com",
    to=member.email,
    template="myemailtemplate",
    subject="Thank You for Becoming a Member",
    recipientName=member.name,
    startDate=member.startDate
);

Here we are sending an email by including the myemailtemplate template and passing values for recipientNameand startDate to it.

Note that the template argument should be the path to the view's folder name and template file name without the extension. If the template is in the current controller, then you don't need to specify a folder path to the template file. In that case, just be sure to store the template file in the folder with the rest of the views for that controller.

Did you notice that we did not have to specify the type attribute of cfmail? CFWheels is smart enough to figure out that you want to send as HTML since you have tags in the email body. (You can override this behavior if necessary though by passing in the type argument.)

Multipart Emails

The intelligence doesn't end there though. You can have CFWheels send a multipart email by passing in a list of templates to the templates argument (notice the plural), and CFWheels will automatically figure out which one is text and which one is HTML.

Attaching Files

You can attach files to your emails as well by using the file argument (or files argument if you want multiple attachments). Simply pass in the name of a file that exists in the files folder (or a subfolder of it) of your application.

// Send files/termsAndConditions.pdf
sendEmail(
    to="tom@domain.com",
    from="tom@domain.com",
    template="/email/exampletemplate",
    subject="Test",
    files="termsAndConditions.pdf"
);

Alternatively you can pass in mail parameters directly if you require more control (such as sending a dynamically generated PDF which isn't written to disk):

Mail Example
// Create PDF
cfdocument(name="PDFContent", format="pdf"){ 
  writeOutput("<h1>Cats are better than dogz!</h1>"); 
};

// Setup Mail Params
mailParams[1]["file"]= "Test.pdf";
mailParams[1]["type"]= "application/pdf";
mailParams[1]["content"]= PDFContent;

sendEmail(
    to="tom@domain.com",
    from="tom@domain.com",
    template="/email/exampletemplate",
    subject="Test",
    mailParams=mailParams
);

Using Email Layouts

A layout should be used just as the name implies: for layout and stylistic aspects of the email body. Based on the example given above, let's assume that the same email content needs to be sent twice.

  • Message is sent to a new member with a stylized header and footer.

  • A copy of message is sent to an admin at your company with a generic header and footer.

Best practice is that variables (such as recipientName and startDate, in the example above) be placed as outputs in the template file.

Example
// Get new member.
member = model("member").findByKey(params.key);

// Customer email with customized header/footer.
sendEmail(
    from="service@yourwebsite.com",
    template="myemailtemplate",
    layout="customer",
    to=member.email,
    subject="Thank You for Becoming a Member",
    recipientName=member.name,
    startDate=member.startDate
);

// Plain text message with "admin" layout.
sendEmail(
    from="service@yourwebsite.com",
    template="myemailtemplate",
    layout="admin",
    to="admin@domain.com",
    subject="Membership Verification: #member.name#",
    recipientName=member.name,
    startDate=member.startDate
);

Multipart Email Layouts

CFWheels also lets you set up layouts for the HTML and plain text parts in a multipart email.

Example
// Multipart customer email.
sendEmail(
    from="service@yourwebsite.com",
    templates="/myemailtemplateplain,/myemailtemplatehtml",
    layouts="customerPlain,customerHtml",
    to=member.email,
    subject="Thank You for Becoming a Member",
    recipientName=member.name,
    startDate=member.startDate
);

For both the templates and layouts arguments (again, notice the plurals), we provide a list of view files to use. CFWheels will figure out which of the templates and layouts are the HTML versions and separate out the MIME parts for you automatically.

Go Send Some Emails

Now you're all set to send emails the CFWheels way. Just don't be a spammer, please!

An email template is required for to work and forms the basis for the mail message content. Think of an email template as the content of your email.

Templates may be stored anywhere within the /views/ folder, but we recommend a structured, logical approach. If different controllers utilize and each require a unique template, place each email template within the views/controllername folder structure.

As we've said before, accepts all attribute of CFML's cfmail tag as arguments. But it also accepts any variables that you need to pass to the email template itself.

The logic for which template file to include follows the same logic as the template argument to .

Like the template argument, the logic for which file to include follows the same logic as the template argument to .

Much like the layouts outlined in the chapter, you can also create layouts for your emails.

In this case, the two calls to would be nearly identical, with the exception of the layout argument.

If we set up generic email layouts at views/plainemaillayout.cfm and views/htmlemaillayout.cfm, we would call like so:

sendEmail()
sendEmail()
sendEmail()
sendEmail()
sendEmail()
sendEmail()
sendEmail()
renderView()
renderView()
Layouts
sendEmail()
sendEmail()