Skip to content

Instantly share code, notes, and snippets.

@iruca3
Created July 7, 2014 14:19
Show Gist options
  • Save iruca3/8348849d802702d8966d to your computer and use it in GitHub Desktop.
Save iruca3/8348849d802702d8966d to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use MIME::Base64;
use MIME::QuotedPrint::Perl;
my $mail_file = $ARGV[0];
open( FILE, $mail_file) or die "$!";
my $mail_content = "";
while ( <FILE> ) {
$mail_content .= $_;
}
close( FILE );
# Content-Transfer-Encoding が含まれる場合はBoundary処理します。
if ( $mail_content =~ /Content-Transfer-Encoding/ ) {
if ( $mail_content =~ /^(--[a-zA-Z0-9\-]+)[\s]*?$/m ) {
my @contents = split( $1, $mail_content );
foreach my $content( @contents ) {
$encoding = "";
$transfered = "";
if ( $content =~ /^Content-Type: .*?charset=(.*?)[\s]?$/m ) {
$encoding = $1;
}
if ( $content =~ /^Content-Transfer-Encoding: (.*?)[\s]?$/m ) {
$transfered = $1;
}
# Content-Transfer-Encoding がないものは変換しようがないので無視します。
if ( $encoding eq "" ) { $encoding = "UTF-8"; }
if ( $transfered eq "" ) {
next;
}
# ダブルクオーテーションを除去します。
$encoding =~ s/"//g;
$transfered =~ s/"//g;
# 判定のために小文字に変換します。
$encoding = lc( $encoding );
$transfered = lc( $transfered );
print $encoding . " / " . $transfered . "\n";
$content =~ s/^[\r\n]*//;
@splitted_content = split( /\r\n\r\n|\n\n|\r\r/, $content );
if ( $transfered eq "base64" ) {
$decoded_content = decode_base64( $splitted_content[1] );
} elsif ( $transfered eq "quoted-printable" ) {
$decoded_content = decode_qp( $splitted_content[1] );
} elsif ( $transfered eq "7bit" ) {
$decoded_content = $splitted_content[1];
}
print $decoded_content;
}
}
}
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment