Transformed attachment filenames

0
I am using the IMAP-POP3 email module. On certain email attachments (only some pdf's) the attached filenames are being changed as the emails are read from the external mail server. The string '=-iso-8859-1-Q-' is added to the start of the filename  and '-=' is added to the file extension. as an example      170214_B_KFI_=A3650k_fixed.pdf is transformed to      =-iso-8859-1-Q-170214_B_KFI_=A3650k_fixed.pdf-= which means that the file cant ve viewed. Any ideas. Additional - I'm fairly sure the problem is to do with encoding as one pre-fix I get is 'iso-8859' and the other is 'utf-8' but I dont know how to influence this within Mendix.  
asked
1 answers
0

Hi Ray,

I'm pretty sure your mail server is encoding the filenames improperly. The file name should be encoded like this when it's passed over the wire (note the question marks in place of hyphens in your example):

=?iso-8859-1?Q?170214_B_KFI_=A3650k_fixed.pdf?=

If that were passed into the Java code in the mail module, it would have been correctly decoded as:

170214 B KFI £650k fixed.pdf

 

So, the problem exists on your mail server. I believe this issue is something worth sharing with your mail provider.

I don't see a bulletproof way to work around the issue from your end since you don't know what's actually happening on the server side.

Here's a workaround, but fair warning, there may be other encoding problems on your mail server that aren't accounted for here. It might even fail if there are hypens in file names:

You would need to replace the hyphens in your example text with question marks, but then fix the question marks that would appear in your character set description (i.e., change "iso?8859?1" back to "iso-8859-1"). Then you'd have a properly encoded string that you could decode using:

MimeUtility.decodeText(encodedString);

 Putting that all together, I think you could change line 424 in <project directory>/javasource/imap_pop3_email/actions/EmailHandler.java from:

attach.setName(bodyPart.getFileName());

to:

attach.setName(MimeUtility.decodeText(bodyPart.getFileName().replace('-', '?').replace("iso?8859?1", "iso-8859-1")));

 

answered