Forum Moderators: coopster & phranque

Message Too Old, No Replies

Sendmail issue in CGI

socket failed (unknown error)

         

udot

9:38 am on Sep 18, 2007 (gmt 0)

10+ Year Member



Hi,

I am using Perl5.6 on Windows XP..and an Apache-Tomcat server to host my web page. Though I am able to send mails using Mail::Sendmail in perl, I am not able to do so using CGI from browser.

Here is the Perl script which sends mail successfully....


use Mail::Sendmail;

%mail = ( To => 'somebody@somewhere',
From => 'blah@blah',
Subject =>'sample subject',
Message => "sample message"
);

sendmail(%mail) or die("mail NOT sent \n\nERROR DETAILS: \n". $Mail::Sendmail::error);

print "Log says:\n", $Mail::Sendmail::log;

But when i am using this in my CGI script and open that cgi page from my browser, it gives an error..

Here is my CGI script which is almost same to previous one.


use CGI;
use Mail::Sendmail;

print "Content-Type: text/html\n\n";

print "<html> <head>\n";
print "<title>testmail</title>";
print "</head>\n";
print "<body>\n";

%mail = ( To => 'somebody@somewhere',
From => 'blah@blah',
Subject =>'sample subject',
Message => "sample message"
);

if(sendmail(%mail)) {print "<p>Success!";} else { print("mail NOT sent \n\nERROR DETAILS: \n". $Mail::Sendmail::error);}

print "<p>Log says:<p>\n". $Mail::Sendmail::log;
print "</body> </html>\n";

unfortunately i have never seen that "Success" :( :(
Here is the **latest** ouput in internet explorer i have seen...


mail NOT sent ERROR DETAILS: socket failed (Unknown error)
Log says:

Mail::Sendmail v. 0.79 - Tue Sep 18 15:00:45 2007 Date: Tue, 18 Sep 2007 15:00:45 +0530

...for mail server i am using Default SMTP virtual server in IIS...looking at the error type socket failed (Unknown error), I am at sea as to what after should I run! Is it Perl(nope ..i don think so!) or if is it socket error? (then what is the actual error? how can I fix an error which is termed as Unknown :( :( )
..
PLZZZZZZZZZ Help me out .. :( :( :(

SeanW

5:42 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Can you telnet to port 25 of the local host?

Sean

udot

4:43 am on Sep 19, 2007 (gmt 0)

10+ Year Member



Yess.. I can telnet {my ip} 25 at command prompt which results in 220 ready and it should as there is no problem in sending mail from Perl script...the problem occuring only from browser using CGI!....still clueless about this problem .. :(

perl_diver

6:51 am on Sep 19, 2007 (gmt 0)

10+ Year Member



any firewalls or proxies interferring?

phranque

7:10 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i copied my response here from your other thread on this subject:

i would try using something like the Socket or IO::Socket perl packages to create a test socket directly in your cgi script instead of buried in the Mail::Sendmail package and see if you can get a better error message.
you might also try another mail package such as Mail::Sender to see if it works better on xp.

udot

9:17 am on Sep 19, 2007 (gmt 0)

10+ Year Member



phranque.. i could not get it.. are u trying to say i should connect the smtp port in the CGI script like adding these lines


$sock = IO::Socket::INET->new('{ip}:{smtp port usually 25}');
connect(Socket_Handle,$sock);

i havnt worked on IO::Socket modules before... so I dont have much idea about...

udot

11:45 am on Sep 19, 2007 (gmt 0)

10+ Year Member



Hi phranque, I used the Mail::Sender package too..but still unsuccessful in sending mail ... here is the code..

use CGI;
use Mail::Sender;

print "Content-Type: text/html\n\n";

print "<html> <head>\n";
print "<title>testmail</title>";
print "</head>\n";
print "<body>\n";

$sender = new Mail::Sender {
smtp => '{ip address}',
from => 'test@test',
on_errors => undef,
}
or print "<p>Can't create the Mail::Sender object: <br>$Mail::Sender::Error\n";
$sender->Open({
to => 'blah@blah',
subject => 'Hi there'
})
or print "<p>Can't open the message: <br>$sender->{'error_msg'}\n";
$sender->SendLineEnc("1st line");
$sender->SendLineEnc("\n2nd line");
$sender->Close()
or print "<p>Failed to send the message: <br> $sender->{'error_msg'}\n";

print "</body> </html>\n";

...and the output is...

Can't open the message:
socket() failed: $^E

Failed to send the message:
Connection not established

..still can't get what the error is! :(

phranque

11:48 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i have never done any socket programming, so i can't help you much from here with that.
i was merely suggesting a possible avenue to discover the cause of problem yourself.

your ultimate solution should be to use sendmail which is a more standard perl way to do it.
sendmail comes standard on *nix-type systems, but i'm sure you can find some "sendmail for windows" solutions if you search carefully.

phranque

11:53 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i hadn't noticed before so please allow me to welcome you to WebmasterWorld, udot!

have you tried running your CGI script from the command line or was it another perl script where Mail::Sender worked correctly?

udot

12:46 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



No ..I opened the CGI cript from browser, it failed to send mail (that socket error blah blah..)..but if i use it as Perl script, it works perfect! ..anyway phranque ..thanx for taking ur time out from ur schedule to reply to my queries...but still it remained unsolved..

rocknbil

5:33 pm on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for mail server i am using Default SMTP virtual server in IIS...

I have not used these modules but have done a lot of mailing from IIS/other windows servers using a simple sockets subroutine. In most of these, there the method gethostbyname($smtp) is used ....

If $smtp is not defined, it never worked.

$smtp = 'mail.yourserver.com';

Is there somewhere the smtp mail server is supposed to be defined?

udot

5:12 am on Sep 21, 2007 (gmt 0)

10+ Year Member



No rocknbill.. defining SMTP server is not going to solve this problem.. as I have defined in every places where it needs to be defined! Though I have solved the problem in somewhat an unusual way..(creating dynamic perl script through CGI and running those script through a .bat file using 'Scheduled Task'!)..but I eagerly wait for a normal solution to it ....thanx

perl_diver

7:19 am on Sep 22, 2007 (gmt 0)

10+ Year Member



if you are running a firewall on your PC, turn it off and retry the script as a CGI and see if that helps. This really sounds like an operating system or third party application is interferring with the http server being able to make a connection.

Dabrowski

1:40 pm on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this may be a server issue, udot, are you using IIS and Exchange on Server 2k3?

When you run the script from the command line it uses your logged on user name, and I'm assuming you have administrative rights.

When you run through CGI the scripts runs under IIS's user (IUSR_#*$!#*$!xx), which may not have permission to send mail on your Exchange server?

Have you done CGI/sendmail on that server before?