Posted: Wed Sep 13, 2006 6:07 pm Post subject: KirsleSMTP
This is a simple SMTP server program written entirely in Perl. The reason it was named KirsleSMTP is because I'm working on a bigger project, "Kirsle Mail Server", which acts as a full mail server (SMTP, POP3, IMAP, etc) and the code for this little program was taken from the bigger project.
This program runs a server that acts as an outgoing mail server. This is useful because there aren't many free SMTP servers out there that are any good. The one I used to use, "Free SMTP Server", turns out to only send 10 e-mails per day with the free version. So, this is a simple SMTP server that can send outgoing e-mails with no limitations at all.
This module uses Net::DNS to look up the mail servers for all the recipients of an e-mail, so it uses a "DirectSend" method.
Anyway, here's the source code:
Code:
#########################################################
## Kirsle SMTP Server | Free Perl SMTP Server Software ##
#########################################################
use strict;
use warnings;
use Net::DNS;
use Net::SMTP;
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
#####################################
## Configure the SMTP Server Here! ##
#####################################
our $cfg = {
# Specify the host and port number to run the server on.
# ex: localhost, 25
host => 'localhost',
port => 25,
# Your server identification line (HELO in the SMTP transaction)
helo => 'mail.kirsle.com',
};
####################################
## End Configuration Section ##
####################################
# On Windows, show a system tray icon.
if ($^O =~ /win32/i) {
eval {
use Win32::GUI;
my $win32 = new Win32::GUI::DialogBox (
-height => 1,
-width => 1,
-name => 'Win32GUI',
);
my $icon = new Win32::GUI::Icon ('kirsle.ico');
our $server = new Net::SMTP::Server ($cfg->{host}, $cfg->{port})
or die "Can't spawn SMTP Server: $!";
print "Server listening at $cfg->{host}:$cfg->{port}\n\n";
# Main Server Loop
while (my $conn = $server->accept) {
my $client = new Net::SMTP::Server::Client ($conn);
$client->process || next;
# Get all the recipients.
foreach my $rcpt (@{$client->{TO}}) {
# Get their MX servers.
my ($domain) = $rcpt =~ m/.*?\@(.*?)$/i;
$domain =~ s/(<|>)//g;
my @mx = mx($domain);
# Sort the servers.
my @priority = sort { $b->{preference} <=> $a->{preference} } @mx;
# Try each one.
foreach my $server (@priority) {
my $smtp = Net::SMTP->new (
Host => $server->{exchange},
Hello => $cfg->{helo},
Timeout => 60,
Debug => 1,
);
my $mail = $smtp->mail ($client->{FROM});
my $to = $smtp->to ($rcpt);
my $data = $smtp->data ($client->{MSG});
my $quit = $smtp->quit();
if ($mail && $to && $data && $quit) {
print "Mail sent successfully!\n";
last;
}
}
}
}
Here's the README file:
Code:
+-----------------------------+
| Kirsle SMTP Server | ReadMe |
+-----------------------------+
- Open "KirsleSMTP.pl" in a text editor. Edit the parameters
of the hashref $cfg, between the two blocks saying
"Configure the SMTP Server Here!" and "End Configuration Section"
host - The host name to run the SMTP server on. 'localhost' works
for most cases.
port - The port to run your SMTP server on. The standard SMTP port
is 25, so in most cases you don't need to change this.
helo - This should be the host name of the SMTP server. This doesn't
have to match the "host" parameter. An example would be
"smtp.your-domain-name.com"
#########################
# 2. Running It ##
#########################
- After configuring KirsleSMTP.pl, run it to start the server.
- On Windows, it will create a system tray icon with the "CjK" symbol
on it. This is for visual notification that the server program is
running. Clicking the icon has no effect.
- Note: on Windows, the server is most efficient when run using
wperl.exe provided with ActiveState Perl. See below for how to set
up a shortcut to the server.
#########################
# 3. Using It #
#########################
- In your program codes that you send e-mail with, specify that
the SMTP server should point to the local one. Here's an example
of Perl with Mail::Sendmail:
use Mail::Sendmail;
my %mail = (
Smtp => 'localhost:25', # using default config for your SMTP server
From => 'name@domain.com',
To => 'you@domain.com',
Message => 'this is only a test',
);
sendmail (%mail);
- In your e-mail client (Thunderbird, Outlook Express, etc.), configure
your account's SMTP section to use localhost:25 as the SMTP server. If
you need help in doing this, consult your e-mail client's documentation.
This program is free software, released under the same terms as Perl itself.
You may modify and redistribute the code, as long as you cite the changes and
specify where you obtained the original copy of the code.
1. Move all the files to an easy-to-remember location (i.e. "C:\SMTP").
2. Open the folder where you moved the files, click "File", point to
"New", and click "Shortcut"
3. In the Location: box, type in the following (with the quotes):
wperl "C:\SMTP\KirsleSMTP.pl"
Note that if you extracted this to somewhere besides C:\SMTP, you
need to adjust the path appropriately.
4. Hit "Next" and give your shortcut a name. Name it "SMTP Server" or
something you will remember.
5. Right-click on the newly created shortcut icon and choose "Properties"
6. Edit the "Start in:" field to say "C:\SMTP"
Now you have a shortcut that you can simply double-click to run your SMTP
server with. To set your server to automatically run on startup, follow
these steps:
1. In the SMTP Server folder, select the shortcut and hit Ctrl+C (copy)
2. Right-click the "Start" button and choose "Open"
3. Double-click the "Programs" folder.
4. Double-click the "Startup" folder.
5. Push Ctrl+V (paste) to paste the shortcut into the Startup folder.
And now when you log on to Windows, the SMTP server will start up automatically.
And, the distribution is attached. It includes a local lib with the SMTP server modules and Net::DNS. If you'd rather install or use your own versions of these modules, delete the "Net" folder from the program.