Accessing messages and attachments in gmail via cfpop can be done in a relatively straightforward way. An example of connecting to and reading messages and attachments from gmail is shown here.
Enable POP on gmail
First things first. POP access must be enabled on the gmail (or other) account before attempting to download the mail. To enable POP, go to the Forwarding/POP and IMAP panel in Settings. Enable POP and save the settings. Tip: you can reset this and download the same messages again while testing by enabling pop again.

SSL
gmail requires SSL to access it via POP. Unfortunately, the cfpop tag does not include SSL. It can be turned on with the following code:
<cfset javaSystem = createObject("java", "java.lang.System") />
<cfset jProps = javaSystem.getProperties() />
<cfset jProps.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory") />
<cfset jProps.setproperty("mail.pop3.port",995) />
<cfset jProps.setProperty("mail.pop3.socketFactory.port", 995) />
cfpop
Use cfpop wrapped in a cftry surrounded by a cflock to retrieve the emails. POP is slow and may timeout on you if there are lots of messages and lots of attachments. If that is the case, you may want to retrieve just the headers and selectively grab the messages using the UID. This example grabs everything.
<cflock name="cfpop-#form.username#" type="exclusive" timeout="50">
<cftry>
<cfpop server="#form.popserver#" username="#form.username#" password="#form.pwd#"
action="getAll" name="getEmail" attachmentpath="#attachmentFolder#" generateUniqueFilenames="true">
<cfcatch type="any">
<h2 class="error">Error Message</h2>
<p><strong>Type:</strong> #CFCATCH.Type#</p>
<p><strong>Message:</strong> #CFCATCH.Message#</p>
<p><strong>Detail:</strong> #CFCATCH.Detail#</p>
<cfabort>
</cfcatch>
</cftry>
</cflock>
Attachments
By default the attachments will be downloaded to the temp directory of the server. You can find out where that is with the GetTempDirectory() function.
<cfoutput>#GetTempDirectory()#</cfoutput>
Or, you could create a folder specifically for the attachments.
<cfset attachmentFolder = ExpandPath("attachments") />
Attachment names and files are tab-separated lists in ColdFusion. Putting these lists in an array makes for easy looping later. Set this up by creating the empty arrays.
<!--- Array for attachment name(s) ---> <cfset attachmentNameArray = ArrayNew(1) /> <!--- Array for attachment file(s) ---> <cfset attachmentFileArray = ArrayNew(1) />
Emails
Looping backwards over the getEmails query returned by cfpop puts the output in the same order as the inbox with the newest message first. The attachments are stuffed in the arrays using the ListtoArray() function and then looped over to display what and where they are.
<p><strong>Number of Records:</strong> #getEmail.recordCount#</p>
<p><strong>FYI - TempDirectory:</strong> #GetTempDirectory()#</p>
<ul>
<cfloop from="#getEmail.recordCount#" to="1" step="-1" index="i">
<li><strong>Row: #i#</strong><br />
<em>MessageNumber:</em> #getEmail.messagenumber[i]#<br />
<em>UID:</em> #getEmail.UID[i]#<br />
<em>From:</em> #getEmail.From[i]# <br />
<em>Subject</em>: #getEmail.Subject[i]#<br />
<em>Message:</em> #getEmail.body[i]#<br />
<cfif Len(getEmail.attachments[i])>
<cfset attachmentNameArray = ListtoArray(getEmail.attachments[i], chr(9)) />
<cfset attachmentFileArray = ListtoArray(getEmail.attachmentfiles[i], chr(9)) />
<em>Attachments:</em><br />
<cfloop from="1" to="#ArrayLen(attachmentNameArray)#" index="num">
#attachmentNameArray[num]#<br />
</cfloop>
<em>Attachment Files Server Location:</em><br />
<cfloop from="1" to="#ArrayLen(attachmentFileArray)#" index="num">
#attachmentFileArray[num]#<br />
</cfloop>
</cfif>
</li>
</cfloop>
</ul>
SSL off
Turn off SSL and it’s all done. SSL may or may not interfere with other processes on the server. Turning it off helps alleviate that.
<!--- Turn off SSL --->
<cfset jProps.setProperty("mail.pop3.socketFactory.class", "javax.net.SocketFactory") />
Other Pop-enabled Accounts
This example can be used with other pop-enabled accounts. Try it without SSL first then add that in if needed.
cfpop documentation from Adobe


Twitter
About