LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2008, 08:07 AM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Question Needing help with the linux mail command


over the past month or so, I do have to thank all of you for your tremendous help that you have given me in Perl and Shell scripting. I have learned tons and I look forward to being just as helpful to others as well.
In fact I am steadily rewriting shell codes we have to Perl so that I may further my knowledge of it. The Perl scripts that I have been editing have been done by someone a long time ago that is no longer employed.

The new problem on the same script is the shell command of mail. The script does what it supposed to do of creating a list of users and their stats and then creating a list of printer and their stats without breaking code or needing two different scripts. I managed to do it with sub routines however I have noticed that when I try to use the

Code:
exec("$home/bin/test /<path>/$f | mail -s \"$h\" prn-testing");
where $f is the log file (which changes ever month) and the $h is the subject. It will only email the first half of what is printed to the screen. How do I solve this without writing it to a file and then emailing the file? This server, however powerful, the powers to be do not want anything else installed such as modules and such. All this has been done the long way and some ways via shell calls using system and exec. If I end up have to write a file, how do I write the file on a temp bases and when it gets mailed, erase it?
 
Old 06-10-2008, 10:38 AM   #2
theriddle
Member
 
Registered: Jun 2007
Distribution: Gentoo
Posts: 172

Rep: Reputation: 30
I think (it's doing this on my system) that the second half is being sent to the CC (carbon copy).

On my system, when I type "mail -s test <my-user>"
Code:
$ mail -s test <my-user>
test
^D
Cc:
To fix:
Code:
$ sendmail <my-user>
Subject: test
test
^D
Or in perl (using proper Perl IPC):
Code:
open(LOG,"$home/bin/test /<path>/$f|");
open(MAIL,"|sendmail prn-testing");
print MAIL "Subject: $h\n";
while (<LOG>) {
        print MAIL "$_\n";
}
close(LOG);
close(MAIL);
 
Old 06-10-2008, 10:44 AM   #3
theriddle
Member
 
Registered: Jun 2007
Distribution: Gentoo
Posts: 172

Rep: Reputation: 30
Oh, and 2 last-minute mentions:
1) Since the log is now going through the perl script, you can pre-process it like so:
Code:
while (<LOG>) {
  if (/some_regular_expression/) {
    print MAIL "$_\n";
  }
}
2) sendmail (as the man page mentions) is meant for these types of tasks.
 
Old 06-10-2008, 11:53 AM   #4
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

so the I can invoke it like

Code:
exec("$home/bin/test /<path>/$f | sendmail \"$h\" prn-testing");
where it will run the script and the pipe that output via the send mail or does the send mail have to be in the place of the write command?

IE:

Code:
sub writeUR {
        for (sort { $tot_page_count{$a}<=>$tot_page_count{$b} } (keys(%tot_page_count)))  {
                $tax = $totals{$uname} * .06;
                $~ = "STDOUT";
                write;  (where the print MAIL "$_\n"; would be here)
        }
}
 
Old 06-10-2008, 12:14 PM   #5
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

actually I get this:

print@papyrus:~/bin> sendmail
-bash: sendmail: command not found
print@papyrus:~/bin>
 
Old 06-10-2008, 12:15 PM   #6
theriddle
Member
 
Registered: Jun 2007
Distribution: Gentoo
Posts: 172

Rep: Reputation: 30
Quote:
Originally Posted by HyperTrey View Post
so the I can invoke it like

Code:
exec("$home/bin/test /<path>/$f | sendmail \"$h\" prn-testing");
where it will run the script and the pipe that output via the send mail or does the send mail have to be in the place of the write command?
No. sendmail expects the headers (like Subject) to be in the message itself in the form of "Name: value".
As a (drop-in) example:
Code:
exec("/bin/echo -e \"Subject: $h\\n`$home/bin/test /<path>/$f`\" | sendmail prn-testing");
However, this:
Code:
open(LOG,"$home/bin/test /<path>/$f|");
open(MAIL,"|sendmail prn-testing");
print MAIL "Subject: $h\n";
while (<LOG>) {
        print MAIL "$_\n";
}
close(LOG);
close(MAIL);
while being slightly larger, uses more Perl constructs than shell ones, and is easier to understand.
 
Old 06-10-2008, 12:25 PM   #7
theriddle
Member
 
Registered: Jun 2007
Distribution: Gentoo
Posts: 172

Rep: Reputation: 30
Quote:
Originally Posted by HyperTrey View Post
actually I get this:

print@papyrus:~/bin> sendmail
-bash: sendmail: command not found
print@papyrus:~/bin>
Try /usr/lib/sendmail.

Do you have a MTA on the system at all?
 
Old 06-10-2008, 01:19 PM   #8
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
I could not get the sendmail to work. It seems to be in the /usr/lib folder however it hangs almost every time and the other times it just did nothing. I solved the problem by just redirecting the output to a temp file and then removing the temp file as the script is done.
 
Old 06-10-2008, 01:30 PM   #9
theriddle
Member
 
Registered: Jun 2007
Distribution: Gentoo
Posts: 172

Rep: Reputation: 30
Quote:
Originally Posted by HyperTrey View Post
I could not get the sendmail to work. It seems to be in the /usr/lib folder however it hangs almost every time and the other times it just did nothing. I solved the problem by just redirecting the output to a temp file and then removing the temp file as the script is done.
sendmail is supposed to "hang". Type out the message and press ^D (control+d). Is the MTA on the same system you're sending from (that would explain the hang).

To add a subject, run sendmail like this.
Code:
$ sendmail <to>
Subject: <sub>
<msg>
^D

Last edited by theriddle; 06-10-2008 at 01:32 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Needing a console command sloteel Linux - Software 2 01-10-2008 05:55 AM
mail settings in linux mail command cad Linux - General 1 01-27-2007 04:23 AM
Linux command mail question very easy for you not for me ! zonemikel Linux - Newbie 1 09-25-2006 11:59 AM
Cannot send mail with mail() command in PHP 4.3.4, Apache 2.0.48, Mandrake Linux 10 arcanum Linux - General 3 06-27-2004 06:06 PM
n00b needing help with loading multiple progz using one ./command event chaz Linux - Newbie 2 04-29-2002 07:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration