Handling POP Mail  
 
 

You use the ACTION attributes of the CFPOP tag to describe the data set to retrieve. You can get all data (GetAll), get header data only (GetHeaderOnly), or delete mail (DELETE).

This section gives an example of each usage:

  • Retrieving only message headers
  • Retrieving a message body
  • Retrieving attachments
  • Deleting messages

Note that for any of the examples below to work, you'll need to reference an existing POP mail server with a valid user and password. Save the code example to a directory using the indicated filenames. Then open each CFM file to run the example.

 
 
  Returning only message headers  
 
 

When you use the CFPOP tag with ACTION="GetHeaderOnly", ColdFusion returns only the mail message header information to the query specified in the NAME attribute. The following columns are returned:

  • DATE
  • FROM
  • MESSAGENUMBER
  • REPLYTO
  • SUBJECT
  • CC
  • TO
 
 
  Header information  
 

Mail message header information returned by CFPOP may be enclosed in HTML coding. You can use the ColdFusion function HTMLCodeFormat to replace HTML tags with escaped characters, such as &gt; for > and &lt; for <.

In addition, the date returned by CFPOP can be processed with ParseDateTime, which accepts an argument for converting POP date/time objects into GMT (Greenwich Mean Time).

See the CFML Language Reference for information on these ColdFusion functions.

You can reference any of these columns in a CFOUTPUT tag, as the following example shows.

<CFOUTPUT>
    #ParseDateTime(queryname.date, "POP")#
    #HTMLCodeFormat(queryname.from)#
    #HTMLCodeFormat(queryname.messagenumber)#
</CFOUTPUT>
 
 
  Example of retrieving message headers  
 

Users can retrieve and optionally delete mail messages from a POP mail server. This first example, hdronly.cfm, illustrates how to retrieve message header information. You can use the code as is after you've supplied your POP mail server name, user name and password.

This code retrieves the message headers and stores them in a CFPOP result set called Sample.

Sample file: hdronly.cfm

<HTML>
<HEAD>
<TITLE>POP Mail Message Header Example</TITLE>
</HEAD>

<BODY>
<H2>This example retrieves message 
header information:</H2>

<CFPOP SERVER="mail.company.com"
    USERNAME=#username#
    PASSWORD=#password#
    ACTION="GetHeaderOnly"
    NAME="Sample">

<CFOUTPUT QUERY="Sample">
    MessageNumber: #HTMLEditFormat(Sample.MESSAGENUMBER)# <BR>
    To: #HTMLEditFormat(Sample.TO)# <BR>
    From: #HTMLEditFormat(Sample.FROM)# <BR>
    Subject: #HTMLEditFormat(Sample.SUBJECT)# <BR>
    Date: #HTMLEditFormat(Sample.DATE)# <BR>
    Cc: #HTMLEditFormat(Sample.CC)# <BR>
    ReplyTo: #HTMLEditFormat(Sample.REPLYTO)# <BR>
</CFOUTPUT>

</BODY>
</HTML>
 
 
  Returning an entire message  
 
 

When you use the CFPOP tag with ACTION="GetAll", ColdFusion returns the same columns returned with GETHEADERONLY, as well as two additional columns, BODY and HEADER.

 
 
  Example  
 

In this sample file, hdrbody.cfm, the ACTION attribute is set to GetAll, so it retrieves the message body as well as its header.

Sample file: hdrbody.cfm

<HTML>
<HEAD>
<TITLE>POP Mail Message Body Example</TITLE>
</HEAD>

<BODY>
<H2>This example adds retrieval of 
the message body:</H2>

<CFPOP SERVER="mail.company.com"
    USERNAME=#username#
    PASSWORD=#password#
    ACTION="GetAll"
    NAME="Sample">

<CFOUTPUT QUERY="Sample">
    MessageNumber: #HTMLEditFormat(Sample.MESSAGENUMBER)# <BR>
    To: #HTMLEditFormat(Sample.TO)# <BR>
    From: #HTMLEditFormat(Sample.FROM)# <BR>
    Subject: #HTMLEditFormat(Sample.SUBJECT)# <BR>
    Date: #HTMLEditFormat(Sample.DATE)# <BR>
    Cc: #HTMLEditFormat(Sample.CC)# <BR>
    ReplyTo: #HTMLEditFormat(Sample.REPLYTO)# <BR>
    Body: #HTMLCodeFormat(Sample.BODY)# <BR>
    Header: #HTMLCodeFormat(Sample.HEADER)# <BR>
</CFOUTPUT>

</BODY>
</HTML>
 
 
  Returning attachments with messages  
 
 

When you use the CFPOP tag with ACTION="GetAll", and add the ATTACHMENTPATH attribute, ColdFusion returns two additional columns:

  • ATTACHMENTS contains a tab-separated list of all source attachment names.
  • ATTACHMENTFILES contains a tab-separated list of the actual temporary filenames written to the server. Use the CFFILE tag to delete the temporary files.

Not all messages have attachments. If a message has no attachments, both ATTACHMENTS and ATTACHMENTFILES will be equal to an empty string.

 
 
  Managing attachment filenames  
 

To avoid any problems with duplicate filenames, ColdFusion creates temporary files for all attachments. If you retrieve files with attachments, you can use the ATTACHMENTPATH attribute to choose where the attachment files go. It is the developer's responsibility to manage these files so they can be accessed unambiguously.

Also note that it is the developer's responsibility to clean up any temporary files written as a result of executing a CFPOP tag.

 
 
  Example  
 

This retrieval example, attach.cfm, retrieves all parts of the message including attachments.

Sample File: attach.cfm

<HTML>
<HEAD>
<TITLE>POP Mail Message Attachment Example</TITLE>
</HEAD>

<BODY>
<H2>This example retrieves message header,
body, and all attachments:</H2>

<CFPOP SERVER="mail.company.com"
    USERNAME=#username#
    PASSWORD=#password#
    ACTION="GetAll"
    ATTACHMENTPATH="c:\attachdir"
    NAME="Sample">

<CFOUTPUT QUERY="Sample">
    MessageNumber: #HTMLEditFormat(Sample.MESSAGENUMBER)# <BR>
    To: #HTMLEditFormat(Sample.TO)# <BR>
    From: #HTMLEditFormat(Sample.FROM)# <BR>
    Subject: #HTMLEditFormat(Sample.SUBJECT)# <BR>
    Date: #HTMLEditFormat(Sample.DATE)# <BR>
    Cc: #HTMLEditFormat(Sample.CC)# <BR>
    ReplyTo: #HTMLEditFormat(Sample.REPLYTO)# <BR>
    Attachments: #HTMLEditFormat(Sample.ATTACHMENTS)# <BR>
    Attachment Files: #HTMLEditFormat(Sample.ATTACHMENTFILES)# <BR>
    Body: #HTMLCodeFormat(Sample.BODY)# <BR>
    Header: #HTMLCodeFormat(Sample.HEADER)# <BR>
</CFOUTPUT>

</BODY>
</HTML>
 
 
  Deleting messages  
 
 

By default, retrieved messages are not deleted from the POP mail server. If you want to delete retrieved messages, you must set the ACTION attribute to Delete.

The MESSAGENUMBER attribute returned by all CFPOP retrievals contains the message number you need to pass back to the POP mail server to have the corresponding message deleted. A few notes:

  • Once a message is deleted, it's gone for good.
  • Message numbers are reassigned at the end of every POP mail server communication that contains a delete action. For example, if four messages are retrieved from a POP mail server, the message numbers returned will be 1,2,3,4. If two messages are then deleted within a single CFPOP tag, messages 3 and 4 will be assigned message numbers 1 and 2, respectively.
 
 
  Example  
 

Sample file: msgdel.cfm

<HTML>
<HEAD>
<TITLE>POP Mail Message Delete Example</TITLE>
</HEAD>

<BODY>
<H2>This example deletes messages:</H2>

<CFPOP SERVER="mail.company.com"
    USERNAME=#username#
    PASSWORD=#password#
    ACTION="Delete"
    MESSAGENUMBER="1,2,3">

</BODY>
</HTML>
 
 
  For more information  
 

For more information on using the CFMAIL and CFPOP tags, see the CFML Language Reference.



 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.