16

I know that I can configure a print command in Mutt via .muttrc, e.g.:

set print=ask-yes
set print_command="..."

But what print command should I use for pretty printing?

Well, a2ps output seems a little bit spartan, even with the -=mail shortcut. It does not support UTF8. There is paps which is described as 'UTF-8 to PostScript converter using Pango' and currently does not include support for margins, or Email layout or something like that.

mp: nice default header. Again no UTF8 support. Default margins are ridiculously small - if you specify wider margins the body text is printed behind the footer.

enscript: -G for fancy page headers is indeed fancy. No UTF8 support.

What do you use in the 21st century to print your mails from Mutt?

(It should support UTF8, DIN A4 output as PS and/or PDF)

maxschlepzig
  • 56,316
  • 50
  • 205
  • 279
  • I deleted the notes about MIME because mutt does not pipe MIME information to the `print_command`. Thus, it is not sufficient to test print_commands via the `|` (pipe) command in mutt. – maxschlepzig Sep 11 '11 at 10:11
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackexchange.com/rooms/23952/discussion-on-question-by-maxschlepzig-pretty-print-mails-from-mutt). – terdon May 20 '15 at 17:18

4 Answers4

20

There is also Muttprint. It does support UTF-8 and its output looks decent by default. Muttprint uses LaTeX and provides hooks for customization.

Since I am printing mails from different environments I can't use a default printer. Thus I have set it up (via ~/.muttrc) to use a PDF viewer where I can dynamically select a printer:

set print_command='set -e; f=`mktemp`; \
        muttprint -P A4 -p TO_FILE:"$f"; evince "$f"; rm "$f"'

Mktemp uses /tmp, by default. For using a custom cache directory:

set print_command='set -e; f=`mktemp -p "$HOME"/.cache/mutt`; \
        muttprint -P A4 -p TO_FILE:"$f"; evince "$f"; rm "$f"'

Btw, you can set print_decode inside muttrc to configure if the print_command is able to decode MIME, encodings etc. on its own. By default it is set to yes which is the right setting for commands like muttprint, enscript etc.

Regarding the UTF-8 issues of a2ps, enscript and mp one can just conclude that they should be considered obsolete. Indeed, looking at the project pages they don't seem to be maintained (e.g. last news item of the mp page is from 2002 and it mentions a mailing list located at Sun).

maxschlepzig
  • 56,316
  • 50
  • 205
  • 279
  • This lead to a security flaw: `/tmp/$USER.ps` is highly predictable by other users. Use `mktemp` create temporary files! (See [my answer](https://unix.stackexchange.com/a/366386/27653)). – F. Hauri - Give Up GitHub May 21 '17 at 15:22
  • @F.Hauri, yes, relying on `/tmp/$USER.ps` is silly. I've updated my answer. – maxschlepzig May 22 '17 at 21:47
  • mktemp will fail if file already exist or if directory don't. On failure, your cache directory will fill with unwanted printed mails. This is the main reason why `/tmp/*` is deleted at reboot. – F. Hauri - Give Up GitHub May 23 '17 at 05:53
  • @F.Hauri, `mktemp`'s raison d'être is to avoid file name collisions. It may fail for other reasons, though. I've changed the command such that muttprint is only executed if mktemp was successful (I suspect muttprint would default to some other filename when `f` is empty). Thus, now you only end up with a left-over temp file if evince or rm fail. I consider this unlikely - and if they do I consider having the problematic file retained a help for debugging the issue. Sure, `/tmp` is automatically cleaned up. But `$HOME` is advantageous for sensitive docs when it is encrypted but `/tmp` is not. – maxschlepzig May 25 '17 at 16:58
  • Use `mktemp -p $HOME/.cache/mutt-XXXXXXXXXX` in this case, but `mktemp` is the correct tool. – F. Hauri - Give Up GitHub May 26 '17 at 06:57
  • @F.Hauri, I don't see any advantages to that. – maxschlepzig May 26 '17 at 08:05
  • `mktemp` will make temporary files or directory in a safe way, As you already edited your answer for using them correctly, there is nothing to add. About `muttprint` and your *suspicion*,why don't you make a try? – F. Hauri - Give Up GitHub May 27 '17 at 06:34
  • I'm a bit sad that UTF-8 support means it renders a horizontal line as "[U+2501][U+2501][U+2501][U+2501][U+2501]..." rather than "━━━━━━━━━━━━━━━━━━━━━...", but *sigh* what can you expect from LaTeX? – Marius Gedminas Nov 08 '17 at 10:16
3

You might also consider using paps, which reads a UTF-8 encoded file and generates a PostScript language rendering of the file. The rendering is done by creating outline curves through the pango ft2 backend.

Aaron Massey
  • 217
  • 2
  • 11
3

More secure and using less paper, create a wrapper:

There is my print command for .muttrc:

set print_command="$HOME/bin/print_preview-wrapper.sh"

And there is the content of "$HOME/bin/print_preview-wrapper.sh"

#!/bin/bash

read tmpdir < <(mktemp -d /tmp/print_preview-$USER-XXXXXXXX)
cd $tmpdir || exit 1

trap "cd && rm -fR $tmpdir;exit" 0 1 2 3 6 9 15

cat >file.eml
muttprint -P A4 -p TO_FILE:file.ps <file.eml 

read numPages < <(sed '/^%%Pages/{s/%%Pages: //;q;};d' file.ps)
(( numPages > 1 )) &&
    muttprint -2 -P A4 -p TO_FILE:file.ps <file.eml 

gv file.ps

This will

  • securely create temporary dir to store current printing mail,
  • convert them to postscript, but
    • if there's more than 1 page,
    • convert them again, but putting 2 pages by sheet.
  • run gv because I like it, but you could run any other tool.
  • delete temporary directory on exit.
  • Has `read tmpdir < <(mktemp -d /tmp/print_preview-$USER-XXXXXXXX)` an advantage over `tmpdir=$(mktemp -d /tmp/print_preview-$USER-XXXXXXXX)`? – maxschlepzig May 22 '17 at 21:53
  • @maxschlepzig for only 1 variable, not really. This syntax could be usefull for assigning many variables at once: `read host foo foo ip < <(host google.com); echo $ip $host` – F. Hauri - Give Up GitHub May 23 '17 at 05:48
1

Here is a simple receipe I use on different UNIXes:

set print_command="iconv --unicode-subst="_" -f utf-8 -t iso-8859-1 | enscript -P ••My_printer••"

where ••My_printer•• is the name of the printer.

dan
  • 913
  • 6
  • 23
  • Squishing all Unicode characters outside the basic Latin-1 repertoire seems like probably not what the OP is looking for, and so last millennium. – tripleee May 21 '17 at 16:59