Coldfusion Code to get the mail attachments for unread mails using cfimap
The following code opens a connection 'Conn' using cfimap for the given username and password.
Also it fetches the unread emails with attachments.
<cfimap action="open" connection="Conn" server="imap.gmail.com" username="test@gmail.com" password="test" secure="yes" >
<cfimap action="getall" connection="Conn" name="Query_getAttachments" folder="Inbox" attachmentpath="#GetTempDirectory()#" >
<cfquery dbtype="query" name="getMailAttachments">
select * from Query_getAttachments
where seen=<cfqueryparam value="no" cfsqltype="cf_sql_varchar">
and ATTACHMENTS is not null
</cfquery>
<cfdump var="#getMailAttachments#">
also if we want to copy the mail attachment from the Temp folder to our server folder, we can use cffile action="copy" or cffile action="move" whcih should be inside the cfloop.
For example,
<cfloop query="getMailAttachments">
<cffile action="copy"
destination="path"
nameconflict="MakeUnique"
source="#getMailAttachments.ATTACHMENTFILES#">
</cfloop>
Also we can update the status of the mails as 'READ' by using the following line inside the cfloop.
<cfimap action="MarkRead" connection = "Conn" messagenumber="#getMailAttachments.MESSAGENUMBER#">
For getting multiple attachments, you can refer : Coldfusion code to get multiple mail attachments using cfimap
Also it fetches the unread emails with attachments.
<cfimap action="open" connection="Conn" server="imap.gmail.com" username="test@gmail.com" password="test" secure="yes" >
<cfimap action="getall" connection="Conn" name="Query_getAttachments" folder="Inbox" attachmentpath="#GetTempDirectory()#" >
<cfquery dbtype="query" name="getMailAttachments">
select * from Query_getAttachments
where seen=<cfqueryparam value="no" cfsqltype="cf_sql_varchar">
and ATTACHMENTS is not null
</cfquery>
<cfdump var="#getMailAttachments#">
also if we want to copy the mail attachment from the Temp folder to our server folder, we can use cffile action="copy" or cffile action="move" whcih should be inside the cfloop.
For example,
<cfloop query="getMailAttachments">
<cffile action="copy"
destination="path"
nameconflict="MakeUnique"
source="#getMailAttachments.ATTACHMENTFILES#">
</cfloop>
Also we can update the status of the mails as 'READ' by using the following line inside the cfloop.
<cfimap action="MarkRead" connection = "Conn" messagenumber="#getMailAttachments.MESSAGENUMBER#">
For getting multiple attachments, you can refer : Coldfusion code to get multiple mail attachments using cfimap
Comments
Post a Comment