Solved

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

Posted on 2003-12-09
2
184 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
2 Comments
 
LVL 3

Accepted Solution

by:
terageek earned 250 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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