Gossamer Forum
Home : General : Perl Programming :

Perl socket problems

Quote Reply
Perl socket problems
Greets -
I'm trying to use socket commands to send email and I'm having some problems I thought someone might be able to help with.

When I send the below code, I get a blank email with nothing in the "To" field, no subject and no message. Any ideas?

################################
use Socket;

socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')); #set up the socket
connect(SOCK, sockaddr_in(25, inet_aton($server))); #connect to my server on port 25

select(SOCK); $| = 1; select(STDOUT); # use unbuffemiles i/o.


{
my($inpBuf) = '';


recv(SMTP, $inpBuf, 200, 0);
recv(SMTP, $inpBuf, 200, 0);
}


sendSMTP(0, "HELO $server\n");
sendSMTP(0, "MAIL From: <$config{'admin_address'}>\n");
sendSMTP(0, "RCPT To: <$to>\n");
sendSMTP(0, "DATA\n");

send(SOCK, "From: $from\r\n.\r\n", 0);
#send(SOCK, "Reply-To: $from\r\n.\r\n", 0);
send(SOCK, "Subject: $subject\n", 0);
send(SOCK, $message, 0);

#sendSMTP(0, "From: <$from>\r\n.\r\n");
#sendSMTP(0, "Reply-To: <$from>\r\n.\r\n");
#sendSMTP(0, "Subject: <$subject>\r\n.\r\n");
#sendSMTP(0, $message, 0);


sendSMTP(0, "\r\n.\r\n");
sendSMTP(0, "QUIT\n");
####################################

close(SOCK);

##################
# SendSMTP code
sub sendSMTP {
my($debug) = shift;
my($buffer) = @_;


print STDERR ("> $buffer") if $debug;
send(SOCK, $buffer, 0);


recv(SOCK, $buffer, 200, 0);
print STDERR ("< $buffer") if $debug;


return( (split(/ /, $buffer))[0] );
}
###################################

Again, the email gets sent but the 3 fields mentioned above are blank. Any help would be appreciated.

I'm running this on an NT box if that helps.

EDIT: Added a few other lines I previously left out just in case they're important.

Last edited by:

Xanadu: Jan 30, 2003, 1:19 PM
Quote Reply
Re: [Xanadu] Perl socket problems In reply to
Why re-invent the wheel?

Just use the Mail::Sender module;
Code:
#!/usr/bin/perl
use warnings;
use strict;
use Mail::Sender;

my $sender = Mail::Sener->new({
smtp => 'smtp.mydomain.com',
from => 'me@mydomain.com',
})
or die "Can't create sender!";

$sender->MailMsg({
to => 'you@yourdomain.com',
subject => 'This is the message subject',
msg => 'This is the message body',
})
or die "Can't send mail!";

(Untested)

If you really must use sockets (why?!) then at least ue IO::Socket::INET - it make it a lot simpler!

Cheers,
Dave.