Solved

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

Posted on 2003-12-09
2
182 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now