Subject: | data_validate check |
I need to check the DATA part of the email, this can be implemented as
follows:
in AnyEvent::SMTP::Server::new
DATA => sub {
my ($s,$con) = @_;
$con->{m}{from} or return $con->reply("503 Error:
need MAIL command");
$con->{m}{to} or return $con->reply("554 Error:
need RCPT command");
$con->reply("354 End data with
<CR><LF>.<CR><LF>");
$con->data(cb => sub {
my $data = shift;
if ($self->{data_validate}) {
my ($res,$err,$errstr) = $self->
{data_validate}->($con->{m}, $data);
d $res or return $con->reply("$err
$errstr");
}
$con->{m}{data} = $data;
local $s->{event_failed} = 0;
local $s->{current_con} = $con;
$s->event( mail => delete $con->{m} );
if ($s->{event_failed}) {
$con->reply("500 Internal Server
Error");
} else {
$con->ok("I'll take it");
}
});
},
at the constructor:
my $server = AnyEvent::SMTP::Server->new
(
port => 2525,
mail_validate => sub {
my ($m,$addr) = @_;
if ($good) {
return 1;
} else {
return 0, 513, 'Bad sender.';
}
},
rcpt_validate => sub {
my ($m,$addr) = @_;
if ($good) {
return 1;
} else {
return 0, 513, 'Bad recipient.';
}
},
data_validate => sub {
my ($m,$data) = @_;
my $size = length $data;
if ($size > $max_email_size) {
warn "email too big: $size, from: $m->{from}, to: @{$m->{to}},
host: $m->{host}\n";
return 0, 552, 'REJECTED: message size limit exceeded';
} else {
return 1;
}
}
);