Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use message codepage if body code page is not present #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions lib/Email/Outlook/Message.pm
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ our $MAP_SUBITEM_FILE = {
'1042' => "INREPLYTO", # In reply to Message-Id
'3007' => 'DATE2ND', # Creation Time
'0039' => 'DATE1ST', # Outlook sent date
'3FDE' => 'CODEPAGE', # Code page for text or html body
'3FDE' => 'CODEPAGE', # PR_INTERNET_CPID - Code page for text or html body
'3FFD' => 'MESSAGE_CODEPAGE' # PR_MESSAGE_CODEPAGE - Message Code Page
};

# Map codepage numbers to charset names. Codepages not listed here just get
Expand Down Expand Up @@ -402,14 +403,27 @@ sub _body_html_character_set {

sub _body_character_set {
my $self = shift;
my $body_encoding = shift;
my $codepage = $self->{CODEPAGE};
if (defined $body_encoding && $body_encoding eq "001F") {
my $body_encoding = shift || "";
my $codepage = $self->{CODEPAGE} || $self->{MESSAGE_CODEPAGE};
my $codepage_value;

if (defined $codepage) {
$codepage_value = $MAP_CODEPAGE->{$codepage} || "CP$codepage";
} else {
$codepage_value = "CP1252";
}

$self->{VERBOSE} and
warn "Body-encoding: $body_encoding; Codepage: $codepage_value";

if ($body_encoding eq "001F") {
if (defined $codepage and $codepage_value ne "UTF-8") {
$self->{VERBOSE} and
warn "Unicode encoding used to encode $codepage_value character set";
}
return "UTF-8";
} elsif (defined $codepage) {
return $MAP_CODEPAGE->{$codepage} || "CP$codepage";
} else {
return 'CP1252';
return $codepage_value;
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/Email/Outlook/Message/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ our $skipproperties = {
'3FF9' => "Creator EntryId",
'3FFA' => "Last Modifier Name",
'3FFB' => "Last Modifier EntryId",
'3FFD' => "Message Code Page",
# 'Transport-defined envelope property'
'4019' => "Sender Flags",
'401A' => "Sent Representing Flags",
Expand Down
34 changes: 33 additions & 1 deletion t/internals.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strict;
use warnings;
use Test::More tests => 17;
use Test::More tests => 25;
use Email::Outlook::Message;
#use MIME::Entity;
use Email::MIME::Creator;
Expand All @@ -13,9 +13,15 @@ test_to_email_mime_with_no_parts($p);
test_to_email_mime_with_plain_part($p);
test_to_email_mime_with_html_part($p);
test_to_email_mime_with_two_parts($p);
test_to_email_mime_with_plain_part_and_body_codepage();
test_to_email_mime_with_plain_part_and_message_codepage();

# DONE

sub empty_message {
Email::Outlook::Message->_empty_new();
}

sub test_copy_header_data {
my $p = shift;

Expand Down Expand Up @@ -67,6 +73,32 @@ sub test_to_email_mime_with_plain_part {
like($m->content_type, qr{^text/plain; charset="?CP1252"?$});
}

sub test_to_email_mime_with_plain_part_and_body_codepage {
my $p = empty_message();
$p->{BODY_PLAIN} = "plain";
$p->{BODY_PLAIN_ENCODING} = "001E";
$p->{CODEPAGE} = 1251;
$p->{BODY_HTML} = undef;
my $m = $p->to_email_mime;
ok(defined $m);
ok(($m->parts) == 1);
is($m->body, "plain");
like($m->content_type, qr{^text/plain; charset="?CP1251"?$});
}

sub test_to_email_mime_with_plain_part_and_message_codepage {
my $p = empty_message();
$p->{BODY_PLAIN} = "plain";
$p->{BODY_PLAIN_ENCODING} = "001E";
$p->{MESSAGE_CODEPAGE} = 1251;
$p->{BODY_HTML} = undef;
my $m = $p->to_email_mime;
ok(defined $m);
ok(($m->parts) == 1);
is($m->body, "plain");
like($m->content_type, qr{^text/plain; charset="?CP1251"?$});
}

sub test_to_email_mime_with_html_part {
my $p = shift;
$p->{BODY_PLAIN} = undef;
Expand Down