Link to home
Start Free TrialLog in
Avatar of alex1234
alex1234

asked on

send email script

I have this script to send email:

open (MAIL, '/usr/bin/sendmail -t');
print MAIL "To: user@some.com\n";
print MAIL "Subject: Test\n\n";
print MAIL "body!";
close (MAIL);

How can I check that open() realy "opens"? I do not want to terminate application, I just want something like:
if(!Open())
{
     print MAIL "To: user@some.com\n";
     print MAIL "Subject: Test\n\n";
     print MAIL "body!";
     close (MAIL);
}

but this does not work - even if I supply garbage to Open() it still succeeds but close() crashes my CGI.
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

open (MAIL,'/usr/bin/sendmail -t') or die "Can't fork sendmail: $!\n";
Avatar of alex1234
alex1234

ASKER

I do not want to "die", I want to continue executing my script by skipping sending mail. How can I do it?
open returns non zero on success so
if( open(MAIL,'/usr/bin/sendmail -t') )
{
    print MAIL "To: user@some.com\n";
    print MAIL "Subject: Test\n\n";
    print MAIL "body!";
    close (MAIL);
}
else {
   print "couldn't mail\n"
}


teraplane, thanks, but this is the first thing that I tried. It didn't work. Open() was always "true" but on close() I got "Server error" and CGI crashed. Actually, I checked return value from Open() - in the crash case it was something like 8500 and in the non-crash case 6500. It was never "false"!
maybe this will help,

NAME
Net::SMTP - Simple Mail Transfer Protocol Client



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

SUPPORTED PLATFORMS
Linux
Solaris
Windows

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

SYNOPSIS
    use Net::SMTP;


    # Constructors
    $smtp = Net::SMTP->new('mailhost');
    $smtp = Net::SMTP->new('mailhost', Timeout => 60);


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

DESCRIPTION
This module implements a client interface to the SMTP and ESMTP protocol, enabling a perl5 application to talk to SMTP servers. This documentation assumes that you are familiar with the concepts of the SMTP protocol described in RFC821.

A new Net::SMTP object must be created with the new method. Once this has been done, all SMTP commands are accessed through this object.

The Net::SMTP class is a subclass of Net::Cmd and IO::Socket::INET.



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

EXAMPLES
This example prints the mail domain name of the SMTP server known as mailhost:

    #!/usr/local/bin/perl -w


    use Net::SMTP;

    $smtp = Net::SMTP->new('mailhost');
    print $smtp->domain,"\n";
    $smtp->quit;
This example sends a small message to the postmaster at the SMTP server known as mailhost:

    #!/usr/local/bin/perl -w


    use Net::SMTP;

    $smtp = Net::SMTP->new('mailhost');

    $smtp->mail($ENV{USER});
    $smtp->to('postmaster');

    $smtp->data();
    $smtp->datasend("To: postmaster\n");
    $smtp->datasend("\n");
    $smtp->datasend("A simple test message\n");
    $smtp->dataend();

    $smtp->quit;

----------------------------------------------------
example doesnot use sendmail
lexxwern,

1. in this code you still do not check for errors. Wouldn't it happen the same thing as with my code (silent crash)?

2. use Net::SMTP - do all servers have this object? Mine is Apache

3. $smtp = Net::SMTP->new('mailhost', Timeout => 60) -what does it do?



the code i gave you is taken from the documentation of perl so it will not have any errors.


use Net::SMTP, this module comes preinstalled with most perl packages and binaries, in very rare cases will perl not have this module, if it doesn't ask your sys admin to install it.


$smtp = Net::SMTP->new('mailhost'); in this line you have top replace mailhost with the address of an smtp server, for example your mail smtp server.


pls see the documentation of perl for more info, cause it will not make sense for me to post the complete documentation here.
Thanks, I will test it, but not today, tomorrow unfortunately
The "use Net::SMTP" command gives "Server error". Does this mean that my ISP does not have NET::SMTP installed?
 
ASKER CERTIFIED SOLUTION
Avatar of jhurst
jhurst

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jhurst,

1. will "$$" automatically be replaced with process id for me?

2. after open() returns, is it 100% that error.$$.fil file is already created and valid?

3. what if I use open(FIL,"|/usr/sbin/sendmail -t 2>error.txt") instead with no process ID (why do I need process ID involved)?



 
jhurst,

1. will "$$" automatically be replaced with process id for me?

2. after open() returns, is it 100% that error.$$.fil file is already created and valid?

3. what if I use open(FIL,"|/usr/sbin/sendmail -t 2>error.txt") instead with no process ID (why do I need process ID involved)?



 


>>1. will "$$" automatically be replaced with process id for me?

Yes.  $$ is defined as being the process id of the running process.  I suggested that you use it just in case two people are using the script at the same time.  This will ensure that thy get different and separate files.


>>2. after open() returns, is it 100% that error.$$.fil file is already created and valid?

Yes.  Because the 'xxxx' is processed as a shell it is the case that the 2> will cause any error from the invoked process to be placed in that file.

>>3. what if I use open(FIL,"|/usr/sbin/sendmail -t 2>error.txt") instead with no process ID (why do I need process ID involved)?


This will be fine, unless two people happen to invoked the script at the same time.  In fact, if this is not a risk I sort of prefer it since then there would be no need to remove the old ones, you would just know that the file contained the error status of the last one.