Receiving E-Mail


You can use the <cfpop> tag to read e-mails. This tag uses the POP server to receive the e-mails. The attributes associated with this tag are described in Table 27.2.

Table 27.2: The <cfpop> Tag's Attributes

Attribute

Feature

Optional?

Server

Specifies the hostname or the IP address of the POP3 server. You cannot connect to the POP3 server without the complete IP address.

No

Username

Specifies the name of the user. If no name is specified, the POP connection is anonymous.

Yes

Password

Specifies the password of the user.

Yes

Action

Specifies the action to perform after connecting to the server. It takes three values. GetHeaderOnly retrieves the complete or partial list of the message readers only. GetAll retrieves the header as well as content of one or more messages received in the mailbox. Delete removes the messages from the inbox on the POP server. The default action is to retrieve a list of message headers only if no value is assigned to Action.

Yes

Name

Specifies the name of the ColdFusion query to store the results.

Yes

Port

Specifies the port number, which the POP server uses. The default port number is 110.

Yes

Messagenumber

Specifies the message number on which the Action attribute will work. It accepts single numbers as well as comma-delimited lists of numbers. It ignores invalid message numbers.

Yes

Attachmentpath

Specifies the path to store the incoming attachment files.

Yes

Timeout

Specifies how long (in seconds) the server waits to process the e-mails. The default value is 60 seconds.

Yes

Maxrows

Specifies the number of e-mails to retrieve. This value is ignored if Messagenumber is specified.

Yes

Startrow

Specifies the first message to retrieve. This value is also ignored if Messagenumber is specified. The default value is 1.

Yes

Generateuniquefilenames

Takes a Boolean value. If the value is set to TRUE, unique names are assigned to the incoming files.

Yes

The information retrieved using the <cfpop> tag is stored in a query. This query is named using the Name attribute. The information retrieved can be manipulated and viewed by using the <cfoutput> and <cfloop> tags. The fields of the query are described in Table 27.3.

Table 27.3: Query Fields

Field Name

Description

Messagenumber

Contains the ID assigned to the e-mail by the mail server.

Date

Stores the mailing date.

From

Stores the sender's e-mail addresses.

To

Stores the recipient's e-mail addresses.

Reply To

Stores the e-mail ID to which a reply should be directed.

Subject

Stores the subject of the e-mail.

UID

Stores a unique identifier for the e-mail.

Header

Stores the complete contents of the message's header in a single column.

The recordcount and currentrow variables are used in the <cfpop> tag. The recordcount variable contains the total number of rows returned by the query, and currentrow contains the row number being processed currently.

Receiving Message Headers

You can create an inbox for receiving only the message headers of e-mails. The header information consists of the following:

  • Sender's e-mail ID

  • Mailing date and time

  • Subject of the e-mail

The following code creates an inbox:

 <!---  inbox.cfm template --- > <HTML> <HEAD> <TITLE><H1> Inbox -- Example </H1></TITLE> </HEAD> <BODY>     <HR>     <CFPOP SERVER = "smtp.domainname.com" USERNAME = "Terry" PASSWORD = "abcd"            ACTION = "getheaderonly" NAME = "myinbox">     <CFOUTPUT>       <H2>The total messages are #myinbox.RecordCount# </H2>       <P>       <HR>     </CFOUTPUT>     <TABLE>       <TR>         <TH> Message No. </TH>         <TH> Subject </TH>         <TH> From </TH>         <TH> Date </TH>       </TR>       <CFOUTPUT QUERY = "myinbox">       <TR>         <TD> #myinbox.messagenumber# </TD>         <TD> #myinbox.subject# </TD>         <TD> #myinbox.from# </TD>         <TD> #myinbox.date# </TD>       </TR>       </CFOUTPUT>     </TABLE> </BODY> </HTML> 

Reading the Complete E-Mail

To read the complete e-mail message, you can use the GetAll option of the Action attribute. You can use the following code to read the contents of the message:

 <!--- Readmail.cfm template ---> <HTML> <HEAD> <TITLE> Reading the Contents of the E-mail </TITLE> </HEAD> <BODY>     <HR>     <CFPOP SERVER = "pop.mail.yahoo.com" USERNAME = "Julia@yahoo.com"             PASSWORD = "abcd" ACTION = "GetAll" MESSAGENUMBER = "1"             NAME = "mesg" >     <CFOUTPUT QUERY = "mesg">       The mail has been sent by #mesg.from# on # mesg.date# to #mesg.to#       <P>       #mesg.body#     <CFOUTPUT> </BODY> </HTML> 

The preceding example retrieves the contents of the first message.

Note

Certain POP servers don't support the Messagenumber field. In that case, you can use the UID field of the query result.

Receiving Attachments

You can download the attachment received along with the e-mail by using the Attachmentpath attribute. This attribute specifies the path to store the incoming file attachment. The following code snippet enables you to retrieve the attachment:

 <CFPOP ACTION="GetAll" name="mail" messagenumber="1" timeout="900"         SERVER="pop.mail.yahoo.com" USERNAME=Tina@yahoo.com         PASSWORD="xyz" ATTACHMENTPATH = "c:\data"         GENERATEUNIQUEFILENAMES = "yes" > 

The file is downloaded to the folder path specified in the Attachmentpath attribute. When you use this attribute, the program adds the attachments and attachmentfile columns to the query. The attachments column contains a tab-delimited list of original file names received as attachments. The attachmentfiles column contains a tab-delimited list of file names with which the attachments are actually saved. The Generateuniquefilenames attribute gives a unique file name to each file received as an attachment. After you download the files to your hard disk, you can remove these files only by using the <CFFILE> tag.

You can receive a single attachment or multiple attachments. To view all the attachments received in the browser, you need to use looping, as shown in the following code snippet:

 <OL>      <CFLOOP INDEX = "ListElement" LIST = " mail.attachmentfile">      <CFOUTPUT>        <A HREF = "#c:\data\ListElement#" > #listElement# </A>      </CFOUTPUT>      </CFLOOP> .</OL> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net