Net::S...r::Mail(3pm)

πŸ“¦
version 0.28 Β· 2 versions
  • 0.28 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.28 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Bestie, welcome to Net::S...r::Mail(3pm) β€” a class to easily create a mail server πŸ’Œ. It's from the User Contributed Perl Documentation, and it's about to be your new hyperfixation, no cap.


✨ SYNOPSIS ✨

use Net::Server::Mail::SMTP;

my @local_domains = qw(example.com example.org);
my $server = IO::Socket::INET->new( Listen => 1, LocalPort => 25 );

my $conn;
while($conn = $server->accept)
{
    my $smtp = Net::Server::Mail::SMTP->new( socket => $conn );
    $smtp->set_callback(RCPT => \&validate_recipient);
    $smtp->set_callback(DATA => \&queue_message);
    $smtp->process();
    $conn->close();
}

sub validate_recipient
{
    my($session, $recipient) = @_;

    my $domain;
    if($recipient =~ /\@(.*)>\s*$/)
    {
        $domain = $1;
    }

    if(not defined $domain)
    {
        return(0, 513, 'Syntax error.');
    }
    elsif(not(grep $domain eq $_, @local_domains))
    {
        return(0, 554, "$recipient: Recipient address rejected: Relay access denied");
    }

    return(1);
}

sub queue_message
{
    my($session, $data) = @_;

    my $sender = $session->get_sender();
    my @recipients = $session->get_recipients();

    return(0, 554, 'Error: no valid recipients')
        unless(@recipients);

    my $msgid = add_queue($sender, \@recipients, $data)
      or return(0);

    return(1, 250, "message queued $msgid");
}

🌸 DESCRIPTION 🌸

Okay so, this module is a versatile and extensible implementation of the SMTP protocol and all its glow-ups β€” like ESMTP and LMTP πŸ’…. The event driven, object-oriented API makes it so easy to fold the SMTP protocol into your own programs. It's giving main character energy, fr fr.

The tea on the other SMTPd implementations? They don't support the useful ESMTP extensions, and they don't do LMTP either 😭. Their interface design literally precludes adding them later. So the author decided to rewrite a complete implementation with extensibility in mind. Iconic behaviour, honestly πŸ‘‘

It provides a mechanism to easily add future β€” or not-yet-implemented β€” ESMTP extensions ✨. Developers can hook code at each SMTP session state and change the module's behaviors by registering event call-backs. And the class is designed to be easily inherited from. So you can build your little server family tree 🫢.

This class is the base class for mail service protocols such as Net::Server::Mail::SMTP, Net::Server::Mail::ESMTP and Net::Server::Mail::LMTP. Go read the documentation that ships with each of those modules, bestie πŸ“š.


πŸŽ€ METHODS πŸŽ€

new πŸ¦‹

$instance = Net::Server::Mail->new( [option => 'value', ...] )

Here are the options, babes:

  • handle_in β€” sets the input handle, which is where the server reads data from. Defaults to STDIN.
  • handle_out β€” sets the output handle, which is where the server writes data to. Defaults to STDOUT.
  • socket β€” sets a socket to be used for server reads and writes instead of handles.
  • error_sleep_time β€” the number of seconds to wait for before printing an error message. This dodges some DoS attacks that try to flood the server with bogus commands 🚫🌊. A value of 0 turns this feature off. Defaults to 0.
  • idle_timeout β€” the number of seconds a connection has to remain idle before it gets closed. A value of 0 turns this feature off. Defaults to 0.

dojob ⏳

Some commands need to do a job after the handler call. The handler may want to override this to stop the job from being executed.

By calling this method with a (defined) false value as an argument, the expected job isn't executed. Defaults to true.

set_callback πŸ“ž

($success, $code, $msg) = $obj->set_callback(VERB, \&function, $context)>

Sets the callback code to be called on a particular event. The function should return 1 to 3 values: (success, [return_code, ["message"]]).

$mailserver->set_callback
(
    'RCPT', sub
    {
        my($address) = @_;
        if(is_relayed($address))
        {
            # default success code/message will be used
            return 1;
        }
        else
        {
            return(0, 513, 'Relaying denied.');
        }
    }
);

process πŸš€

$mailserver->process;

Start a new session. That's it, that's the whole vibe.

Send the introduction banner. You have to call it manually when you're using the process_once() method. Don't use it with the process() method. Just don't πŸ™ˆ.


🌈 EVENTS 🌈

Append at the opening of a new connection.

Handler takes no argument.

timeout ⏰

This event append where timeout is exceeded.

Handler takes no argument.

timeout ⏰ (yes, bestie, the original really does list timeout twice β€” iykyk πŸ’€)

This event append where connection is closed or an error occurs during reading from socket.

Takes the error description as an argument if an error occurred, and the argument is undefined if the session was closed by peer.

$mailserver->set_callback
(
    'stop_session', sub
    {
        my($session, $err) = @_;
        if( defined $err )
        {
            print "Error occurred during processing: $err\n";
        }
        else
        {
            print "Session closed py peer\n";
        }
        return 1;
    }
);

πŸ”₯ SEE ALSO πŸ”₯

Please, see Net::Server::Mail::SMTP, Net::Server::Mail::ESMTP and Net::Server::Mail::LMTP. Go read them, they're the whole squad 🫢.


πŸ‘‘ AUTHOR πŸ‘‘

Olivier Poitrey <rs@rhapsodyk.net> β€” legend behaviour.


⭐ AVAILABILITY ⭐

Available on CPAN.

anonymous Git repository:

git clone git://github.com/rs/net-server-mail.git

Git repository on the web:

https://github.com/rs/net-server-mail

πŸ’€ BUGS πŸ’€

Please use the CPAN system to report a bug (http://rt.cpan.org/). Be nice about it, the maintainers are people too πŸ₯Ί.


πŸ“œ LICENCE πŸ“œ

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. (So real talk, no promises, bestie 😭)

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.


Copyright (C) 2002 - Olivier Poitrey
Copyright (C) 2007-2013 - Xavier Guimard

STARTTLS πŸ”

Copyright (C) 2009 - Dan Moore <dan at moore.cx>
Copyright (C) 2013 - Mytram <rmytram@gmail.com>
Copyright (C) 2013 - Xavier Guimard <x.guimard@free.fr>

Contributors 🫢

2012 - Georg Hoesch (patch to reduce memory consumption)

And that's the whole page, bestie β€” this one's perl v5.34.0, dated 2022-05-26, and it's Net::S...r::Mail(3pm) from the User Contributed Perl Documentation. Go build something iconic with it πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from