Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Emailing from a Perl program

Posted by enquirer (enquirer), 8 August 2002
An enquirer by email wrote:

In the following context is it also possible to generate a email if a file cannot be opened

open (FILEHANDLE, "/file/location/somename.txt") || die "Problem with file opening - $!"

Can I put something on the end like a shell script statement

&& (echo "From: a_user@email.com"; echo "Subject: Something"; mail a_anotheruser@.email.com)

So in essence it would look like:

open (FILEHANDLE, "/file/location/somename.txt") || system (echo "From: a_user@email.com"; echo "Subject:
Something"; mail a_anotheruser.email.com) && die "Problem with file opening - $!"

Posted by admin (Graham Ellis), 8 August 2002
Yes - you can get the server to send you an email - sample code follows.  One of the things that does vary from server to server is which email programs are available, and where they're located; my example uses the most common location for Linux / Unix boxes. I have also added a nice error message for the user, rather than have them get the default "Internal Server Error 500" message.

Code:
$wantfile = "insertfilenamehere";
unless ( open (FH,$wantfile)) {
       open (MAIL, "|/usr/lib/sendmail graham\@wellho.net") ;
       print MAIL "Subject: Web server cannot read file $wantfile\n";
       print MAIL "From: Your Web Server  <admin\@kwellho.net>\n\n";
print MAIL <<"XXX";
This is the body of the message that will be sent by the web server if
a CGI script that tries to open the file in \$wantfile should fail.
XXX
       close (MAIL);
       print "Content-type: Text/HTML\n\n";
print BROWSER <<"YYY";
<html>
<head><title>Error Page</title></head>
<body bgcolor=white>
Your requst to the server failed.  You do not need to report the
problem, as an email has been sent to the server administrator to
detail the problem.
</body></html>
YYY
} else   {
# Insert here the real job if the file opens correctly
}




Posted by John_Moylan (John_Moylan), 10 August 2002
I have seen many people send mail by piping to sendmail(or whatever) as Grahams example.

But there are some Modules that can help, Mail::Mailer being the one that I regularly use.

Code:
use Mail::Mailer;
my $mailer = Mail::Mailer->new("sendmail");

my $mailmssg = "Theres more than one way to do it";

$mailer->open({From    => "never@spam.com",
              To      => "always@spam.com",
              Cc      => "seldom@spam.com",
              BCc     => "often@spam.com",
              Subject => "We all spam together",
             }) or croak "Can't do the mail thing : $!";

print $mailer $mailmssg;
$mailer->close();


(Sorry about the bad code formatting above, its a bit difficult when using the BB's code tags, but I hope you get the idea.)

I usually create a function wrapper for the Module to simplify calling it, but it's a nice clean way if your sending multiple emails from a program, and it handles 'CC & BCC' nicely too.

http://search.cpan.org/author/MARKOV/MailTools-1.48/Mail/Mailer.pm

jfp

Posted by John_Moylan (John_Moylan), 10 August 2002
Oh, and on the subject of this thread, I mainly used this to have the server mail me if it encountered errors.

Didn't want to be seen a moving "off topic"



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho