Solved

cfimap - download email as an attachment

Posted on 2014-01-30
10
480 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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