Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with this mail code

Status
Not open for further replies.

Kirsle

Programmer
Joined
Jan 21, 2006
Messages
1,179
Location
US
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:
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.
 
Nevermind, I resolved the problem!

I turned on debugging on Net::SMTP and found that I was sending RCPT TO and MAIL FROM in the wrong order. So I switched the two commands in my code:

Code:
	my $mail = $smtp->mail ($mail{from});
	my $to = $smtp->to ($mail{to});
	my $data = $smtp->data("From: $mail{from}\r\n"
		. "To: $mail{to}\r\n"
		. "Subject: $mail{subject}\r\n\r\n"
		. "$mail{body}\r\n");

(and also changed all the \n's to \r\n if that helped the matter out at all). GMail put the message into my spam folder though, and so I wasn't getting it through Thunderbird.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top