Peter Staab
Peter Staab

Reputation: 552

error when sending email using Dancer2::Plugin::Email;

I am sending email using Dancer2 via the Dancer2::Plugin::Email package. The main code that I have for this is:

sub sendEmail {
  my ($params,$email_address,$template) = @_;

  my $text = '';

  my $tt = Template->new({
    INCLUDE_PATH => config->{views},
    INTERPOLATE  => 1,
    OUTPUT => \$text
  }) || die "$Template::ERROR\n";

  my $out = $tt->process($template,$params);

  my $email = email {
    from    => XXXXX,
    to      => $email_address,
    subject => XXXXX,
    body    => $text,
    'Content-Type' => 'text/html'
  };
}

where I have hidden a couple of the fields. I have gotten the following error:

Route exception: open body: Invalid argument at 
/usr/local/share/perl/5.22.1/MIME/Entity.pm line 1878. in 
/usr/local/share/perl/5.22.1/Dancer2/Core/App.pm l. 1454

It is not occurring all of the time and I haven't been able to find a consistent piece of code that always fails.

I have set the host parameter of the mail server that I am using in the configuration as explained here: https://metacpan.org/pod/Dancer2::Plugin::Email Simple tests show it works, but I get sporadic errors that I can't track down.

Upvotes: 1

Views: 154

Answers (1)

Nathan
Nathan

Reputation: 143

Almost certainly unencoded Unicode in your email body, see https://rt.cpan.org/Public/Bug/Display.html?id=105377:

MIME::Entity requires encoded data if you supply the Data => 'xxx' key. Rather than supplying native Unicode strings, you should encode them first. Sample below illustrates what I mean:

 use Encode;
 use MIME::Entity;
 
 my $data = "\x{1f4a9}";
 my $e = MIME::Entity->build(Data => Encode::encode('UTF-8', $data, Encode::FB_CROAK));
 $e->print();

Upvotes: 0

Related Questions