Using <cfmail>

Contact Us or call 1-877-932-8228
Using <cfmail>

Using <cfmail>

Emails are sent with the <cfmail> tag. The table below shows some of <cfmail>'s most common attributes.

<cfmail> Attributes
Attribute Description
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.
username SMTP username
password SMTP password
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.

Code Sample:

Email/Demos/SimpleMail.cfm
<cfmail to="ndunn@webucator.com" from="info@runnershome.com" 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 server, username, and password attributes. Your mail server may also require a valid from email.

Sending a Confirmation Email

The code sample below shows how to send a confirmation email with <cfmail>.

Code Sample:

Email/Demos/Register.cfm
---- 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="runners@runnershome.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 ----

Sending Email as HTML

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.

Using <cfmailpart>

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.

<cfmailpart> Attributes
Attribute Description
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.
charset Character encoding.

Code Sample:

Email/Demos/Register-2.cfm
---- C O D E   O M I T T E D ----

			<cfmail to="#FORM.email#" from="runners@runnershome.com"
						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 ----

Attaching Files

Files can be attached using the <cfmailparam> tag. The syntax is as follows:

<cfmailparam file = "#ExpandPath("Logs/RunningLog.txt")#" type="image/gif">

There is no limit to the number of <cfmailparam> tags that can be included within a <cfmail> tag.

Next