Handling POP Mail

This section gives an example of each of the following usages:

Retrieving only message headers

The header includes the following information. When you use cfpop to get the header or the entire message, ColdFusion returns the values of each these fields in a query column, with one record per retrieved message:

To retrieve only the message header:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <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=#myusername#
    
      password=#mypassword#
    
      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><br>
    
    </cfoutput>
    
    
    
    </body>
    
    </html>
    
    
  3. Change the following line so that it refers to a valid POP mail server, as well as a valid user name and password:
    <cfpop server="mail.company.com"
    
      username=#username#
    
      password=#password#
    
    
  4. Save the file as hdronly.cfm in myapps under the Web root directory and view it in the ColdFusion Studio Browse tab or your Web browser.

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

The ColdFusion function HTMLEditFormat replaces characters that have meaning to HTML, such as the < and > signs that can surround detailed e-mail address information, with escaped characters such as &lt; and &gt;.

In addition, you can process the date returned by cfpop with ParseDateTime, which accepts an argument for converting POP date/time objects into a CFML date-time object.

For information on these ColdFusion functions, see the CFML Reference.

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>

Retrieving an entire message

When you use the cfpop tag with action="GetAll", ColdFusion returns the same columns as with getheaderonly, plus two additional columns, body and header.

To retrieve an entire message:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <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=#myusername#
    
      password=#mypassword#
    
      action="GetAll"
    
      name="Sample">
    
    
    
    <cfoutput query="Sample">
    
      MessageNumber: #HTMLEditFormat(Sample.messageNumber)# <br>
    
      To: #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>
    
      <br>
    
      Body:<br>
    
      #Sample.body#<br>
    
      <br>
    
      Header:<br>
    
      #HTMLCodeFormat(Sample.header)#<br>
    
      <hr>
    
      </cfoutput>
    
    
    
    </body>
    
    </html>
    
    
  3. Change the following line so that it refers to a valid POP mail server, as well as to a valid user name and password:
    <cfpop server="mail.company.com"
    
      username=#username#
    
      password=#password#
    
    
  4. Save the file as hdrbody.cfm in myapps under the Web root directory and view it in the ColdFusion Studio Browse tab or your Web browser.

Note that this example does not use a CFML function to encode the body contents. As a result, the browser displays the formatted message as you would normally see it in a mail program that supports HTML messages.

Retrieving attachments with messages

When you use the cfpop tag with action="getAll", and use the attachmentpath attribute to specify the directory in which to store attachements, ColdFusion gets any attachment files from the POP server and puts them in the specified directory. It also returns two additional columns:

You must make sure that the attachmentpath directory exists before you use the cfpop tag to get attachments. ColdFusion generates an error if it tries to write an attachment file to a nonexistent directory.

Not all messages have attachments. If a message has no attachments, attachments and attachmentfiles are empty strings.

To retrieve all parts of a message, including attachments:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <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:\temp\attachments"
    
      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>
    
      <br>
    
      Body:<br>
    
      #Sample.body# <br>
    
      <br>
    
      Header:<br>
    
      HTMLCodeFormat(Sample.header)# <br>
    
      <hr>
    
    </cfoutput>
    
    
    
    </body>
    
    </html>
    
    
  3. Change the following line so that it refers to a valid POP mail server, as well as to a valid user name and password:
    <cfpop server="mail.company.com"
    
      username=#username#
    
      password=#password#
    
    
  4. Save the file as hdrbody.cfm in myapps under the Web root directory and view it in the ColdFusion Studio Browse tab or your Web browser.

Note

To avoid duplicate filenames when saving attachments, set the generateUniqueFilenames attribute of cfpop to Yes.


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. You must also specify use the messagenumber attribute to specify the numbers of the messages to delete.

Using cfpop to delete a message permanently removes it from the server. If the messagenumber does not correspond to a message on the server, ColdFusion generates an error.


Note

Message numbers are reassigned at the end of every POP mail server communication that contains a delete action. For example, if you retrieve four messages from a POP mail server, the message numbers returned are 1,2,3,4. If you then delete messages 1 and 2 with a single cfpop tag, messages 3 and 4 are assigned message numbers 1 and 2, respectively.


To delete messages:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <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>
    
    
    
    
  3. Change the following line so that it refers to a valid POP mail server, as well as to a valid user name and password:
    <cfpop server="mail.company.com"
    
      username=#username#
    
      password=#password#
    
    
  4. Save the file as hdrbody.cfm in myapps under the Web root directory.

Caution

When you view this page in your browser or the ColdFusion Studio Browse tab, it immediately deletes the messages from your POP server.




Banner.Novgorod.Ru