For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > how to mail ??









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to mail ??
Saurabh Singhvi

2005-03-22, 3:56 pm

#!/usr/bin/perl

foreach (`df -h|grep /dev/hda11`) {
split();
# our $total = $_[1];
# our $used = $_[2];
our $free = $_[3];
if ($free =~ /M/){
$free=~ s/M//;
print $free."\n";
if ($free <= 10) {
$mesg = "the space left is $free";
print("low on space.\n");
system('mailx','-s','"Space
low"','sobers_2002@iitb.ac.in','<',qq($mesg)) == 0 or die "Couldn't
mail $!";
}

}
}


this isn't working....i have to press Ctrl+D to send the mail.....this
script will be cronned so i need the mail to be send
automatically......what can i do ?? alternatives???

thnx in adv
Saurabh
Wiggins d'Anconia

2005-03-22, 3:56 pm

Saurabh Singhvi wrote:
> #!/usr/bin/perl
>
> foreach (`df -h|grep /dev/hda11`) {
> split();
> # our $total = $_[1];
> # our $used = $_[2];
> our $free = $_[3];
> if ($free =~ /M/){
> $free=~ s/M//;
> print $free."\n";
> if ($free <= 10) {
> $mesg = "the space left is $free";
> print("low on space.\n");
> system('mailx','-s','"Space
> low"','sobers_2002@iitb.ac.in','<',qq($mesg)) == 0 or die "Couldn't
> mail $!";
> }
>
> }
> }
>
>
> this isn't working....i have to press Ctrl+D to send the mail.....this
> script will be cronned so i need the mail to be send
> automatically......what can i do ?? alternatives???
>
> thnx in adv
> Saurabh
>


Use one of the many modules found on CPAN for mail handling. Handling
mail manually like above is a very bad idea. Or if this is really just
going to be cron'd then just print to STDOUT, anything cron sees on
stdout it will mail to the user running the script, set up a proper
..forward and everything should just work.

http://danconia.org
Jay Savage

2005-03-22, 3:56 pm

On Tue, 22 Mar 2005 17:46:30 +0000, Saurabh Singhvi
<saurabhsinghvi@gmail.com> wrote:
> #!/usr/bin/perl
>
> foreach (`df -h|grep /dev/hda11`) {
> split();
> # our $total = $_[1];
> # our $used = $_[2];
> our $free = $_[3];
> if ($free =~ /M/){
> $free=~ s/M//;
> print $free."\n";
> if ($free <= 10) {
> $mesg = "the space left is $free";
> print("low on space.\n");
> system('mailx','-s','"Space
> low"','sobers_2002@iitb.ac.in','<',qq($mesg)) == 0 or die "Couldn't
> mail $!";
> }
>
> }
> }
>
> this isn't working....i have to press Ctrl+D to send the mail.....this
> script will be cronned so i need the mail to be send
> automatically......what can i do ?? alternatives???


That's because mailx expects your operating system's EOF. You really
need to rewrite this using Net::SMTP, or MIME::Lite, or Mail::Mailer,
or something along those lines. as a stop-gap measure, try ending
$mesg with "\n.\n"; some versions of mail will accept a . on a line by
itself as EOF, but I don't know how that works with STDIN in pipes.

More importantly, though, if you're ever planning to expand this
script, it has a fairly fatal flaw: the result of `` isn't
automatically evaluated in list context. Store your system call to an
array first. You also don't need to test m// before s///; if is
doesn't match s/// won't do anything. And finally, you should be
getting errors about non-numeric tests since you don't strip anything
other than 'M'. You can ignore them, but they'll eventually get
annoying.

#!/usr/bin/perl
use strict ;
use warnings ;

my @parts = `df -h|grep hda`;

foreach (@parts) {
(my $free = (split())[2]) =~ s/M//;
if ($free <= 10){
# rewrite this part with a module from cpan
my $mesg = "the space left is $free\n.\n";
system('mailx','-s','"Space low"','sobers_2002@iitb.ac.in',
'<',qq($mesg)) == 0 or die "Couldn't mail $!";
}
}

HTH

--jay
John Moon

2005-03-22, 3:56 pm

Subject: how to mail ??

#!/usr/bin/perl

foreach (`df -h|grep /dev/hda11`) {
split();
# our $total = $_[1];
# our $used = $_[2];
our $free = $_[3];
if ($free =~ /M/){
$free=~ s/M//;
print $free."\n";
if ($free <= 10) {
$mesg = "the space left is $free";
print("low on space.\n");
system('mailx','-s','"Space
low"','sobers_2002@iitb.ac.in','<',qq($mesg)) == 0 or die "Couldn't
mail $!";
}

}
}


this isn't working....i have to press Ctrl+D to send the mail.....this
script will be cronned so i need the mail to be send
automatically......what can i do ?? alternatives???

thnx in adv
Saurabh


Here something that I do routinely...

<snip>
open MAILMAN, "| rmail $To_Address";
print MAILMAN "Subject: Some subject\n";
print MAILMAN "From: $Reply_to\n\n";
print MAILMAN "some message.....";
close MAILMAN;
</snip>


Wiggins d'Anconia

2005-03-22, 8:55 pm

Moon, John wrote:
[snip]

>
> Here something that I do routinely...
>
> <snip>
> open MAILMAN, "| rmail $To_Address";
> print MAILMAN "Subject: Some subject\n";
> print MAILMAN "From: $Reply_to\n\n";
> print MAILMAN "some message.....";
> close MAILMAN;
> </snip>
>


And routinely it is a bad idea...please don't encourage its use here.
Mail should be handled delicately, by a module designed to do it properly.

http://danconia.org
John Moon

2005-03-22, 8:55 pm


Moon, John wrote:
[snip]

>
> Here something that I do routinely...
>
> <snip>
> open MAILMAN, "| rmail $To_Address";
> print MAILMAN "Subject: Some subject\n";
> print MAILMAN "From: $Reply_to\n\n";
> print MAILMAN "some message.....";
> close MAILMAN;
> </snip>
>


And routinely it is a bad idea...please don't encourage its use here.
Mail should be handled delicately, by a module designed to do it properly.

http://danconia.org

FMI... What the difference in:

open EXTRACT, "|sort > /tmp/Tmp$$";

And

open MAILMAN, "|rmail $to_someone";


jwm
Wiggins d'Anconia

2005-03-22, 8:55 pm

Moon, John wrote:
> Moon, John wrote:
> [snip]
>
>
>
> And routinely it is a bad idea...please don't encourage its use here.
> Mail should be handled delicately, by a module designed to do it properly.
>
> http://danconia.org
>
> FMI... What the difference in:
>
> open EXTRACT, "|sort > /tmp/Tmp$$";
>
> And
>
> open MAILMAN, "|rmail $to_someone";
>
>
> jwm
>
>


Nothing. Don't encourage either, I certainly haven't. They are both
insecure, error prone, non-portable, slow, and generally poorly written.

I'm not saying you can't do it, or that it won't work, just please don't
encourage it in a forum where beginners are (hopefully) trying to learn
good habits and methods.

So if I want my output as an attachment, does your example work? How
much code do you think it would take to get it to work? Do you know all
of the intricacies of the message protocol to do it without looking at
an RFC?

What if I am running in a chroot'd environment?

http://danconia.org
Nikhil

2005-03-27, 3:55 pm

Why not use a module Mail::Sendmail
grab the output relatively into an array
configure the mail parameters and send the output array as the mailbody.
looks fine.
if the output is needed as an attachment , use the system command
uuencode with mailx that should do.
Wiggins d'Anconia wrote:
> Moon, John wrote:
>
>
> Nothing. Don't encourage either, I certainly haven't. They are both
> insecure, error prone, non-portable, slow, and generally poorly written.
>
> I'm not saying you can't do it, or that it won't work, just please don't
> encourage it in a forum where beginners are (hopefully) trying to learn
> good habits and methods.
>
> So if I want my output as an attachment, does your example work? How
> much code do you think it would take to get it to work? Do you know all
> of the intricacies of the message protocol to do it without looking at
> an RFC?
>
> What if I am running in a chroot'd environment?
>
> http://danconia.org

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com