POP3 Email Client with Attachments
By Robert Jones

This tutorial is quite simple if you are familiar with many of the basic practices of ColdFusion. It outlines exactly how to create a webbased email client for all of your clients currently hosted by your company. It is basically an expansion on Pablo Varando's Email client, which is all well and good for those of us that are web-savvy and dont care about alert boxes, but in my experiance clients that aren't very familiar with the internet tend to freak out and think something is wrong with their computers when they see and alert box, which tends to be a hassle when you provide customer support and they are calling you every five seconds because of a simple harmless alert.

Therefore I set out to provide an extreamly user friendly email sending and reciveing client. The files associated with thie tutorial are as follows:

application.cfm - A simple session management application
index.cfm - this is our login page, you could realistically name this one login.cfm, but remember we are going for user friendlyness
inbox.cfm - This one should be obvious, delete.cfm and send.cfm are set as cfincludes here
details.cfm - viewing messages and providing a way to reply/forward/delete the messages
compose.cfm - create new messages, reply to messages, and also forward messages
delete.cfm - delete messages from the POP3 server
send.cfm - send messages
upload.cfm - this is used for attaching messages to outgoing emails.
LinkFinder.cfm - a simple custom tag that parses urls in received messages and makes them active http://www.cfcomet.com/utilities/CF_LinkFinder_v1-1.zip


File: application.cfm
this file isn't to hard to figure out, it's simply to manage active sessions you can set the timespan to be how ever long you feel neccisary

<cfapplication name="name" sessionmanagement="yes" sessiontimeout="#CreateTimeSpan(0,0,10,0)#">

^Top


File: index.cfm
This is our login screen, it gives the user the opportunity to remember their user ID so that they don't have to enter it everytime they come to check their email, because people like that sort of thing ;-P

Note: I only use this system for those of my clients that are hosted on MY system and thus all have the same POP3 server, but it is easily adaptable for those of you who wish to use it for anyone.

<!--- If the user has logged out, we clear the session variables --->
<cfif IsDefined('logout')>
  <cfscript>
    StructClear(Session);
  </cfscript>
<!--- if the cookie was set and the user wants to sign in as someone else, we clear the cookie --->
<cfelseif IsDefined('newuser')>
  <cfcookie name="username" expires="now">
  <cfscript>
    StructClear(Cookie);
  </cfscript>
</cfif>

<!--- If the user is still active we simply redirect them right to their Inbox --->
<cfif IsDefined('session.username') and IsDefined('session.userpass')>
  <cflocation url="inbox.cfm" addtoken="yes">
</cfif>


<!--- Our login form --->
<form method="post" action="inbox.cfm?login=try">
<table border="0" width="80%" cellspacing="0" cellpadding="0" align="center">

<!--- If we have a session timeout or an invalid username/password, display an error message here --->
<cfif IsDefined('error')>
  <tr>
     <td colspan="2" align="center">
       <span style="color: red">* Invalid Username or Password</span>
    </td>
  </tr>
</cfif>

   <tr>
     <td align="right" width="50%">
       <font face="Arial" size="2">Username:&nbsp;</font>
     </td>
     <td align="left" width="50%">
<!--- If the user has chosen to remember their username we insert it as a hidden feild and display it in bold letters --->
       <cfif IsDefined('cookie.username')>
         <cfoutput>
           <input type="hidden" name="userNAME" value="#cookie.username#">
           <font face="Arial" size="2"><b>#cookie.username#</b></font>
         </cfoutput>
<!--- If they haven't set thier cookies, we display a text input --->
       <cfelse>
         <input type="text" name="userNAME" size="30" >
       </cfif>
     </td>
   </tr>

<!--- We do not want to remember the users password in the cookie, so this is just a plain ole' password field --->
   <tr>
     <td align="right" width="50%">
       <font face="Arial" size="2">Password:&nbsp;</font>
     </td>
     <td align="left" width="50%">
       <input type="password" name="userPASS" size="30">
     </td>
   </tr>

   <tr>
     <td colspan="2" align="center">
<!--- If the cookies haven't been set yet, we ask the user if the WANT them set via a checkbox --->
        <cfif NOT IsDefined('cookie.username')>
          <input type="checkbox" name="remember" value="indeed"><font face="Arial" size="2"> Remember ID on this Computer?</font>
<!---
If the cookies ARE set we ask them if the are the user that is set by the cookie, if not they can log out, if so we pass the value of "remember" along in a hidden field
--->

        <cfelse>
          <cfoutput>
            <input type="hidden" name="remember" value="indeed">
            <font face="Arial" size="2">Not #cookie.username#? <a href="index.cfm?newuser=true">Login as a different user</a></font>
          </cfoutput>
        </cfif>
     </td>
   </tr>

   <tr>
     <td colspan="2" align="center">
       <input type="submit" value="Log In">
     </td>
   </tr>

</table>
</form>

^Top


File: inbox.cfm
This is the meat and potatoes of our application, without this page, we wouldn't be able to Recieve, send OR delete Messages, so its a tad bit important.
The formatting and layout of this page can be EASILY changed to fit you needs, but you probably already knew that right?

<!---
Some if/else statements to start us off here
First we find out if the user filled out the login form,
or if they were simply redirected because their session
was still valid.
--->

<cfif IsDefined('login')>
<!---
If the user did use the login form,
we check to make sure the user entered data,
if so, we set the form variables up as session variables for easy use
--->

<cfif IsDefined('form.userNAME') and #form.userPASS# is not "">
  <cfset session.username=#form.userNAME#>
  <cfset session.userpass=#form.userPASS#>
<!---
If they want us to remember there login info We'll set a cookie
otherwise we will set a cookie with the same name to expire now
that way there will deffinently be no cookies set on the users machine
--->

    <cfif IsDefined('form.remember')>
      <cfcookie name="username" value="#form.userNAME#" expires="never">
    <cfelse>
      <cfcookie name="username" expires="now">
    </cfif>
<!---
If the user didn't use the login form OR they didn't enter valid information
Redirect them to the index screen with error set to true
--->

  <cfelse>

    <cflocation url="index.cfm?error=true" addtoken="yes">
  </cfif>
</cfif>

<!---
Here is where the sending and deleting includes come in
We check to see if an action is defined at all,
then we find out what that function is and include the appropriete file for that action
--->

<cfif isDefined('action')>
  <cfif action is "send">
    <cfinclude template="send.cfm">
  <cfelseif action is "delete">
    <cfinclude template="delete.cfm">
  </cfif>
</cfif>

<!---
This is where we retrieve the messages from the POP3 server
We've surrounded the CFPOP tag with a CFTRY statement
This is another way to check to be sure that the user entered valid data
if there are any errors thrown back at us we redirect the user to the login screen
this is what they are used to anyway right?
--->

<cftry>
  <cfpop action="getheaderonly" name="qGetMessages" server="mail.yoursever.com" timeout="90" username="#session.username#" password="#session.userpass#">
<cfcatch type="Any">
  <cflocation url="index.cfm?error=true">
  <cfabort>
</cfcatch>
</cftry>

<!---
This is just a header,
I like to have an image here at the top,
you can of course put whatever you want here
--->

<table width=
"100%" border="0">
  <tr>
    <td align="left">
      <img src="images/logo.gif">
    </td>
    <td align="right">
      <font size="2" face="Arial">
      <cfoutput>
       #session.username#<br>
      <a href="index.cfm?logout=true">Log Out</a>
      </font>
      </cfoutput>
    </td>
  </tr>
</table>

<table width="100%" border="0" cellpadding="3" cellspacing="0">
  <tr bgcolor="#000000">
    <td width="44%">
      <font size="2" face="Arial" color="white">
      <b>Inbox</b>
      </font>
    </td>
    <td width="54%" align="center">
      <font size="2" face="Arial" color="red"><b>
<!---
This is our conformation cell,
if the user sent or deleted something,
this is where we tell them
Its just one of those user friendly kinda things
--->

         <cfif
IsDefined('sent')>
           --MESSAGE SENT--
        <cfelseif IsDefined('delete')>
           --MESSAGE DELETED--
        </cfif>
      </b></font>
    </td>
    <td width="1%" colspan="2" align="right">
      <a href="inbox.cfm">Refresh</a>
    </td>
    <td width="1%" align="right">
      <font color="#FFFFFF">&nbsp;||&nbsp;</font><a href="compose.cfm">Compose</a>
    </td>
  </tr>
</table>


<!---
This is our column headers,
They are displayed regaurdless of weather or not there are messages
so there is really nothing all that special about them
--->

<table width="100%" border="0" cellpadding="2" cellspacing="1">
  <tr bgcolor="#efefef">
    <td width="14%" colspan="2">
      <font size="2" face="Arial">
        <b>DATE</b>
      </font>
    </td>
    <td width="20%">

About This Tutorial
Author: Robert Jones
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX
Total Views: 110,064
Submission Date: June 19, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • Hi there... Im trying to use this tutorial to make a email client for my website... i wrote the tutorial exacttly as u have in t his page... but when i try to open the index.cfm the system send me this error: Error Occurred While Processing Request Errors reported by Java compiler: Found 1 semantic error compiling "C:/CFusionMX/wwwroot/WEB-INF/cfclasses/cfApplication2ecfm623464128.java": 1. public final class cfApplication2ecfm623464128 extends coldfusion.runtime.CFPage{ <-------------------------> *** Error: Cannot write class file "cfApplication2ecfm623464128.class" because that name conflicts with the name of the class file "cfapplication2ecfm623464128.class" in directory "cfclasses". This is illegal because file names are case-insensitive in this system. . can u help me please? i dont know why this error appear

  • when i try to view some of my emails i get the following error: An exception occurred when retrieving mails. The cause of this exception was that: java.security.PrivilegedActionException <>. any ideas? thanks.

  • Thanks for the tutorial. Got it up and running in about 10 minutes and it works great....with one exception. Where and how (if it does) store the downloaded emails? After I refreshed the screen (using the link), the emails listed in my inbox disappeared and when I went to check the dir made, there was nothing there. I plan on storing the messages in a db but they keep disappearing!! :) Thanks for the assist.

  • Robert, thank you for your scrips. I'm a Coldfusion beginner and interested in email system. You scrips are very helpful. I have a question about index.cfm. I found that "StructClear(Session); StructClear(Cookie);" always showed up in the browser. Please click on the following address: http://publish.uwo.ca/~llin24/index.cfm Should I put the following part in the section, in stead in the section? StructClear(Session); StructClear(Cookie); Thank you very much for your help. Lixia

  • I created an office web client, and all the emails are stored into the database, once they retrived from the server. However when I try to read the multiple attachments with the same name in an email,without using generateUniquefilenames, they are overlapped. I am not supposed to use this feature because, users of web client will not be able to understand the filenames they uploaded. So, how is it possible to read one attachment at one time from an email, such that I can upload a file with unique file name on the server and store into database as "serverfilename" and "clientfilename" of whay users uploaded. Thank you. Sushma.

  • I'll post this here so theres no confusion. the inbox is simply where everything gets dumped from the POP3 server, if you want filters and the like you will have to set them up in a database and then hand code the filters, I could do it for you, but that would be cheating on your part ;P The same goes for an adress book. It's all simple database work.

  • is there a way to create different folders to store messages in aside from the inbox and then move them around? Thanks Bruce

  • Grrrrrrrrrrrrrrr.....I cant get to the site to find link finder does anyone have a copy of it lol! I would be very greatful!

  • To forward the attachments, simply make the follwing changes to the script. Firstly in the "details.cfm" make this change to the "actions" links: ... Reply Forward Forward With Attachments Delete ... Next in the "compose.cfm" page make these changes: ... ... Files Attached: #ThisFilePath#  Attach Files:  ... Now, I'm simply shooting from the hip here and I've never actually tested this method, and have never had a use for it to be honest, but you can try it out, the only place I can see snagging is the hidden input for the attachments, you may run into problems because the server is going to try and upload a file from itself, so I' don't know how that will behave, but it should work.... I think.

  • Hi. I found this program quite usefull and i am using this program for our office as an web client. But i shall be grateful if you could tell me how can i forward the attachments along with the message. thanks and regards

Advertisement

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