Create your own Email Client With ColdFusion!
By Pablo Varando

 

NOTE: A tutorial was added to the site by Robert Jones that takes this example to ColdFusion MX. The tutorial below will work on a CF 5 environment only. If you are running CFMX please check out the new tutorial.

This tutorial will show you how to create a mail system for your site. It will allow you to get your email from a POP3 server, view your inbox, then view the message (with attachments), reply and delete that message as well.

There will be seven files associated with this tutorial that you will create.

application.cfm - This file will allow you to define some important variables to be used by the application.
inbox.cfm - This is the file that you will see a listing of all your messages.
details.cfm - This file will allow you to view the message in detail. (Also provide with a way to reply, delete and download attachments)
download.cfm - This file will allow you to download your attachments.
delete.cfm - This file will allow you to delete a specified message.
reply.cfm - This file will allow you to reply to the message you received.
reply_process.cfm - This will be the page that actually send the reply email.

Let's begin by creating the "application.cfm" file.


File Name : Application.cfm
<!--- Define your POP 3 mail server address, username and password --->
<cfparam name="pop3_server" default="mail.yoursite.com">
<cfparam name=
"pop_username" default="webmaster@yoursite.com">
<cfparam name=
"pop_password" default="Your Password">
<!--- define a path to save attachments --->
<cfset save_path = "D:\email_attachments\">

The variables you set will allow you to easily refer to them anywhere in the application, that's why it's a good thing to create the application.cfm file. Let's continue, the next file we will create is the "Inbox.cfm" file.

^ Top


File Name : Inbox.cfm
<!--- Connect to your POP 3 Server and get all your messages --->
<cfpop action="GETHEADERONLY"
           server=
"#pop3_server#"
           name=
"qGetMessages"
           username=
"#pop_username#"
           password=
"#pop_password#">

<cfif qGetMessages.RecordCount>
    <!--- loop through the list of messages --->
    <table width="100%" border="0" cellpadding="2" cellspacing="1">
        <tr>

            <!--- display a header --->
            <td width="100%" colspan="4" bgcolor="#000080">
                <font size="2" face="Verdana" color="white"><strong>My Inbox</strong></font>
   
            </td>
        </tr>
        <tr bgcolor=
"#efefef">
            <!--- display message count for the current row --->
            <td width="10%"><font size="2" face="Verdana"><strong>ID</strong></font></td>
            <!--- display who the message is from --->
            <td width="20%"><font size="2" face="Verdana"><strong>FROM</strong></font></td>
            <!--- display the subject of the message --->
            <td width="40%"><font size="2" face="Verdana"><strong>SUBJECT</strong></font></td>
            <!--- display a form to view or delete --->
            <td width="30%"><font size="2" face="Verdana">&nbsp;</font></td>
        </tr>
        <cfoutput query="qGetMessages">
        <tr>
            <!--- display message count for the current row --->
            <td width="10%"><font size="2" face="Verdana">#qGetMessages.Currentrow#</font></td>
            <!--- display who the message is from --->
            <td width="20%"><font size="2" face="Verdana">#qGetMessages.from#</font></td>
            <!--- display the subject of the message --->
            <td width="40%"><font size="2" face="Verdana">#qGetMessages.subject#</font></td>
            <!--- display a form to view or delete --->
            <td width="30%" align="center">
            <font size="2" face="Verdana">
                <a href="details.cfm?messagenumber=#qGetMessages.messagenumber#">View</a>
                <a href="delete.cfm?messagenumber=#qGetMessages.messagenumber#">Delete</a>
            </font>
            </td>
        </tr>

        </cfoutput>
    </table>
<cfelse>
    <!--- there are no messages on the server --->
    <font size="2" face="Verdana">There are no messages in the inbox.</font>
</cfif>

^ Top


Ok, now let's create the "details.cfm" page, to actually view the message:

<!--- Make sure that the user has specified a message to view --->
    <cfif NOT IsDefined("messagenumber")>
        <script>
            alert("You did not specify a valid message you wanted to view!");
            self.location="Javascript:history.go(-1)";
        </script>
    </cfif>

<!--- Get the information --->
    <cfpop action="GETALL"
                attachmentpath=
"#save_path#"
                messagenumber=
"#messagenumber#"
                name=
"qGetMessageDetails"
                username=
"#pop_username#"
                password=
"#pop_password#"
                server=
"#pop3_server#">

<!--- Now Display the contents of the message --->
    <cfoutput query="qGetMessageDetails">
        <font size="2" face="Verdana">
           <strong>From:</strong> #from#<BR>
           <strong>Date:</strong> #date#<BR>
           <strong>Subject:</strong> #subject#<BR>
           <BR>
           <strong>
Message:</strong><BR>
            #body#
           <BR>
        <cfif ListLen(attachmentfiles, "#chr(9)#") gt 1>
           <BR>
           <strong>Attachments:</strong><BR>
           <cfset current_attachment = 1>
           <cfloop list=
"#attachmentfiles#" index="attach" delimiters="#chr(9)#">
            <cfif current_attachment neq 1>

             #Evaluate("#current_attachment# - 1")#] 
             <a href="download.cfm?file=#ReplaceNoCase(attach, save_path, "", "ALL")#">#ReplaceNoCase(attach, save_path, "", "ALL")#</a>
             <BR>
            </cfif>
            <cfset current_attachment = current_attachment + 1>
           </cfloop>
        </cfif>

           <BR>
           <a href="reply.cfm?messagenumber=#messagenumber#">Reply</a>
           <a href="delete.cfm?messagenumber=#messagenumber#">Delete</a>
        </font>
    </cfoutput>

Next, let's create a way to download the attachments with the file "download.cfm":

<!--- make sure a file is specified --->
<cfif NOT IsDefined("url.file")>
    <script>

        alert("You did not specify a file to download!");
        self.location="Javascript:history.go(-1)";
    </script>
    <cfabort>
</cfif>

<!--- define the file info to download --->

<CFHEADER NAME=
"Content-Disposition" VALUE="filename=#url.file#">
<cfcontent type=
"attachment" file="#save_path##url.file#">
<!--- redirect the user back to the details page --->
<script>

    self.location="Javascript:history.go(-1)";
</script>

^ Top


The next thing we need to do is a "delete.cfm" page that will allow us to delete a specified message:

<!--- Make sure that the user has specified a message to view --->
<cfif NOT IsDefined("messagenumber")>
    <script>

        alert("You did not specify a valid message you wanted to view!");
        self.location="Javascript:history.go(-1)";
    </script>
</cfif>


<!--- delete the message specified --->
    <cfpop action="DELETE"
              server=
"#pop3_server#"
              username=
"#pop_username#"
              password=
"#pop_password#"
              messagenumber=
"#messagenumber#">

<!--- alert and redirect user to inbox --->
    <script>
        alert("You have successfully deleted this message from your inbox!");
        self.location="inbox.cfm";
    </script>

^ Top


The last page we need to create is a reply page, so we can reply to the incoming messages, we'll call this page "reply.cfm".

<!--- Make sure that the user has specified a message to view --->
<cfif NOT IsDefined("messagenumber")>
    <script>

        alert("You did not specify a valid message you wanted to view!");
        self.location="Javascript:history.go(-1)";
    </script>
</cfif>


<!--- Get the information --->
    <cfpop action="GETALL"
              attachmentpath=
"#save_path#"
              messagenumber=
"#messagenumber#"
              name=
"qGetMessageDetails"
              username=
"#pop_username#"
              password=
"#pop_password#"
              server=
"#pop3_server#">

<cfoutput query="qGetMessageDetails">
    <form action="reply_process.cfm" method="post">
        To: <input type='text' name='to_email' value='#qGetMessageDetails.From#'><BR>
        Subject: <input type="text" name="subject" value="RE: #qGetMessageDetails.Subject#"><BR>
        <BR>
        Message:
        <textarea name="reply_message" rows="10" cols="35">
        
        
        You Wrote:
        #qGetMessageDetails.Body#</textarea>
        <BR>
        <input type="submit" name="Process" value="Send Reply">
    </form>

</cfoutput>

^ Top


Then, the last part of this tutorial will be the "reply_process.cfm" page, this page will actually send the email that the previous page is submitting.

<!--- send out the email reply --->
<cfmail from="you@yoursite.com"
           to=
"#to_email#"
           subject=
"#subject#">
#reply_message# 
</cfmail>

<!--- alert the user and redirect back to the inbox --->
<script>
    alert("You have replied successfully!");
    self.location="inbox.cfm";
</script>

^ Top


That's it, you can now check your own emails and not have to use any email client again :)

If you have questions, Comments? Email Me....

About This Tutorial
Author: Pablo Varando
Skill Level: Advanced 
 
 
 
Platforms Tested: CF5
Total Views: 153,651
Submission Date: November 07, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • The line that currently says: It should be GTE 1, so , so that if you have only 1 attachment, it will still show up here. =)

Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.