msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:

Hi And greetings Monks,

Firstly i would thank all the monks for there support and help in the group by which i have gained a lot of knowledge and which has helped me a lot. I am presently working on sending mail i have seen the sendmail program in the Search and i have got the program using the sendmail that is since i am working on linux systems but wheni am using the Mail::SendMail module in the CGI script i am getting and error i think that i am missing something out here or i have done something wrong here i need ur help monks i have tried in it but i am getting

Software error:

socket failed (Permission denied)

For help, please send mail to the webmaster

(root@localhost), giving this error message and the time and date of the error.

firstly i have tried by using the sendmail pipe there it worked perfectly and i was able to send mail but when i am using the module i am getting the error. And here is my Code plz do help me out i am in my learning stages.

#!/usr/bin/perl #Sending A Mail To Single Person use strict; use Mail::Sendmail; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $ob = CGI->new; print $ob->header, $ob->start_html( -title =>" Send An E-mail" ), $ob->h2($ob->i("<u>Sending Mail </u>")), $ob->hr({-color=>"black"}); if ( $ob->param('Submit') ) ## If u Press submit Bu +tton the Get form values & Send Mail { my $ur_name = $ob->param('sender'); my $ur_eid = $ob->param('ur_email'); my $rp_name = $ob->param('recipient'); my $rp_eid = $ob->param('res_email'); my $subjt = $ob->param('subj'); my $mssg = $ob->param('msg'); print $ob->b("Ur : $ur_eid Rps: $rp_eid Subject: $subjt Me +ssage: $mssg <br> "); my %mail = ( To => "$rp_eid" , From => "$ur_eid" , Subject => "$subjt" , Message => " $mssg " ); sendmail(%mail) or die $Mail::Sendmail::error; print "OK. Log says:\n", $Mail::Sendmail::log; # &sendmail( $ob, $ur_eid, $rp_eid, $subjt, $mssg); ### Sen +ding The Mail # # sub sendmail # { # my ($ob, $from, $to, $subject, $message) = @_; # $ENV{PATH} = "/usr/sbin"; # open (MAIL, "|/usr/sbin/sendmail -oi -t") or # &dienice("Can't fork for sendmail: $!\n"); # print MAIL "To: $to\n"; # print MAIL "From: $from\n"; # print MAIL "Subject: $subject\n\n"; # print MAIL "$message\n"; # close(MAIL); # } print <<EndHTML; <h2>Thank You</h2> <p>Thank you for writing!</p> <p>Return to our <a href="">home page</a>.</p> EndHTML } else { print $ob->start_form( -name =>'email' , -method => "POST" , -action =>"http://localhost/cgi-bin/cgiprog +/send_email.cgi"); # printf $ob->b( "user/group:%s/%s</br>\n",scalar getpwuid( $< +), scalar getgrgid( $( ) ); print "<table>"; print "<tr><td>"; print $ob->b($ob->i("Sendername")); print " &nbsp &nbsp "; print $ob->textfield(-name =>'sender',-size=>'25'); print "</td><td>"; print " &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp "; print $ob->b($ob->i("Ur Email Id")); print " &nbsp &nbsp "; print $ob->textfield(-name =>'ur_email',-size=>'25'), $ob->br; print "</td></tr></table>"; print $ob->br($ob->br); print "<table>"; print "<tr><td>"; print $ob->b($ob->i("RecipientName")); print " &nbsp "; print $ob->textfield(-name =>'recipient' , -size => '25'); print "</td> <td>"; print "&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp "; print $ob->b($ob->i(" Email To")); print " &nbsp "; print $ob->textfield(-name =>'res_email', -size=>'25'); print "</td></tr>"; print "</table>"; print $ob->br($ob->br), $ob->b($ob->i("Subject")), $ob->textfield(-name =>'subj', -size => '74'), $ob->br($ob->br), $ob->b($ob->i("Message")), $ob->br, $ob->textarea(-name =>'msg', -rows =>'15' , -cols => '80'), $ob->br($ob->br); print "<Center>"; print $ob->submit(-name => "Submit" , -value =>"Send Mail"); print "&nbsp &nbsp &nbsp"; print $ob->reset; print $ob->end_form; } print $ob->end_html;
Thanks Sushil

Replies are listed 'Best First'.
Re: Using The Mail::Sendmail module in CGI Script
by monkfan (Curate) on Jul 19, 2006 at 07:46 UTC
    firstly i have tried by using the sendmail pipe there it worked perfectly and i was able to send mail but when i am using the module i am getting the error.
    It's not quite clear what do you mean by that sentence. Do you mean that the Mail::Sendmail module doesn't work under your cgi script although it works as normal Perl script?

    If so perhaps your problem is that your script doesn't recognize the module. You can try to direct the location of your perl modules with .htaccess.

    In a .htaccess file, put
    SetEnv PERL5LIB /where/the/mods/are/:/where/else/lib
    Then get your script going, by the following snippet.
    #!/usr/bin/perl -w use CGI ':standard'; # Newly inserted BEGIN { if ( $ENV{PERL5LIB} and $ENV{PERL5LIB} =~ /^(.*)$/ ) { # Blindly untaint. Taintchecking is to protect # from Web data; # the environment is under our control. eval "use lib '$_';" foreach ( reverse split( /:/, $1 ) ); } } use Mail::Sendmail; # The rest of the script....

    Regards,
    Edward
      Yes Sir

      Its right it works in normal perl script but does not work when use the CGI script. Ok so that means ur right that my CGI script doesn't recognize the module.

      Ok i am going to try it out

Re: Using The Mail::Sendmail module in CGI Script
by imp (Priest) on Jul 19, 2006 at 11:35 UTC
    The error is coming from this block of code in Mail::Sendmail:
    unless ( socket S, AF_INET, SOCK_STREAM, scalar(getprotobyname + 'tcp') ) { return fail("socket failed ($!)") }
    What this means is that the error occurs when the system is creating a socket, but before it attempts to connect to anything.

    Are you running fedora? This sounds like a problem with SELINUX preventing the script from creating a socket.

Re: Using The Mail::Sendmail module in CGI Script
by b10m (Vicar) on Jul 19, 2006 at 08:31 UTC

    This is not really answering your question, but I noticed you just blindly use the user's input. Never trust your users! ;-) What if I craft my POST or GET request so that the recipient variable looks something like:

    recip1@doma.in\nBcc: recip2@doma.in, recip3@doma.in, recip4@doma.in
    Please run some tests over the values you receive, before spitting them to a mail process.

    Spammers will love your script ;-)

    --
    b10m

    All code is usually tested, but rarely trusted.
      yeah but i am just trying out this program and not yet tried to complete it . But what u have said is that my script is having security risk so how can i enable it.