Edit 2/20/2013: Be sure to see Karl's comment! I got an interesting question earlier this week. A reader was using cfpop to retrieve email information. They had built a paging system by using getHeadersOnly. This returns a 'slimmer' query where email bodies are not included in the result. He then paged through the query and fetched the bodies 10 at a time. (Do I need to demo that? Let me know and I'll do another blog entry.) This worked fine until the email account got overloaded. Eventually even the "quicker" getHeadersOnly operation was taking forever. He ask - is there some way to get just the total number of messages in a mailbox?
I double checked the documentation for cfpop and didn't see any particular support for this operation. On a whim I tried the following:
<cfpop server="Mail.colts.com" action="getHeaderOnly" username="email@example.com" password="icodephpinthecloset" name="mail" maxrows=1 startrow=999 >
The start row 999 is greater than the total number of messages 5.
So cfpop definitely was able to get the total number of messages, but I saw no way to get that myself. I could have wrapped the call in a try/catch and parsed the error, but that just felt dirty to me.
I did some digging into the javax.mail package which ColdFusion uses under the hood for it's mail support. It was a bit confusing at first, but this article helped a bit and formed the basis for my code. Basically, POP support entails creating a Mail Session (not the same as a ColdFusion session!), a mail store, and finally grabbing the inbox folder. The Folder object contains a method that returns a message count. It took me a good hour or two to figure this out (boy do I appreciate how easy ColdFusion makes things!) but I was able to get it down to a few lines of code:
<cfset props = createObject("java","java.lang.System").getProperties()> <cfset msession = createObject("java", "javax.mail.Session")> <cfset m2 = msession.getDefaultInstance(props)> <cfset store = m2.getStore("pop3")> <cfset store.connect("Mail.colts.com","firstname.lastname@example.org","isecretlywritephp")> <cfset df = store.getFolder("INBOX")> <cfset df.open(df.READ_ONLY)> <cfdump var="#df.getMessageCount()#">
The main line you would care about is the connet operation. Note the server, username, and password (yes, it's fake). The df variable is my folder object. You can do more with it then just getting a count obviously, but actually getting mail would be simpler with cfpop. If you actually used this in production you would probably want to turn it into a UDF.