Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In ColdFusion 8, we could access to an e-mail Inbox via the CFPOP tag. Unfortunately that means sucking mail down to cf server and dealing with things locally. We didn't have much control over the Inbox itself. In ColdFusion 9, we can connect to a mailbox via IMAP and have complete control over the Inbox including creating and deleting folders, moving mail around on the server and performing any other action that a user could perform directly on their own Inbox. So, I am going to show you how to connect to an IMAP account, how to pull e-mail out of that account and how to create and delete folders inside of that account.
So, in our Chapter9 folder, open up imap.cfm. In this file, the first thing I am doing is opening a connection to the mail server. So in this case I have action equals open, I have specified the server, the username and the password that I want to use to connect, and I am naming this connection. I am going to be reusing this connection throughout the video to perform actions on the mailbox. When I am all done with those actions, I am going to close that connection, so I don't leave any open imap connections on the server. So I am going to switch over to Safari real quick and I am already logged into this mail account, so we can take a look and see what's inside.
So I have two new emails here in the Inbox and in the Test Folder, I have one email that's already been read. So let's go back to cfbuilder and the first thing I am going to do is get a list of folders inside of the mailbox. Using the cf imap tag, we will choose action = and I'm going to list all folders. Now I need to specify the connection that I want to use and I need to specify the name of the variable that this information should be stored in.
So, let's call that FolderList and then I'm going to dump the value of FolderList out to the browser. Let's switch over to preview and we can see it tells us I have the Inbox. This tells me how many new messages I have in my Inbox, the total number of messages and how many of those are unread. So I can see I have Inbox and Test Folder. Let's take a look at the messages in Inbox. Switch over to source and let's do cfimap action = getheaderonly.
So I am only going to get the email headers, not the actual contents of the emails. Again I am going to use the current connection and I am going to put the data in to a variable called email list. I will dump out the email list and let's preview our file again. So now we can see all the headers for the two emails that are sitting in my Inbox. As you can see, I have a lot of information available to me. I have whether or not this particular email has been answered, CC's, whether it's been marked as deleted, draft, flagged, who it's from, the actual full email headers, so I can see who that it came from and how it got there.
If I scroll over to the right, I can see how many lines are in the email, its message ID, the number in the Inbox, a whole bunch of stuff. One of the more important values to notice here is the UID. This is the unique ID for this particular email inside of the mail server itself. So in this case these two email messages have UID's of two and three. So I just want to get just one particular email, if I go back to my source here, I can specify the UID in here.
So I only want to get the email with the UID of three, so we'll save and preview our file again, and now I'm just getting the headers for that single email. You should also notice that here that these messages have now been marked as read. They been pulled into ColdFusion through the cf imap, and during that process they have been marked as read. Now that is all well and good. I can see everything about the email other than the content itself. So if I go back to my source view and choose getall and then preview the file.
Now I am getting everything about that email including information on attachments and the actual content of the body of the email itself. Now, we can do more than just read in emails. I can also go in and create new folders. So let's add a new folder in our mailbox, so I am going to take this and comment out our getall code, and we will do cfimap action = CreateFolder. We use out CurrentConnection again and then I am going to create the folder under Test Folder/New Folder.
So, let's load that page up and we can see it's still dumped our folder list but we don't see our new folder here. Let's check and make sure it actually created it in the mailbox. So we will switch back to Safari and we will reload our page to reload the folder that's over there on the left and we see out plus icon and sure enough there's our New Folder. So in order to get that to display in our ColdFusion query, we will switch back to the source code and I am going to comment out the create folder code now otherwise, I will give an error because it will say that the folder already exists.
And on our listallfolders action, I will say recurse equals true. Now, If I will load this up, it will recurse through and pick up all subfolders as well. So now we can see Inbox, Test Folder and Test Folder/New Folder. Now the last thing we will do is delete the Test Folder we created, so we can clean up our mess, so do cfimap, action = deletefolder, and we will use our CurrentConnection and the specify the folder that we want to delete.
So let's load that file up and sure enough we can see it's no longer in our dump and if we go back to Safari and reload out page, New Folder has disappeared from our left-hand side bar. So, as you can see with the new cfimap tag, I have total control over your email Inboxes directly from ColdFusion. You can now easily manage automated emails in the shared Inbox, or even build your own custom email client with just a few well placed ColdFusion tags.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96273 Viewers
56 Video lessons · 110045 Viewers
71 Video lessons · 78853 Viewers
131 Video lessons · 37836 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.