Emails are sent with the <cfmail> tag. The table below shows some of <cfmail>'s most common attributes.
|to||Required. Recipient email address.|
|from||Required. Sender email address.|
|subject||Required. Email subject.|
|cc||Email addresses to be copied.|
|bcc||Email addresses to be blind copied.|
|replyto||Address to receive replies.|
|type||HTML. If omitted, the email will be sent as plain text.|
|query||Query to use when sending multiple emails.|
|server||SMTP server address. Overrides setting in ColdFusion Administrator.|
|timeout||Seconds to wait before timing out. Overrides setting in ColdFusion Administrator.|
Here is an example of a simple <cfmail> tag.
<cfmail to="email@example.com" from="firstname.lastname@example.org" subject="Hello There">Hello there! Thanks for visiting. Best regards, RunnersHome</cfmail>
Expressions in pound signs within
<cfmail> tags are evaluated.
<cfoutput> tags are not required and will likely cause an error or
undesired results. For this file to work, you must have a valid mail server set up in ColdFusion Administrator. Alternatively, you can configure the
<cfmail> tag to select the server using the
password attributes. Your mail server may also require a valid
The code sample below shows how to send a confirmation email with <cfmail>.
---- C O D E O M I T T E D ---- <cfif FORM.password EQ FORM.password2> <cfquery name="emailcheck" datasource="#APPLICATION.datasource#"> SELECT * FROM Users WHERE email='#FORM.email#' </cfquery> <cfif emailcheck.RecordCount EQ 0> <cfquery datasource="#APPLICATION.datasource#"> INSERT INTO Users (firstname, lastname, email, password) VALUES ('#FORM.firstname#', '#FORM.lastname#', '#FORM.email#', '#FORM.password#') </cfquery> <cfset SESSION.firstname = FORM.firstname> <cfset SESSION.lastname = FORM.lastname> <cfset SESSION.userid = emailcheck.userid> <cfmail to="#FORM.email#" from="email@example.com" subject="Successful Registration"> Congratulations! You have successfully registered for Runners Home! </cfmail> Thanks for registering! <cfelse> <p>It appears you have already registered.</p> </cfif> <cfelse> <p class="errors"><b>Your passwords do not match. Please <a href= "Register.cfm">try again</a>.</p> </cfif> ---- C O D E O M I T T E D ----
Email can be sent in plain text format or in HTML format. The default is plain text format. Setting the type attribute of the <cfmail> tag can be set to "html" (only possible value) indicates that the message is in HTML format. You can then use HTML tags within the body of the message. HTML-enabled email clients will render the message as an HTML page.
Of course, you do not always know what type of email client the mail recipient will have. By nesting <cfmailpart> tags within <cfmail>, you can provide alternative email messages in plain text and HTML format.
|type||Required. Options are text, plain, and html. text and plain both specify text/plain format. html specifies HTML format.|
|wraptext||Used for plain text emails to specify the number of characters per line. If omitted, text will not wrap.|
---- C O D E O M I T T E D ---- <cfmail to="#FORM.email#" from="firstname.lastname@example.org" subject="Successful Registration"> <cfmailpart type="text" wraptext="72"> Congratulations! You have successfully registered for Runners Home! </cfmailpart> <cfmailpart type="html"> <b>Congratulations!</b> You have successfully registered for Runners Home!<br> <img src="http://www.astroleague.org/al/regional/congrats-fireworks.gif"> </cfmailpart> </cfmail> ---- C O D E O M I T T E D ----
Files can be attached using the <cfmailparam> tag. The syntax is as follows:
There is no limit to the number of <cfmailparam> tags that can be included within a <cfmail> tag.