Solved

cfimap - download email as an attachment

Posted on 2014-01-30
10
467 Views
Last Modified: 2014-04-07
Hello,

I'm using cfimap to store the contents of emails in our DB as well as to download documents attached to the email.  Our users are requesting to have the actual email downloaded as an attachment as well for reference. Can this be done?

Thanks!
0
Comment
Question by:HumanScaleDev
  • 5
  • 3
10 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
It can be done, yes. But not with CFIMAP. It doesn't expose the raw message, only the "parsed" message parts.  

If you need the raw message, you'll have to use a low level java library to connect to the mail server, grab the raw messages, and save them as binary.  Not a simple task.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
HumanScaleDev - Can I ask why the "B" grade? I know it is not the answer you wanted to hear, but unfortunately "not possible with cfimap" is the correct answer.

(I'd be happy to explore the java approach, but given its greater complexity, you didn't say whether or not that was an option here)
0
 

Author Comment

by:HumanScaleDev
Comment Utility
Where can I see the grade of this question? Sorry I was not familiar with the way EE handles grades. I was assuming 500 points was the rate reflected on the person that answers the question and the grade on the question itself. Meaning, if I would be looking into this and found this question, I'd loved to find an example on how this could be achieved. Anyway, if grades and points are not tied together and they reflect on the person that answer the question, then ok, I'd give _agx_ and A.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
I'm not that familiar with where they're shown when asking a question.  I *think* they're visible when you close the question, maybe in your profile history too?

> I'd loved to find an example on how this could be achieved.

Sure.  I'm on a deadline today, but I'll put up an example this w/e.  In summary - unfortunately given that cfimap doesn't support it you have two choices:

1) process everything (ie email details: sender, date, subject, ...) in java -OR-
2) use CFIMAP to process the details and use java to get a copy of the whole message

Though it's a bit of double work, I'd go with option #2 because it's much simpler. Then you don't have to write a ton of extra code to do the rest, since CFMIMAP does that already. The only thing it doesn't give you is a copy of the full message.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 52

Expert Comment

by:_agx_
Comment Utility
I haven't forgotten about this, I'm just swamped with work since last week.
0
 

Author Comment

by:HumanScaleDev
Comment Utility
Don't worry about it. I'm sure if someone else needs it they'll post a question about it :). We're good. Thanks!
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Finally had a chance to get back to this! (Sorry for the delay) Like I mentioned earlier, there's 2 approaches. The simpler one is use java only to get and save the message, and keep using cfimap for everything else.

To save the message, you 1st need to connect to the mail server. Obviously different mail servers have different settings (port number, folders, ssl, tls, etc...) so the actual code may vary. Here is an translated example for gmail that I know works w/CF10:

<cfscript>
	// initialize mail session props
	props = createObject("java", "java.util.Properties").init();
	props.setProperty("mail.store.protocol", "imaps");
	// session is reserved word in CF
	JavaXSession = createObject("java", "javax.mail.Session");
	mailSession = JavaxSession.getDefaultInstance(props, javacast("null", ""));
	mailStore = mailSession.getStore("imaps");
	mailStore.connect("imap.gmail.com", "someUserName@gmail.com", "somePassword");
	mailFolder = mailStore.getFolder("Inbox");
	mailFolder.open(mailFolder.READ_ONLY);

        // .... code to retrieve and save messages

       // close when finished
       mailStore.close();
    
</cfscript>

Open in new window


Once you've successfully connected, check the number of messages with mailFolder.getMessageCount().  Then use a loop to save each message to a file.  Obviously you don't want to try and save a gazillion messages at once... the page will probably time out.  Here's how you could do it in small batches:

	
<cfscript>
      //... connect to mail server

       // TEST: save maximum of 5 messages only
	maxMessages = 0;
	if (mailFolder.getMessageCount() > 0) {
		maxMessages = Min(mailFolder.getMessageCount(), 5);
	}

	FileOutputStream = createObject("java", "java.io.FileOutputStream");	
	// save a maximum of 5 messages ....
	for (i = 1; i < maxMessages; i++) {
		// get current email
		message = mailFolder.getMessage(i);	
		// generate a unique file name 
		saveToFile = expandPath("./"& createUUID() &".eml");
		// save the email to file
		outStream = FileOutputStream.init(saveToFile);
		message.writeTo(outStream);
		outStream.close();
		WriteOutput("<br>[#i#] saveToFile = #saveToFile#");
	}

	// always close when finished
    mailStore.close();
</cfscript>

Open in new window


That is a greatly simplified example, but hopefully should get you started.
0
 

Author Comment

by:HumanScaleDev
Comment Utility
thank you!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now