Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-12-09
2
Medium Priority
?
207 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Six Sigma Control Plans

618 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