Sending e-mail messages



Before you configure ColdFusion to send e-mail messages, you must have access to an SMTP e-mail server. Also, before you run application pages that access the e-mail server, you can configure the ColdFusion Administrator to use the SMTP server. If you must override the ColdFusion Administrator SMTP server setting for any messages, you can specify a new mail server in the server attribute of the cfmail tag.

Configure ColdFusion for e-mail

  1. In the ColdFusion Administrator, select Server Settings > Mail.

  2. In the Mail Server box, enter the name or IP address of your SMTP mail server.

  3. (Optional) Change the Server Port and Connection Timeout default settings.

  4. Select the Verify Mail Server Connection option to make sure ColdFusion can access your mail server.

  5. If your mail server does not use port 25, the default, SMTP port, change the Server Port default settings.

  6. Depending on your ColdFusion edition, the Mail page in the Administrator has additional options that you can use to configure and optimize ColdFusion mail behavior. Select these options as appropriate.

  7. Click Submit Changes.

    ColdFusion saves the settings. The page displays a message indicating success or failure for connecting to the server.

ColdFusion Enterprise edition includes additional mail spooling and delivery features. For more information on these features, and for information on the ColdFusion Administrator mail settings, see Configuring and Administering ColdFusion.

Sending SMTP e-mail with the cfmail tag

The cfmail tag provides support for sending SMTP e-mail from within ColdFusion applications. The cfmail tag is like the cfoutput tag, except that cfmail outputs the generated text as an SMTP mail message rather than to a page. The cfmail tag supports all the attributes and commands that you use with cfoutput, including query. The following table describes basic cfmail tag attributes that you could use to send a simple e-mail message. For a complete list of attributes, see the cfmail description in the CFML Reference.

Attribute

Description

subject

The subject of the message.

from

The e-mail address of the sender.

to

The e-mail address of the recipient. Use a comma-delimited list to specify multiple recipients.

cc

(Optional) The e-mail address of a carbon copy recipient. The recipient address is visible to other recipients. Use a comma-delimited list to specify multiple cc recipients.

bcc

(Optional) The e-mail address of a blind carbon copy recipient. The recipient address is not visible to other recipients. Use a comma-delimited list to specify multiple bcc recipients.

Send a simple e-mail message

  1. Create a ColdFusion page with the following content:

    <html> 
    <head> 
        <title>Sending a simple e-mail</title> 
    </head> 
    <body> 
    <h1>Sample e-mail</h1> 
    <cfmail 
        from="Sender@Company.com" 
        to="#URL.email#" 
        subject="Sample e-mail from ColdFusion"> 
     
    This is a sample e-mail message to show basic e-mail capability. 
     
    </cfmail> 
    The e-mail was sent. 
     
    </body> 
    </html>
  2. Save the file as send_mail.cfm in the myapps directory under your web_root directory.

  3. Open your browser and enter the following URL:

    http://localhost:8500/myapps/send_mail.cfm?email=myname@mycompany.com

    (Replace myname@mycompany.com with your e-mail address.)

    The page sends the e-mail message to you, through your SMTP server.

Note: If you do not receive an e-mail message, check whether you have configured ColdFusion to work with your SMTP server; for more information, see Sending e-mail messages.

The cfmail tag has many options that let you customize your mail or control how it is sent. For a description of all attributes, including options to wrap mail text at a specified column, specify the mail character encoding, and specify the mail server, user name, and password, see the cfmail description in the CFML Reference.

Sending HTML e-mail

If you know all the mail recipients use mail applications that are capable of reading and interpreting HTML code in a mail message, you can use the cfmail tag to send an HTML message. The cfmail tag type="HTML" attribute informs the receiving e-mail client that the message contains embedded HTML tags that must be processed. For an example that sends HTML mail, see Including images in a message.

Sending multipart mail messages

The cfmailpart tag lets you create multipart mail messages, with each part having a different MIME type or character set. For example, if you do not know that all recipients can interpret HTML mail messages, you can send your message as a multipart mail with a text part and an HTML part. To do so, use two cfmailpart tags, one with the HTML version of the message and one with the plain text message, as shown in the following example. To test this example, replace the To attribute value with a valid e-mail address, save and run the page, and check the incoming e-mail at the address you entered.

<cfmail from = "peter@domain.com" To = "paul@domain.com"  
Subject = "Which version do you see?"> 
    <cfmailpart  
        type="text" 
        wraptext="74"> 
            You are reading this message as plain text, because your mail reader  
            does not handle HTML text. 
    </cfmailpart>> 
    <cfmailpart  
        type="html"> 
            <h3>HTML Mail Message</h3> 
            <p>You are reading this message as <strong>HTML</strong>.</p> 
            <p>Your mail reader handles HTML text.</p> 
    </cfmailpart> 
</cfmail>
Note: In the HTML version of the message, escape any number signs, such as those used to specify colors, by using two # characters; for example, bgcolor="##C5D9E5".