1

I'm trying to send an mail with Email::Sender which contains umlauts.

#!/usr/bin/perl

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP::TLS;
use Email::Simple ();
use Email::Simple::Creator ();

open(my $mailbody, "<", "mail-content");

my $smtpserver = 'smtp.gmail.com';
my $smtpport = 587;
my $smtpuser   = 'example@mail.de';
my $smtppassword = 'password';

my $transport = Email::Sender::Transport::SMTP::TLS->new({
  host => $smtpserver,
  port => $smtpport,
  username => $smtpuser,
  password => $smtppassword,
});

my $email = Email::Simple->create(
  header => [
    To      => 'example@mail.de',
    From    => 'example@mail.de',
    Subject => 'Mail',
  ],
  body => <$mailbody>,
);

sendmail($email, { transport => $transport });

The received mail looks like gegrÃŒÃt instead of gegrüßt.
Is there any way i can specify how the mail is getting encoded in perl?

ProfGhost
  • 359
  • 2
  • 20

3 Answers3

3

You have

open(my $mailbody, "<", "mail-content");

If the file mail-content contains UTF-8 encoded data, you must open it with the appropriate IO layer:

open my $mailbody, '<:encoding(UTF-8)', 'mail-content'
    or croak ...;

If, in addition, the source code of your script contains UTF-8 strings that will be incorporated in the message, you must have use utf8; in your script.

In addition, if any header fields include UTF-8 encoded strings, you must encode those strings as well.

Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Well, create a simple `'mail-content'` file, and paste the hexdump in your question then. Is the file really UTF-8 encoded? – Sinan Ünür Jun 10 '15 at 11:34
2

Yes. Add a Content-Type line to your message:

Content-Type: text/html; charset=UTF-8

You may also need to specify your input encoding when reading the mail file:

open ( my $input, "<:encoding(UTF-8)", "Your_File" ) or die $!; 
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • Adding both lines changes nothing at all. I just noticed that the mail is shown correctly at outlook.com but not in the preview on my iPhone – ProfGhost Jun 09 '15 at 15:04
  • Maybe Microsoft and iPhone don't go well together. ;) – simbabque Jun 09 '15 at 15:06
  • @ProfGhost on a more serious note: did you add the `Content-Type` header correctly in the params of the `E::S->create` or did you put it in front of the mail body? The latter would not work. – simbabque Jun 09 '15 at 15:08
  • @simbabque put it in the mail body, i will try it in the create. – ProfGhost Jun 09 '15 at 15:58
  • Add FULL set of MIME headers. `MIME-Version: 1.0` `Content-Type: text/plain; charset=utf-8` `Content-Transfer-Encoding: 8bit` – AnFi Jun 09 '15 at 16:47
0

Just had the same problem and could solve it by adding a

require Net::SMTP;

before using Email::Simple. This seems to be related to a 'workaround' in Email::Sender::Transport::SMTP where it says

utf8::downgrade($next_hunk) if (Net::SMTP->VERSION || 0) < 3.07;

If Net::SMTP is not loaded this resolves to if (0 < 3.07) and causes the downgrade.

smonff
  • 3,399
  • 3
  • 36
  • 46
DeDry
  • 41
  • 2