Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

22 Experts available now in Live!

Get 1:1 Help Now