ProfGhost
ProfGhost

Reputation: 359

Send UTF-8 encoded mail with Email::Sender

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   = '[email protected]';
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      => '[email protected]',
    From    => '[email protected]',
    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?

Upvotes: 1

Views: 3072

Answers (3)

DeDry
DeDry

Reputation: 41

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.

Upvotes: 0

Sinan &#220;n&#252;r
Sinan &#220;n&#252;r

Reputation: 118148

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.

Upvotes: 3

Sobrique
Sobrique

Reputation: 53488

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 $!; 

Upvotes: 2

Related Questions