APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Using the spooler to print to email

© December 2003 Dirk Hart
Web Site: https://www.mailstarusa.com/

The other day i got a call from a client asking if they could have a report sent by email rather than have it printed out each night. The thing was that the report was only interesting under specific conditions so it was thrown out most of the time.

After some experimentation and some useful advice I came across this useful formula.

First define a new spooler destination using the dumb interface script and /dev/null for the device. Give your new spooler destination a name - I called mine email displaying a deficit of imagination. Next, change directory to /usr/spool/lp/admins/lp/interfaces and edit email if that's what you called it. I use and like vi for editing but you can use whatever editor you like. Finally, I added a line, changing the FILTER to get the emailing done.

Immediately after this bit:

    #If we are not using a filter, use the default one.
   if [ -z "${FILTER}" ] then

I added this bit:

   FILTER="/usr/bin/mail -s report boopy"
Where boopy is a valid user on the system. If you have sendmail or some other mail program running you can email to remote users with boopy@yourdomain.com.

Since my client wanted more than one user to receive this report by email I changed the user (boopy) to an alias and made a new alias in /usr/lib/mail/aliases. Don't forget to run newaliases if you do this, although I discovered that on Solaris it's unnecessary.

I had a little bit of help from JP Radley (jpr), which amounted to a whack on the side of the head, but it turns out he implements this with a different modification. He likes to apply this bit around the part of the script that does the actual printing: { ... } | mail -s report boopy.

   { while [ $i -le $copies ] do
           for file in $files do
                   0<${file} eval ${FILTER} 2>&1 echo "\014\c"
           done i=`expr $i + 1`
   done              } | mail -s report boopy

Scott McMillan dispenses with the 'dumb' interface script altogether preferring to roll his own:

   shift; shift; shift; shift 
   for file in $files 
      mail -s "testing through lp" root <${file}

   exit 0

Using any of these methods you can send email simply by invoking the spooler as you normally do, e.g., lp -demail /etc/hosts.

This works fine for plain text reports. Reports that have imbedded postscript or pcl are not candidates for emailing unless they can be converted to html, pcl or pdf. In this case you might prefer a solution where you can send your print jobs as an attachment.

Thanks to JP Radley and Scott McMillan for their ideas.

Editor's note:

See Printing FAQ also

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Using the spooler to print to email

Inexpensive and informative Apple related e-books:

Take Control of Parallels Desktop 12

Take control of Apple TV, Second Edition

Take Control of High Sierra

Take Control of iCloud, Fifth Edition

Take Control of Preview

More Articles by © Dirk Hart

And of course there is always the option to print to PDF: https://www.sanface.com/scolp.html
Both this article, and the PDF article are great examples of why I like working with *nix. I was once asked to have a report print at three different printers, in three different buildings, depending on the type of the report. With a little help from 'grep' 'cut' and 'head' this was easy. My Windows friends are still wondering how I did it :-)

- Bruce Garlock

Or ascii2pdf. Note that emailing stuff as an attachment is not within the capability of 'mail' (and ergo the solution presented above).


I wouldn't say that "emailing stuff as an attachment is not within the capability of 'mail'". There's nothing stopping you from manually constructing a MIME compliant message with attachment and sending it with "mail".

What "mail" lacks is any assistance in that regard, but Mutt and https://aplawrence.com/Reviews/cleancodeemail.html can help you with that.


Yeah, well, since this was on a SCO system, where are the SCO binaries?


Sco binaries for Mutt are on Skunkawre.

See https://aplawrence.com/SCOFAQ/scotec6.html#mailxmutt


Another way to do all this:

Put Scott's script in /usr/spool/lp/model/email, modified to your needs. Then,

/usr/lib/lpadmin -p email -m email -v /dev/null

/usr/lib/accept email

/usr/lib/enable email

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

FORTRAN—the "infantile disorder"—, by now nearly 20 years old, is hopelessly inadequate for whatever computer application you have in mind today: it is now too clumsy, too risky, and too expensive to use. (Edsger W. Dijkstra)

Linux posts

Troubleshooting posts

This post tagged:





Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode