Controller
sendEmail()
Signature
Section titled “Signature”sendEmail() — returns any
Available in: controller
Category: Miscellaneous Functions
Description
Section titled “Description”Sends an email using a template and an optional layout to wrap it in.
Besides the Wheels-specific arguments documented here, you can also pass in any argument that is accepted by the cfmail tag as well as your own arguments to be used by the view.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
template | string | yes | — | The path to the email template or two paths if you want to send a multipart email. if the detectMultipart argument is false, the template for the text version should be the first one in the list. This argument is also aliased as templates. |
from | string | yes | — | Email address to send from. |
to | string | yes | — | List of email addresses to send the email to. |
subject | string | yes | — | The subject line of the email. |
layout | any | no | false | Layout(s) to wrap the email template in. This argument is also aliased as layouts. |
file | string | no | — | A list of the names of the files to attach to the email. This will reference files stored in the files folder (or a path relative to it). This argument is also aliased as files. |
detectMultipart | boolean | no | true | When set to true and multiple values are provided for the template argument, Wheels will detect which of the templates is text and which one is HTML (by counting the < characters). |
deliver | boolean | no | true | When set to false, the email will not be sent. |
writeToFile | string | no | — | Path that receives the rendered text and/or HTML body. This is a debug dump of the body content, not a MIME .eml — no From/To/Subject/Content-Type headers are written. A .eml extension will not open as a rendered message in Outlook; use .html/.txt and open the file in a browser or editor. |
Examples
Section titled “Examples”1. Basic email to a new user
newMember = model("member").findByKey(params.member.id);
sendEmail(
to=newMember.email,
template="welcomeEmail",
subject="Thank You for Joining!",
recipientName=newMember.name,
startDate=newMember.startDate
);
2. Multipart email (HTML + text)
sendEmail(
to="user@example.com",
template="welcomeEmailText, welcomeEmailHTML",
subject="Welcome!",
detectMultipart=true
);
3. Email with a layout
sendEmail(
to="user@example.com",
template="newsletter",
layout="emailLayout",
subject="Monthly Newsletter",
userName="Salman"
);
4. Email with attachments
sendEmail(
to="user@example.com",
template="reportEmail",
subject="Your Monthly Report",
file="report.pdf, summary.xlsx"
);
5. Write the rendered body to a file without sending
// writeToFile dumps the text/HTML body only — it is not a MIME .eml.
// Open the file in a text editor (or a browser for HTML). A .eml
// extension will show raw HTML tags in Outlook.
sendEmail(
to="user@example.com",
template="testEmail",
subject="Testing Email",
writeToFile="#expandPath('./tmp/testEmail.html')#",
deliver=false
);