CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Win32 Programming Help:
Problem with sending email

 



LeoTron
New User

Dec 10, 2006, 8:17 PM

Post #1 of 3 (3393 views)
Problem with sending email Can't Post

Please someone help me I am trying to send and email through a perl script, I have been trying to do this and have not been able to. I have installed various modules to try and do this... with no success. here is the my most recent code.

************ CODE *************************

#!/usr/bin/perl -w

use Socket;
use strict;

my($mailTo) = 'me@somehost.com';
my($mailServer) = 'mail.somhost.com';

my($mailFrom) = 'someone@somehost.com';
my($realName) = "The Master";
my($subject) = 'Test';
my($body) = "Test Line One. \nTest Line Two.\n";

$main::SIG{'INT'} = 'closeSocket';

my($proto) = getprotobyname("tcp") || 6;
my($port) = getservbyname("SMTP", "tcp") || 25;
my($serverAddr) = (gethostbyname($mailServer))[4];

if(! defined($length)){
die('gethostbyname failed.');
}

socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)
or die("socket: $!");

$packFormat = 'S n a4 x8'; # Windows 95, SunOs 4.1+
#$packFormat = 'S n c4 x8'; # SunOs 5.4+ (Solaris 2)

connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr))
or die("connect: $!");

select(SMTP); $| = 1; select(STDOUT); #use unbuffered I/O.
{
my($inpBuf) = '';

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

sendSMTP(1, "HELO\n");
sendSMTP(1, "MAIL From: <$mailFrom>\n");
sendSMTP(1, "RCPT To: <$mailTo>\n");
sendSMTP(1, "DATA\n");

send(SMTP, "From: $realName\n", 0);
send(SMTP, "Subject: $subject\n", 0);
send(SMTP, $body, 0);

sendSMTP(1, "\r\n.\r\n");
sendSMTP(1, "QUIT\n");

close(SMTP);

sub closeSocket { #close smtp socket on error
close(SMTP);
die("SMTP socket closed due to SIGINT\n");
}

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

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

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

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

********* END CODE *************

Now here is the message that I get from the above code

******** ERROR MESSAGE ***********

Global symbol "$length" requires explicit package name at Untitled.pl line 33.
Global symbol "$packFormat" requires explicit package name at Untitled.pl line 40.
Global symbol "$packFormat" requires explicit package name at Untitled.pl line 43.
Execution of Untitled.pl aborted due to compilation errors.
Exit code: 255 , 00FFh

******** END ERROR MESSAGE *********

Please help me with this I have been trying all kinds of things for the last 3 days and at this point I am stuck. any help will be greatly apreciated.

Leo C


KevinR
Veteran


Dec 10, 2006, 10:43 PM

Post #2 of 3 (3392 views)
Re: [LeoTron] Problem with sending email [In reply to] Can't Post

this should take care of those error messages (just need to add "my" in front of the variables although $length was serving no purpose), but doesn't mean the script will work:


Code
#!/usr/bin/perl -w 

use Socket;
use strict;

my($mailTo) = 'me@somehost.com';
my($mailServer) = 'mail.somhost.com';

my($mailFrom) = 'someone@somehost.com';
my($realName) = "The Master";
my($subject) = 'Test';
my($body) = "Test Line One. \nTest Line Two.\n";

$main::SIG{'INT'} = 'closeSocket';

my($proto) = getprotobyname("tcp") || 6;
my($port) = getservbyname("SMTP", "tcp") || 25;
my($serverAddr) = (gethostbyname($mailServer))[4];

if(! defined($serverAddr)){
die('gethostbyname failed.');
}

socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)
or die("socket: $!");

my $packFormat = 'S n a4 x8'; # Windows 95, SunOs 4.1+
#$packFormat = 'S n c4 x8'; # SunOs 5.4+ (Solaris 2)

connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr))
or die("connect: $!");

select(SMTP); $| = 1; select(STDOUT); #use unbuffered I/O.
{
my($inpBuf) = '';

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

sendSMTP(1, "HELO\n");
sendSMTP(1, "MAIL From: <$mailFrom>\n");
sendSMTP(1, "RCPT To: <$mailTo>\n");
sendSMTP(1, "DATA\n");

send(SMTP, "From: $realName\n", 0);
send(SMTP, "Subject: $subject\n", 0);
send(SMTP, $body, 0);

sendSMTP(1, "\r\n.\r\n");
sendSMTP(1, "QUIT\n");

close(SMTP);

sub closeSocket { #close smtp socket on error
close(SMTP);
die("SMTP socket closed due to SIGINT\n");
}

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

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

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

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

-------------------------------------------------


(This post was edited by KevinR on Dec 10, 2006, 10:44 PM)


davorg
Thaumaturge / Moderator

Jan 8, 2007, 7:29 AM

Post #3 of 3 (3339 views)
Re: [LeoTron] Problem with sending email [In reply to] Can't Post

You really don't want to be talking raw SMTP over a socket. That's just asking for trouble.

See the FAQ for the recommended approaches.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives