Linux.com

Taraman

Taraman

  • Linux.com Member
  • Posts: 3
  • Member Since: 19 Jan 11
  • Last Logged In: 02 Mar 11

Latest Posts

Posted by
Topic
Post Preview
Posted
  • Taraman
    RE: auto print email attachments as they arrive
    I fear, that is not my cup of tea...
    Link to this post 31 Jan 11

    I fear, that is not my cup of tea...

  • Taraman
    RE: auto print email attachments as they arrive
    I now added an eMail-Response in case of failure to print. This needs a working [i]mail[/i] command. [code]#!/bin/bash SUPPORTED_FILETYPES=".pdf.jpg.png.tif.gif" LP_OPTIONS="-o media=A4,tray1 -o fit-to-page -o position=top -o scaling=100" MAILFILE=~/mailtemp/$(date +%H%M%S).txt PRINT_FOLDER=~/printable /usr/bin/fetchmail --bsmtp $MAILFILE if [ "$?" = "0" ]; then MAIL_ADDRESS=$(grep 'From:' $MAILFILE | sed -n -e 's/^[^<]*<\([^>]*\)>.*$/\1/p') /usr/bin/uudeview +e $SUPPORTED_FILETYPES -p $PRINT_FOLDER -i $MAILFILE rm $MAILFILE PRINTED="no" for f in $PRINT_FOLDER/* do if [ "$f" != "$PRINT_FOLDER/*" ]; then LP_OUTPUT=$(lp $LP_OPTIONS $f) if [ "$?" != "0" ]; then MAILTEXT="File $f could not be printed." echo "$MAILTEXT" | mail -s "Print-Error" $MAIL_ADDRESS fi rm $f PRINTED="yes" else if [ "$PRINTED" = "no" ]; then echo "No printable Attachments" | mail -s "Print-Error" $MAIL_ADDRESS fi fi done ~/mailprint fi[/code] For that to work, you have to add the following line to your [i].fetchmailrc[/i]: [code]fetchlimit 1[/code] With this line, only one mail is received each time the script is executed. Therefore the script calls itself again as long as messages are downloaded.
    Link to this post 30 Jan 11

    I now added an eMail-Response in case of failure to print.
    This needs a working mail command.

    #!/bin/bash
    SUPPORTED_FILETYPES=".pdf.jpg.png.tif.gif"
    LP_OPTIONS="-o media=A4,tray1 -o fit-to-page -o position=top -o scaling=100"

    MAILFILE=~/mailtemp/$(date +%H%M%S).txt
    PRINT_FOLDER=~/printable

    /usr/bin/fetchmail --bsmtp $MAILFILE

    if [ "$?" = "0" ]; then
    MAIL_ADDRESS=$(grep 'From:' $MAILFILE | sed -n -e 's/^[^<]*<\([^>]*\)>.*$/\1/p')
    /usr/bin/uudeview +e $SUPPORTED_FILETYPES -p $PRINT_FOLDER -i $MAILFILE
    rm $MAILFILE

    PRINTED="no"

    for f in $PRINT_FOLDER/*
    do
    if [ "$f" != "$PRINT_FOLDER/*" ]; then
    LP_OUTPUT=$(lp $LP_OPTIONS $f)
    if [ "$?" != "0" ]; then
    MAILTEXT="File $f could not be printed."
    echo "$MAILTEXT" | mail -s "Print-Error" $MAIL_ADDRESS
    fi
    rm $f
    PRINTED="yes"
    else
    if [ "$PRINTED" = "no" ]; then
    echo "No printable Attachments" | mail -s "Print-Error" $MAIL_ADDRESS
    fi
    fi
    done
    ~/mailprint
    fi

    For that to work, you have to add the following line to your .fetchmailrc:

    fetchlimit 1

    With this line, only one mail is received each time the script is executed.
    Therefore the script calls itself again as long as messages are downloaded.

  • Taraman
    RE: auto print email attachments as they arrive
    Hi, I see this post was inactive for some time and I hope my answer will still help. I just set up exactly what you are trying to do. Programs needed: [ul][li]fetchmail[/li][li]uudeview[/li][li]cups[/li][/ul] I created a user named "autoprint" for this task First you need to set up fetchmail to retrieve mail from your account and save it to a file. In the users home directory create file [i].fetchmailrc[/i] and do [i]chmod 600[/i] on it (fetchmail will issue a warning if other users have read privileges on this file) it should have following content: [code]poll your.mailserver.xx proto POP3 user your-mailbox-username pass secretpassword[/code] for proto you should also be able to use IMAP. now you just need a shell script to download mail and print it: [code] #!/bin/bash SUPPORTED_FILETYPES=.pdf.jpg LP_OPTIONS=-o media=A4,tray1 -o fit-to-page -o position=top -o scaling=100 FILENAME=$(date +%H%M%S).txt /usr/bin/fetchmail --bsmtp ~/mailtemp/$FILENAME if [ "$?" = "0" ]; then /usr/bin/uudeview +e $SUPPORTED_FILETYPES -p ~/printable -i ~/mailtemp/$FILENAME rm ~/mailtemp/$FILENAME for f in ~/printable/* do lp $LP_OPTIONS $f rm $f done fi [/code] The script does the following: [ul][li]save the mail to a file naed by the current time - just in case two files exist at the same time which should not happen[/li][li]If mail has been retrieved, pass the file to uudeview and extract all attachments with the extensions listet in FILETYPES. Which files are supported by cups, you can find with google. uudeview also accepts multiple emails in one file[/li][li]The extracted files are then printed with [i]lp[/i] using the options from LP_OPTIONS if the printer to be used is not your standard printer, you should add [i]-d printername[/i] to the lp command [/li][li]the files are deleted after being processed. So is the mail, when fetchmail downloads it[/li][/ul] When you have tested the script on the command line and it works, you have to make cron run this script every 2, 5 minutes or whatever you like. With this solution, no filtering is done on the linux box. I set up the mail-account, to only accept mails from my address, to counter spam. Sure this is not the most secure solution, but if nobody knows the mail-address and neither that there is a printer-deamon behind it, for myself I feel secure enough. You could still expand the script to extract also a S/MIME signature attached to your mails and have that before printing. Taraman
    Link to this post 20 Jan 11

    Hi,

    I see this post was inactive for some time and I hope my answer will still help.

    I just set up exactly what you are trying to do.

    Programs needed:
    [ul][li]fetchmail[/li][li]uudeview[/li][li]cups[/li][/ul]
    I created a user named "autoprint" for this task

    First you need to set up fetchmail to retrieve mail from your account and save it to a file.
    In the users home directory create file .fetchmailrc and do chmod 600 on it (fetchmail will issue a warning if other users have read privileges on this file)

    it should have following content:

    poll your.mailserver.xx
    proto POP3
    user your-mailbox-username
    pass secretpassword

    for proto you should also be able to use IMAP.

    now you just need a shell script to download mail and print it:


    #!/bin/bash
    SUPPORTED_FILETYPES=.pdf.jpg
    LP_OPTIONS=-o media=A4,tray1 -o fit-to-page -o position=top -o scaling=100

    FILENAME=$(date +%H%M%S).txt

    /usr/bin/fetchmail --bsmtp ~/mailtemp/$FILENAME

    if [ "$?" = "0" ]; then
    /usr/bin/uudeview +e $SUPPORTED_FILETYPES -p ~/printable -i ~/mailtemp/$FILENAME
    rm ~/mailtemp/$FILENAME

    for f in ~/printable/*
    do
    lp $LP_OPTIONS $f
    rm $f
    done
    fi

    The script does the following:
    [ul][li]save the mail to a file naed by the current time - just in case two files exist at the same time which should not happen[/li][li]If mail has been retrieved, pass the file to uudeview and extract all attachments with the extensions listet in FILETYPES. Which files are supported by cups, you can find with google. uudeview also accepts multiple emails in one file[/li][li]The extracted files are then printed with lp using the options from LP_OPTIONS if the printer to be used is not your standard printer, you should add -d printername to the lp command [/li][li]the files are deleted after being processed. So is the mail, when fetchmail downloads it[/li][/ul]
    When you have tested the script on the command line and it works, you have to make cron run this script every 2, 5 minutes or whatever you like.

    With this solution, no filtering is done on the linux box. I set up the mail-account, to only accept mails from my address, to counter spam.
    Sure this is not the most secure solution, but if nobody knows the mail-address and neither that there is a printer-deamon behind it, for myself I feel secure enough.
    You could still expand the script to extract also a S/MIME signature attached to your mails and have that before printing.

    Taraman

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board