3

I want to send email. My Email::Simple (module version 2.216) object is:

Email::Simple {
  body => \## EXCEPTION MESSAGE
RU Error message: Какой-то текст‚ at /home/kes/work/projects/tucha/monkeyman/lib/MaitreD.pm line 75.
,
  header => Email::Simple::Header {
    headers => [
      To,
      [
        to@domain,
        To: to@domain,
      ],
      From,
      [
        from@domain,
        From: from@domain,
      ],
      Subject,
      [
        Exception,
        Subject: Exception,
      ],
      Content-Type,
      [
        text/plain; charset="utf8",
        Content-Type: text/plain; charset="utf8",
      ],
      Date,
      Wed, 26 Feb 2020 14:30:39 +0200,
    ],
    mycrlf => 
,
  },
  mycrlf => 
,
}




Net::SMTP::_SSL>>> Net::SMTP::_SSL
Net::SMTP::_SSL>>>   IO::Socket::SSL(2.066)
Net::SMTP::_SSL>>>     IO::Socket::IP(0.39)
Net::SMTP::_SSL>>>       IO::Socket(1.38)
Net::SMTP::_SSL>>>         IO::Handle(1.36)
Net::SMTP::_SSL>>>           Exporter(5.72)
Net::SMTP::_SSL>>>   Net::SMTP(3.08_01)
Net::SMTP::_SSL>>>     Net::Cmd(3.08_01)
Net::SMTP::_SSL=GLOB(0x10293e98)>>> MAIL FROM:<from@domain>
Net::SMTP::_SSL=GLOB(0x10293e98)<<< 250 2.1.0 <from@domain> ok
Net::SMTP::_SSL=GLOB(0x10293e98)>>> RCPT TO:<to@domain>
Net::SMTP::_SSL=GLOB(0x10293e98)<<< 250 2.1.5 <to@domain> recipient ok
Net::SMTP::_SSL=GLOB(0x10293e98)>>> DATA
Net::SMTP::_SSL=GLOB(0x10293e98)<<< 354 Enter mail, end with "." on a line by itself
H1
Net::SMTP::_SSL=GLOB(0x10293e98)>>> To: to@domain
Net::SMTP::_SSL=GLOB(0x10293e98)>>> From: from@domain
Net::SMTP::_SSL=GLOB(0x10293e98)>>> Subject: Exception
Net::SMTP::_SSL=GLOB(0x10293e98)>>> Content-Type: text/plain; charset="utf8"
Net::SMTP::_SSL=GLOB(0x10293e98)>>> Date: Wed, 26 Feb 2020 14:11:55 +0200
Net::SMTP::_SSL=GLOB(0x10293e98)>>> 
Net::SMTP::_SSL=GLOB(0x10293e98)>>> ## EXCEPTION MESSAGE
Net::SMTP::_SSL=GLOB(0x10293e98)>>> RU Error message: Какой-то текст at /home/kes/work/projects/tucha/monkeyman/lib/MaitreD.pm line 75.
H2
H3
Net::SMTP::_SSL=GLOB(0x10293e98)>>> .

and here communication is halted. My side is waiting mail server, mail server is waiting something from my side. then server disconnects by timeout

/home/kes/work/projects/tucha/monkeyman/local/lib/perl5/Email/Sender/Transport/SMTP.pm
    300:     # -- rjbs, 2015-08-10
    301:     utf8::downgrade($next_hunk) if (0+Net::SMTP->VERSION || 0) < 3.07;
    302:
   x303:     warn "H1\n";
   x304:     $smtp->datasend($next_hunk) or $FAULT->("error at during DATA");
   x305:     warn "H2\n";
    306:   }
    307:
   x308:   warn "H3\n";
   x309:   DB::x;
  >>310:   $smtp->dataend                     or $FAULT->("error at after DATA");
   x311:   warn "H4\n";

How to correctly send utf8 text via Email::Simple?

My code:

    my $email =  Email::Simple->create(
        header =>  [
            To             =>  $address,
            From           =>  $cnf->{ from },
            Subject        =>  $subject,
            %$headers,
        ],
        body => $body,
    );
    my $err =  Email::Sender::Simple->send( $email, { transport => $transport } );

UPD
The mail body is:

Devel::Peek::Dump( $body )
SV = IV(0xc3ff920) at 0xc3ff930
  REFCNT = 2
  FLAGS = (ROK)
  RV = 0x10d0d530
  SV = PVMG(0x10d36f60) at 0x10d0d530
    REFCNT = 3
    FLAGS = (OBJECT,POK,IsCOW,pPOK,UTF8)
    IV = 0
    NV = 0
    PV = 0x10d69cd0 "## EXCEPTION MESSAGE\nRU Error message: \320\232\320\260\320\272\320\276\320\271-\321\202\320\276 \321\202\320\265\320\272\321\201\321\202 at /home/kes/work/projects/tucha/monkeyman/lib/MaitreD.pm line 75.\n\n"\0 [UTF8 "## EXCEPTION MESSAGE\nRU Error message: \x{41a}\x{430}\x{43a}\x{43e}\x{439}-\x{442}\x{43e} \x{442}\x{435}\x{43a}\x{441}\x{442} at /home/kes/work/projects/tucha/monkeyman/lib/MaitreD.pm line 75.\n\n"]
    CUR = 134
    LEN = 136
    COW_REFCNT = 1
    STASH = 0x35583d0   "Mojo::ByteStream"

When I Encode::decode( 'utf8', $body ) I get error:

Wide character at ...

When I Encode::encode( 'utf8', $body )

Devel::Peek::Dump( $body )
SV = PVIV(0x10c77a18) at 0xc3ff930
  REFCNT = 2
  FLAGS = (POK,IsCOW,pPOK)
  IV = 0
  PV = 0x10df0750 "## EXCEPTION MESSAGE\nRU Error message: \320\232\320\260\320\272\320\276\320\271-\321\202\320\276 \321\202\320\265\320\272\321\201\321\202 at /home/kes/work/projects/tucha/monkeyman/lib/MaitreD.pm line 75.\n\n"\0
  CUR = 134
  LEN = 136
  COW_REFCNT = 0

and send

Then everything is fine:

Net::SMTP::_SSL=GLOB(0x10e280e8)>>> .
Net::SMTP::_SSL=GLOB(0x10e280e8)<<< 250 2.0.0 Ok: queued as D83FF102BA8
H4
smonff
  • 3,399
  • 3
  • 36
  • 46
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • 1
    Maybe you need to [Encode](https://perldoc.pl/Encode) the `$body`? – smonff Feb 26 '20 at 17:39
  • You can also `use Try::Tiny` and `use Email::Sender::Simple qw(sendmail)` and do something like `my $email = Email::Simple->create( ... ); try { sendmail( $email )} catch { warn "Sending failed: $_"};` to retrieve more useful informations or see [in case of error](https://metacpan.org/pod/Email::Sender::Manual::QuickStart#in-case-of-error) part of the Manual. – smonff Feb 26 '20 at 17:48
  • Also, which version of `Email::Sender` are you using? And CPAN installed or from OS package? – smonff Feb 26 '20 at 17:56
  • Have you had a chance to visit the following [webpage](https://metacpan.org/pod/Email::Sender::Manual::QuickStart)? – Polar Bear Feb 26 '20 at 19:50
  • Try `Email::Mime` if you use non ASCII charset (e.g. utf-8). [e.g. you declared only one of three needed mime headers (Content-Type:)] – AnFi Feb 27 '20 at 02:54
  • @AnFi: Which are two remaining? – Eugen Konkov Feb 27 '20 at 07:52
  • 1
    The complete set: `MIME-Version: 1.0`,`Content-Type: text/plain; charset=utf-8`,`Content-Transfer-Encoding: 8bit`. **WARNING:** Some SMTP server will not accept 8bit (raw utf-8) encoding. – AnFi Feb 27 '20 at 09:43

1 Answers1

1

Thank @smonff. The body should be encoded before sending:

$body =  Encode::encode( utf8 => $body )

Now I can send utf8 emails:

my $email =  Email::Simple->create(
    header =>  [
        To             =>  $address,
        From           =>  $cnf->{ from },
        Subject        =>  $subject,
        %$headers,
    ],
    body => $body,
);
my $err =  Email::Sender::Simple->send( $email, { transport => $transport } );

Also I have found these few recommendations how to work with utf8

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • 1
    Aaaah great. Happy it helped! – smonff Feb 27 '20 at 07:47
  • Instead you should use [Email::MIME](https://metacpan.org/pod/Email::MIME) (an Email::Simple subclass designed for this) as mentioned in a comment to handle this correctly, or [Email::Stuffer](https://metacpan.org/pod/Email::Stuffer) which wraps Email::MIME in a simpler interface. – Grinnz Feb 28 '20 at 22:32
  • @Grinnz: thank you. Yeah, `Email::Simple` is not simple, so I switched to `Email::Stuffer` – Eugen Konkov Feb 29 '20 at 18:17