Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use strict;
use warnings;
use utf8;
use Test::More;
use Test::TCP;
use Email::MIME;
use Mail::Address;
use Encode;
use IO::Socket::INET;
use Net::Server::Mail::SMTP;
use Email::Sender::Simple;
BEGIN {
$ENV{EMAIL_SENDER_TRANSPORT} = 'SMTP';
$ENV{EMAIL_SENDER_TRANSPORT_host} = '127.0.0.1';
};
my $server = Test::TCP->new(
code => sub {
my $port = shift;
my $sock = IO::Socket::INET->new(
LocalAddr => '127.0.0.1',
LocalPort => $port,
Proto => 'tcp',
Listen => 1,
) or die "Cannot open server socket: $!";
$sock->accept;
while (my $remote = $sock->accept) {
eval {
my $smtp = Net::Server::Mail::SMTP->new( socket => $remote );
$smtp->set_callback(
'RCPT' => sub {
my $session = shift;
my $rcpt = shift;
my ($email) = Mail::Address->parse($rcpt);
my $domain = $email->host;
return (0, 513, 'Syntax error.') unless $domain;
return 1;
},
);
$smtp->set_callback(
'DATA' => sub {
my $session = shift;
my $data = shift;
my $email = Email::MIME->new($data);
my $from = $email->header('from');
my $body = $email->body;
is $from, 'Foobar<foo@example.com>';
like decode_utf8($body), qr(ボディボディ);
return (1, 250, 'message queued');
},
);
$smtp->process;
};
if ($@) {
diag $@;
$remote->close;
}
}
},
);
my $attributes = +{
content_type => 'text/plain',
charset => 'UTF-8',
encoding => 'quoted-printable',
};
my $header_str = [
To => 'foo@localhost.localdomain',
From => 'Foobar<foo@example.com>',
Subject => '件名です',
];
my $body = 'ボディボディ';
my $email = Email::MIME->create(
attributes => $attributes,
header_str => $header_str,
body_str => $body,
);
$ENV{EMAIL_SENDER_TRANSPORT_port} = $server->port;
my $res = Email::Sender::Simple->send($email);
isa_ok $res, 'Email::Sender::Success';
undef $server;
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment