I'm attempting to program a full mail server (SMTP, POP3, Webmail, etc) and in doing so I wrote a test script to learn how to send e-mails out using only Perl.
Here's my code for the SMTP server:
The output is:
However, it doesn't seem to have worked. I think the problem is that my Net::SMTP code is incorrect. Can anyone point out the problem?
What I want it to do is try the mail servers in the order that the server prefers (greatest to lowest preference) and to quit looping through the servers when the e-mail was successfully delivered.
Here's my code for the SMTP server:
Code:
######################################
## Kirsle Mail Server | SMTP Server ##
######################################
use Net::DNS;
use Net::SMTP;
use Data::Dumper;
print "SMTP\n";
# test directsend
my %mail = (
# I obfuscated the e-mails here
from => 'xxxxx -@- yahoo.com',
to => 'yyyyy -@- gmail.com',
subject => 'hello world',
body => 'this is a test',
);
# get gmail's MX servers
my @mx = mx("gmail.com"); # I already know the recipient uses gmail.com
# sort by priority
my @priority = sort { $b->{preference} <=> $a->{preference} } @mx;
print Dumper(@priority);
# try each server
foreach my $server (@priority) {
my $smtp = Net::SMTP->new (
Host => $server->{exchange},
Hello => 'mail.rainbowboi.com',
Timeout => 60
);
print $smtp->domain . "\n";
my $to = $smtp->to ($mail{to});
my $mail = $smtp->mail ($mail{from});
my $data = $smtp->data("From: $mail{from}\n"
. "To: $mail{to}\n"
. "Subject: $mail{subject}\n\n"
. "$mail{body}\n");
my $quit = $smtp->quit();
print "to=$to; mail=$mail; data=$data; quit=$quit\n";
}
1;
The output is:
Code:
F:\Kirsle\MailServer>MailServer.pl
SMTP
$VAR1 = bless( {
'preference' => 50,
'rdlength' => 13,
'ttl' => 3504,
'name' => 'gmail.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => ' gsmtp163?9',
'exchange' => 'gsmtp163.google.com'
}, 'Net::DNS::RR::MX' );
$VAR2 = bless( {
'preference' => 50,
'rdlength' => 13,
'ttl' => 3504,
'name' => 'gmail.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => ' gsmtp183?9',
'exchange' => 'gsmtp183.google.com'
}, 'Net::DNS::RR::MX' );
$VAR3 = bless( {
'preference' => 10,
'rdlength' => 9,
'ttl' => 3504,
'name' => 'gmail.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => '
?alt1?)',
'exchange' => 'alt1.gmail-smtp-in.l.google.com'
}, 'Net::DNS::RR::MX' );
$VAR4 = bless( {
'preference' => 10,
'rdlength' => 9,
'ttl' => 3504,
'name' => 'gmail.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => '
?alt2?)',
'exchange' => 'alt2.gmail-smtp-in.l.google.com'
}, 'Net::DNS::RR::MX' );
$VAR5 = bless( {
'preference' => 5,
'rdlength' => 27,
'ttl' => 3504,
'name' => 'gmail.com',
'class' => 'IN',
'type' => 'MX',
gmail-smtp-in?l?google??',> ' ?
'exchange' => 'gmail-smtp-in.l.google.com'
}, 'Net::DNS::RR::MX' );
mx.gmail.com
to=0; mail=1; data=; quit=1
mx.gmail.com
to=0; mail=1; data=; quit=1
mx.gmail.com
to=0; mail=1; data=; quit=1
mx.gmail.com
to=0; mail=1; data=; quit=1
mx.gmail.com
to=0; mail=1; data=; quit=1
However, it doesn't seem to have worked. I think the problem is that my Net::SMTP code is incorrect. Can anyone point out the problem?
What I want it to do is try the mail servers in the order that the server prefers (greatest to lowest preference) and to quit looping through the servers when the e-mail was successfully delivered.