Solved

cfimap - download email as an attachment

Posted on 2014-01-30
10
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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
 
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CFSelect not working correctly when query item has commas in it in ColdFusion 1 83
Coldfusion Datefield problem 2 137
Attendance Tracking 3 24
SpreadsheetAddRow Sytax Error 4 13
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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…

751 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