12-10-2008
My code was meant to show you the general process of MIME decoding (and mostly concept). It was not quite good for production use. Parsing a real-world email message is likely slightly more complex due to existence of variations.
To be frank, if you can get hold of PHP 5, as indicated in the inline comment, the simplest approach would be to use the iconv_mime_decode() function which is a one-stop shop of what you want. There was a (intentional) flaw in my posted code because it didn't handle the case where the encoding is quoted-printable, that is also supported by MIME. For simplicity, I only posted the part which decodes Base64, because that was used in your sample posted.
If you get hold of the concepts needed, you may then check other languages or tools to see if they may better suit your environment compared with PHP. As PHP install is typically pretty big, it may not be necessarily suitable in all deployment environments (say on very limited storage space).
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi, I'm using putty and when I try to write ü it writes | (or when I try to write é , it writes i)
I tried to change settings/translation of putty but with no success
I have KSH
# locale
LANG=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"... (3 Replies)
Discussion started by: palmer18
3 Replies
2. Shell Programming and Scripting
Hi,
I'm trying to strip all lines between two headers in a file:
### BEGIN ###
Text to remove, contains all kinds of characters
...
Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c
om/updates/
Antispyware-Downloadserver.com #2... (3 Replies)
Discussion started by: Trones
3 Replies
3. Shell Programming and Scripting
Hi ,
I have a typical situation. I have 4 files and with different headers (number of headers is varible ).
I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only).
For example -
File 1
H1|H2|H3|H4
11|12|13|14
21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies
4. Shell Programming and Scripting
Hi experts,
I have a gz file from other system(solaris), which is ftped to our system(solaris).
After gunzip, the file is a xml file and we are using ORACLE built in xml transformiing tool ORAXSL to transform XML to TXT.
Now the issue is we come accross issue regarding UTF8 as below:... (1 Reply)
Discussion started by: summer_cherry
1 Replies
5. Shell Programming and Scripting
Hi,
I am beginner to Unix.
My requirement is to validate the encoding used in the incoming file(csv,txt).If it is encoded with UTF-8 format,then the file should remain as such otherwise i need to chnage the encoding to UTF-8.
Please advice me how to proceed on this. (7 Replies)
Discussion started by: cnraja
7 Replies
6. Shell Programming and Scripting
Hi ,
I have a sql query in the unix script ,whose output is shown below.I want to convert this output to HTML table format & send email from unix with this table as email body.
p_id src_system amount
1 A 100
2 B 200
3 C ... (3 Replies)
Discussion started by: jagadeeshn04
3 Replies
7. UNIX for Dummies Questions & Answers
Hi All
I am working on AIX 7.1 and I am trying to show an output that I get from "cat" a log file to email. However in email I get the below output:
In the script I have defined the colors as:
#!/bin/sh
echo "\033
Below is the script I have created to send this output:
... (9 Replies)
Discussion started by: Bubs
9 Replies
8. UNIX for Dummies Questions & Answers
I'm trying to pick up some Unix SysAdmin skills on my own outside of work through the use of the "Unix and Linux System Administrators Handbook." I've found the exercises to be very beneficial, until I came to this.... "What path did the email take? To Whom was it addressed, and to whom was it... (0 Replies)
Discussion started by: ksmarine1980
0 Replies
9. UNIX for Dummies Questions & Answers
Hi all
I need help converting a text file into a html table in bash and I need to email this table. The text file looks like the below. Two columns with multiple rows. Top row being header.
Application Name Application Status
Application 1 Open
Application 2 ... (2 Replies)
Discussion started by: hitmanjd
2 Replies
10. Solaris
Hi all!!
I´m using command file -i myfile.xml to validate XML file encoding, but it is just saying regular file . I´m expecting / looking an output as UTF8 or ANSI / ASCII
Is there command to display the files encoding?
Thank you! (2 Replies)
Discussion started by: mrreds
2 Replies
LEARN ABOUT CENTOS
mime::base64
MIME::Base64(3pm) Perl Programmers Reference Guide MIME::Base64(3pm)
NAME
MIME::Base64 - Encoding and decoding of base64 strings
SYNOPSIS
use MIME::Base64;
$encoded = encode_base64('Aladdin:open sesame');
$decoded = decode_base64($encoded);
DESCRIPTION
This module provides functions to encode and decode strings into and from the base64 encoding specified in RFC 2045 - MIME (Multipurpose
Internet Mail Extensions). The base64 encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly
readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used, enabling 6 bits to be represented per printable character.
The following primary functions are provided:
encode_base64( $bytes )
encode_base64( $bytes, $eol );
Encode data by calling the encode_base64() function. The first argument is the byte string to encode. The second argument is the
line-ending sequence to use. It is optional and defaults to "
". The returned encoded string is broken into lines of no more than 76
characters each and it will end with $eol unless it is empty. Pass an empty string as second argument if you do not want the encoded
string to be broken into lines.
The function will croak with "Wide character in subroutine entry" if $bytes contains characters with code above 255. The base64
encoding is only defined for single-byte characters. Use the Encode module to select the byte encoding you want.
decode_base64( $str )
Decode a base64 string by calling the decode_base64() function. This function takes a single argument which is the string to decode
and returns the decoded data.
Any character not part of the 65-character base64 subset is silently ignored. Characters occurring after a '=' padding character are
never decoded.
If you prefer not to import these routines into your namespace, you can call them as:
use MIME::Base64 ();
$encoded = MIME::Base64::encode($decoded);
$decoded = MIME::Base64::decode($encoded);
Additional functions not exported by default:
encode_base64url( $bytes )
decode_base64url( $str )
Encode and decode according to the base64 scheme for "URL applications" [1]. This is a variant of the base64 encoding which does not
use padding, does not break the string into multiple lines and use the characters "-" and "_" instead of "+" and "/" to avoid using
reserved URL characters.
encoded_base64_length( $bytes )
encoded_base64_length( $bytes, $eol )
Returns the length that the encoded string would have without actually encoding it. This will return the same value as
"length(encode_base64($bytes))", but should be more efficient.
decoded_base64_length( $str )
Returns the length that the decoded string would have without actually decoding it. This will return the same value as
"length(decode_base64($str))", but should be more efficient.
EXAMPLES
If you want to encode a large file, you should encode it in chunks that are a multiple of 57 bytes. This ensures that the base64 lines
line up and that you do not end up with padding in the middle. 57 bytes of data fills one complete base64 line (76 == 57*4/3):
use MIME::Base64 qw(encode_base64);
open(FILE, "/var/log/wtmp") or die "$!";
while (read(FILE, $buf, 60*57)) {
print encode_base64($buf);
}
or if you know you have enough memory
use MIME::Base64 qw(encode_base64);
local($/) = undef; # slurp
print encode_base64(<STDIN>);
The same approach as a command line:
perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' <file
Decoding does not need slurp mode if every line contains a multiple of four base64 chars:
perl -MMIME::Base64 -ne 'print decode_base64($_)' <file
Perl v5.8 and better allow extended Unicode characters in strings. Such strings cannot be encoded directly, as the base64 encoding is only
defined for single-byte characters. The solution is to use the Encode module to select the byte encoding you want. For example:
use MIME::Base64 qw(encode_base64);
use Encode qw(encode);
$encoded = encode_base64(encode("UTF-8", "x{FFFF}
"));
print $encoded;
COPYRIGHT
Copyright 1995-1999, 2001-2004, 2010 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Distantly based on LWP::Base64 written by Martijn Koster <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and code posted
to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans Mulder <hansm@wsinti07.win.tue.nl>
The XS implementation uses code from metamail. Copyright 1991 Bell Communications Research, Inc. (Bellcore)
SEE ALSO
MIME::QuotedPrint
[1] <http://en.wikipedia.org/wiki/Base64#URL_applications>
perl v5.16.3 2013-03-04 MIME::Base64(3pm)