Skip to content

Commit

Permalink
Fix encode_mimewords call
Browse files Browse the repository at this point in the history
If charset is not specified when calling `encode_mimewords`, it uses
`ISO-8859-1` as default value, which may not be the correct encoding for
the strings.

This commit extracts the charset from `Content-Type` header and uses it
in `encode_mimewords` call.
  • Loading branch information
ldidry committed Sep 14, 2020
1 parent 8ee7dcc commit c4e1b17
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/Email/Mailer.pm
Expand Up @@ -59,16 +59,19 @@ sub send {
->format( HTML::TreeBuilder->new->parse( $mail->{html} ) );
}

$mail->{'Content-Transfer-Encoding'} //= 'quoted-printable';
$mail->{'Content-Type'} ||= 'text/plain; charset=us-ascii';

my $charset;
($charset = $mail->{'Content-Type'}) =~ s/.*charset=([^;]+);?.*/$1/
if ( index($mail->{'Content-Type'}, 'charset') != -1 );
my @keys = keys %$mail;
for my $name ( qw( to from subject ) ) {
my ($key) = grep { lc($_) eq $name } @keys;
$mail->{$key} = encode_mimewords( $mail->{$key} )
if ( $key and defined $mail->{$key} and $mail->{$key} =~ /[^[:ascii:]]/ );
$mail->{$key} = encode_mimewords( $mail->{$key}, Charset => $charset )
if ( $key and defined $mail->{$key} and $mail->{$key} =~ /[^[:ascii:]]/ and $charset);
}

$mail->{'Content-Transfer-Encoding'} //= 'quoted-printable';
$mail->{'Content-Type'} ||= 'text/plain; charset=us-ascii';

# create a headers hashref (delete things from a data copy that known to not be headers)
my $headers = [
map {
Expand Down

0 comments on commit c4e1b17

Please sign in to comment.