Solved

cfimap - download email as an attachment

Posted on 2014-01-30
10
473 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
ID: 39821520
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_
ID: 39829809
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
ID: 39892359
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_
ID: 39894657
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

Expert Comment

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

Author Comment

by:HumanScaleDev
ID: 39910726
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_
ID: 39931958
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
ID: 39984183
thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

21 Experts available now in Live!

Get 1:1 Help Now