?
Solved

Checking, then printing a users email from a mailbox to lpr

Posted on 2003-12-09
2
Medium Priority
?
202 Views
Last Modified: 2010-03-04
Hello

I am after a script that bascially checks a Unix/Linux standard users mailbox in /var/spool/mail/$USER then prints the email inside it to the lpr program. If the mailbox has zero bytes, the command is ignored, but if there is mail and it then prints it, the users mailbox is then moved to a different location and renamed to the system time & date. I guess the program would be run by cron say every 15 minutes or so. If there is a better solution around, then it would be also a great help.

Cheers.......
0
Comment
Question by:Zoist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
terageek earned 1000 total points
ID: 9909105
#!perl

use Fcntl qw/:flock/;

my ($USER) = $ARGV[0];
my ($NEW_FILE) = $ARGV[1];
my ($NEW_FILE) .= "/";
my @time = localtime;
# Date format YYYYMMDD
$NEW_FILE .= ($time[5] + 1900) . sprintf ("%0.2d", $time[4] + 1) . sprintf ("%0.2d", $time[3]);
$NEW_FILE .= "_";
# Time format HHMMSS
$NEW_FILE . sprintf ("%0.2d", $time[2]) . sprintf ("%0.2d", $time[1]) . sprintf ("%0.2d", $time[0]);

if (!-z "/var/spool/mail/$USER") {
    #Lock the mailbox to make sure mail isn't dumped in while we are messing with it
    open MBOX, "</var/spool/mail/$USER";
    flock (MBOX, LOCK_EX) or die "can't lock mailbox: $!";

   # Move the file, unlock it, and close the handle
    system "mv /var/spool/mai/$USER $NEW_FILE";
    flock (MBOX, LOCK_UN);
    close MBOX;

   # Now print the moved file
    system "lpr $NEW_FILE";
}

You need to pass to the program the user's name and an existing directory to stick the mail files.
Your printouts will get messy if there are attatchments.
0
 

Author Comment

by:Zoist
ID: 9910637
Thanks a lot mate!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question