Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!)
Hi,
I'm working on a Perl script that handles e-mails and attachments using MIME::Tools. This script saves the attachments on filesystem, but whenever it finds an "&" (ampersand) into the filename (the filename is given by the "path" method of the "MIME::Body" class), it is changed into "%26" and then stored on disk. To make an example, if an attachment is named "You & me.pdf", it is saved on disk as "You %26 me.pdf". I also tried to change that char with a regexp and setting again the path passing the new (and working) value as a parameter to the "path" method, and if I print the value it is right, but on the filesystem is *always* stored with the "%26" string. Any suggestion on how to bypass this problem? Thanks in advance for any precious help. :-D |
Quote:
Regarding '&' vs '%26' - file names with special characters are not shell friendly. For example, if in a shell you type cat some&thing , the shell will do the following: Code:
sergei@amdam2:~/junk> cat some&thing You can still have almost any character in a filename - except for '/' which is, of course, path element separator. without seeing you code it's unnecessarily difficult to to guess what exactly needs to be changed. |
Yeah, I know that concepts, and I know that an ampersand in a file name may be "dangerous" somehow (even if the shell normally escapes it).
But as long as I can do Code:
touch 'This file contains an &.pdf' I haven't written the Perl script by myself, I'm just modifying it in some parts, by the way the situation is the following: there is a while loop looping through the parts of an Entity (parsed with MIME::Parser), so there is the following line: Code:
[...] Moreover there is this line: Code:
[...] Now, if I try to change the "&" into something else (for example, "and") with: Code:
$fileName2 = $entity->parts($i)->bodyhandle->path; What a crap... :( |
Quote:
So, why's the fuss about crap ? |
Just because you can
[quote] touch 'This file contains an &.pdf' [/quote ] doesn't mean you should. Using special chars like space, & etc in filenames WILL cause you endless grief... as I said in the other version of your qn in another forum here. |
Fix..
If it really irritates you, just edit your instance of MIME::Parser::Filer to add & to the list of characters allowed in evil_filename. Or alternately, you can derive a class from MIME::Parser::Filer and jump thruogh a lot of hoops. ;-)
I changed mine to return 1 if ($name =~ /[^-A-Z0-9_+=.,@\#\&\$\% ]/i); # Only allow good chars |
All times are GMT -5. The time now is 08:06 PM. |