CFPOP and Message Management Previously in this chapter, we discussed how you can use CFMAIL to create and send messages to recipients directly from your ColdFusion application. This is just one of the ways that ColdFusion enables you to leverage the power of email to enhance your web applications. Through the use of the CFPOP tag, you can enable users of your web applications to retrieve, read, and delete messages that exist on remote POP mail servers. This can be useful if you need to create a portal-type site, check a specific server for alerts, or create a host of other applications. In the next code example, let's examine how we might connect to a remote server and download the message headers on that server to see what email we have waiting for us: <cfpop server = "#server#" username = #UserName# password = #pwd# action = "getheaderonly" name = "TheMessageHeaders"> <cfoutput query = "TheMessageHeaders"> From: #From# Subject: #Subject#<BR> </cfoutput> The preceding code example will connect to the mail server we specify, check for any messages that exist for the username we've specified, and then output a summary of those messages to us. In the previous example, if we had specified getall as an attribute of action, as opposed to getheaderonly, CFPOP would have retrieved the entire message contents, rather than just the header information. Table 11.3 gives you an overview of the valid attributes for use with the CFPOP tag. Table 11.3. Attributes of the <CFPOP> Tag Attribute | Description | Server | This is a required attribute used to specify the IP address of the POP server to which you want to connect. | Port | This is an optional parameter that enables you to specify the port on which you want to connect to the POP server. By default, this is 110, the standard POP port. | Username | Although this is an optional attribute, this is where you need to specify the username of the user for whom you are attempting to retrieve mail. If nothing is specified here, the username defaults to anonymous, though it should be noted that most mail servers will reject an attempt to connect as an anonymous user. | Password | This is an optional parameter that should correspond to the password of the user that you've specified in the username attribute. | Action | This is an optional parameter that enables you to specify one of three options. You can choose to retrieve only message headers by specifying getheaderonly, you can return all available information by specifying getall, or you can attempt to delete a specific message by specifying delete. By default, the action will be getheaderonly to speed up the processing of this tag. | Name | This parameter is required if you've chosen an action of getheaderonly or getall. This enables you to specify a name of the query object that is returned. This is the query in which your message information will be contained, and you'll need to refer to the name you specify here to output any information. | Messagenumber | This is an optional parameter that enables you to specify the message number on which you want the action performed. If you've chosen delete as your action, a message number is required. With any other action, only the message numbers specified will be retrieved. | Attachmentpath | This parameter is useful when your action=getall. This enables attachments to be retrieved and written to the specified directory. | Timeout | This is an optional parameter that specifies the time, in seconds, to wait for mail processing to complete. The default timeout value is 60 seconds; so if you know you have a large attachment to download, you may want to increase this value. | Maxrows | This is an optional parameter that enables you to specify the maximum number of messages to be retrieved from the server, starting with the startrow parameter. | Startrow | This is an optional parameter that enables you to specify the first message number that you would like to retrieve. By default, this value is set to 1. | Generateuniquefilenames | This is an optional Yes/No parameter that enables you to specify whether you want the attachments to be saved with a unique name in the event of a naming conflict. | As with tags like CFFILE and CFQUERY, CFPOP generates some unique query values depending on the action that you have specified. Table 11.4 describes the query values that are returned for all actions completed with CFPOP. Table 11.4. Query Values Returned for All Actions Completed with CFPOP Value Returned | Description | Queryname.Recordcount | This value returns the total number of records that were returned when your CFPOP action was completed. | Queryname.Currentrow | When processing a query result set for output, there may be times in which you will want to display the row with which you are currently working. This value enables you to retrieve that number. | Queryname.ColumnList | This returns a list of all column names contained with in the query result set returned by your CFPOP action. | The following list describes the values that are returned by CFPOP when your action is equal to getheaderonly or getall: queryname.Date queryname.From queryname.Replyto queryname.Subject queryname.CC queryname.To queryname.Messagenumber The following list describes the values that are returned by CFPOP when your action is getall. These values are not available when the action equals getheaderonly. You can use any of the values returned by your CFPOP action in the output of your pages to construct an application that almost completely mimics the functionality of a mail client. |