Apache+Perl Net::SMTP Strange problem

Discussion in 'Perl Misc' started by zhaoningli@gmail.com, May 16, 2007.

  1. Guest

    I am setting up a web application using apache 2.2 + perl v5.8.8 on
    FC5. The application needs to send out email using a SMTP server as
    mail relay (MTA). I wrote a test perl script test.cgi. Run the script
    as CGI, it always return with errors. The code fails at:

    my $smtp=new Net::SMTP($smtpserver, Timeout =>60) || die 'Cannot
    connect to smtp server';

    The apache server is configured to run at apache:apache. The above
    script works well if I use

    bash# sudo -u apache perl test.cgi.

    Please advise how to fix this. Thanks a lot.
     
    , May 16, 2007
    #1
    1. Advertising

  2. Guest

    The full script I wrote is as the following:

    #!/usr/bin/perl

    use Net::SMTP;
    use MIME::Base64;


    print "Content-Type: text/html; charset=UTF-8\n\n\n";

    print "\n\n<html><body>Hello</body></html>\n\n";
    MessageToMTA();
    exit;

    sub trim {
    my ($str) = @_;
    if ($str) {
    $str =~ s/^\s+//g;
    $str =~ s/\s+$//g;
    }
    return $str;
    }

    sub MessageToMTA {
    my $rcpt_to = trim('');

    my $smtp_server = '192.168.1.100';
    my $smtp_user = '';
    my $smtp_pass = '123';

    my $encode_smtpuser = trim(encode_base64($smtp_user));
    my $encode_smtppass = trim(encode_base64($smtp_pass));

    my $smtp = Net::SMTP->new($smtp_server, Timeout => 60, Debug
    => 1) || die 'Cannot connect to smtp server';

    my $result = $smtp->command('AUTH', 'LOGIN');
    my $answer = $smtp->getline();

    $result = $smtp->command($encode_smtpuser);
    $answer = $smtp->getline();

    $result = $smtp->command($encode_smtppass);
    $answer = $smtp->getline();

    $smtp->mail($smtp_user);
    $smtp->to($rcpt_to);
    $smtp->data();
    $smtp->datasend('here');
    $smtp->dataend();
    $smtp->quit;
    }

    On May 16, 2:07 pm, wrote:
    > I am setting up a web application using apache 2.2 + perl v5.8.8 on
    > FC5. The application needs to send out email using a SMTP server as
    > mail relay (MTA). I wrote a test perl script test.cgi. Run the script
    > as CGI, it always return with errors. The code fails at:
    >
    > my $smtp=new Net::SMTP($smtpserver, Timeout =>60) || die 'Cannot
    > connect to smtp server';
    >
    > The apache server is configured to run at apache:apache. The above
    > script works well if I use
    >
    > bash# sudo -u apache perl test.cgi.
    >
    > Please advise how to fix this. Thanks a lot.
     
    , May 16, 2007
    #2
    1. Advertising

  3. J. Gleixner Guest

    wrote:

    > On May 16, 2:07 pm, wrote:
    >> I am setting up a web application using apache 2.2 + perl v5.8.8 on
    >> FC5. The application needs to send out email using a SMTP server as
    >> mail relay (MTA). I wrote a test perl script test.cgi. Run the script
    >> as CGI, it always return with errors. The code fails at:
    >>
    >> my $smtp=new Net::SMTP($smtpserver, Timeout =>60) || die 'Cannot
    >> connect to smtp server';
    >>
    >> The apache server is configured to run at apache:apache. The above
    >> script works well if I use
    >>
    >> bash# sudo -u apache perl test.cgi.
    >>
    >> Please advise how to fix this. Thanks a lot.


    First, can you send e-mail through $smtpserver directly?

    Second, have die() show you why the method failed:

    my $smtp=new Net::SMTP($smtpserver, Timeout =>60)
    or die "Error for $smtpserver: $!";

    Third, try the Debug parameter.
     
    J. Gleixner, May 17, 2007
    #3
  4. Guest

    Problem has been resolved. Because I enabled SELiunx in FC5, if I
    setenforce to 0, there will be no problem.

    On May 18, 3:04 am, "J. Gleixner" <>
    wrote:
    > wrote:
    > > On May 16, 2:07 pm, wrote:
    > >> I am setting up a web application using apache 2.2 + perl v5.8.8 on
    > >> FC5. The application needs to send out email using a SMTP server as
    > >> mail relay (MTA). I wrote a test perl script test.cgi. Run the script
    > >> as CGI, it always return with errors. The code fails at:

    >
    > >> my $smtp=new Net::SMTP($smtpserver, Timeout =>60) || die 'Cannot
    > >> connect to smtp server';

    >
    > >> The apache server is configured to run at apache:apache. The above
    > >> script works well if I use

    >
    > >> bash# sudo -u apache perl test.cgi.

    >
    > >> Please advise how to fix this. Thanks a lot.

    >
    > First, can you send e-mail through $smtpserver directly?
    >
    > Second, have die() show you why the method failed:
    >
    > my $smtp=new Net::SMTP($smtpserver, Timeout =>60)
    > or die "Error for $smtpserver: $!";
    >
    > Third, try the Debug parameter.
     
    , May 18, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,103
    Roedy Green
    Jan 24, 2006
  2. Alex Hunsley
    Replies:
    4
    Views:
    3,561
    Tim Williams (gmail)
    Jun 29, 2004
  3. Raaijmakers, Vincent \(GE Infrastructure\)

    RE: sending of mail (smtp) - connection refused - but smtp serveris running!

    Raaijmakers, Vincent \(GE Infrastructure\), Jun 29, 2004, in forum: Python
    Replies:
    0
    Views:
    894
    Raaijmakers, Vincent \(GE Infrastructure\)
    Jun 29, 2004
  4. Copelandia Cyanescens

    smtplib.SMTP "no attributes SMTP"

    Copelandia Cyanescens, Jul 31, 2004, in forum: Python
    Replies:
    2
    Views:
    1,482
    Copelandia Cyanescens
    Jul 31, 2004
  5. The Poor
    Replies:
    2
    Views:
    244
    Gregory Toomey
    Sep 27, 2003
Loading...

Share This Page