# Convert incoming email to PDF



## Sylhouette (Oct 11, 2011)

Hello all.

We are using a postfix MailScanner solution to filter spam.
We also want to start archiving mails to a network share as pdf files.

Does anyone know a solution for this.
So that e-mail is archived to a pdf file from postfix or from MailScanner.

thanks 

regards
Johan


----------



## SirDice (Oct 11, 2011)

I'm not familiar with a ready made solution but there are modules you can use with various scripting languages (perl for instance) and create your own.


----------



## Carpetsmoker (Oct 12, 2011)

Why save them as a PDF file and not just as a text file?

I don't see the added value of PDF here ... I only see disadvantages (Harder to index, may truncate/destroy some data, larger).


----------



## geodni (Oct 12, 2011)

Hi,

Remember that mail may contain attachment and you might not want to keep it into your PDF file.
Try this to keep only 6 first pages in the generated PDF file using A4 paper in landscape orientation with 2 pages on one sheet, no border/header/footer and "CONFIDENTIAL" under lay :

```
a2ps -q -MA4 -2 --borders=no --no-header -b -a1,6 uCONFIDENTIAL /tmp/input.txt | ps2pdf - /tmp/output.pdf
```
If you want to pipe your mail directly to this command replace /tmp/input.txt by a single hyphen "-".


----------



## Sylhouette (Oct 13, 2011)

Thanks all for your time.

It now only prints page 1 AND 6 so i need -a1-6, and then it prints the first 6 pages of the mail.
It is awesome what you can do on the commandline.

One thing i can not get to work is how to pipe the mail through this program.

What we need is mail send to oneaddress@ourdomain.com being delivered to the mailbox the normal way, but also make a pdf copy of that mail and store it on our system.
So it needs to be done for only one e-mail address.
We use postfix, but i can not get it to pipe it through that command.

Thanks again all.

regards
Johan


----------



## geodni (Oct 13, 2011)

Hi,

To print pages 1 to 6 use "1-6", I agree.

Configure /etc/postfix/master.cf by adding a line to create your specific pipe called mypipe where /usr/local/bin/mail2pdf is your mail converter to PDF runing as user pdfmail or another user who has the ability to create files in the PDF directory store :

```
mypipe   unix   -   n   n   -   -   pipe   flags= user=pdfmail argv=/usr/local/bin/mail2pdf ${sender}
```

Configure your special address to get copy of the mail by adding a line to /etc/postfix/virtual :

```
oneaddress@ourdomain.com oneaddress@ourdomain.com,oneaddressmapped@ourdomain.com
```
And then add a line in /etc/postfix/transport to catch the mail destinated to the special address :

```
oneaddressmapped@ourdomain.com   mypipe
```
Don't forget to tell Postfix to use it by configuring /etc/postfix/main.cf :

```
transport_maps = hash:/etc/postfix/transport
virtual_alias_maps = hash:/etc/postfix/virtual
```
To finish this rehash the transport and virtual files and reload postfix if necessary.
See Postfix transport documentation for more information and also pipe and virtual.

Geodni


----------



## funky (Oct 13, 2011)

Nice, but without specifying the output of the a2ps command, the post script might be sent to the printer instead of stout.
So better use:

```
a2ps example.txt -o - | ps2pdf - example.pdf
```


----------



## Sylhouette (Oct 13, 2011)

I want to thank you all, it all works like we want it to work.

Thanks again, i will mark it solved.

I have learned a lot about postfix and piping to other commands this way.

regards,
Johan


----------



## Sylhouette (Oct 16, 2011)

One more thing i saw with testing, is that when 2 mails arive at almost the same time, the second does not get processed.

i tested with the periodic daily command, and then only the first mail get processed, the second did not get processed, proberbly because the script was busy.

how can i make sure the second mail is put on hold when the script is busy.
I also did try to create a file first, so that i can pick up the file to proces with a2ps and ps2pdf later, but this way is slow also.

thanks 

Johan


----------



## Carpetsmoker (Oct 17, 2011)

Instead of using /tmp/input.txt and /tmp/output.pdf use a random generated filename using mktemp(1)

For example:

```
#!/bin/sh
#

input=$(mktemp -t mail2pdfin)
output=$(mktemp -t mail2pdfout)
dest="/path/to/final/dest"

a2ps -q -MA4 -2 --borders=no --no-header -b -a1,6 uCONFIDENTIAL ${input} | ps2pdf - ${output}
mv ${output} ${dest}
rm ${input}
```

Otherwise one script will overwrite the files of another script if they're executed at the same time 

Make sure you move or delete the files created by mktemp, otherwise you'll end up with a very full /tmp/ very fast


----------



## Sylhouette (Oct 17, 2011)

Thanks again all, it works now and i can shoot as many mails i want at it.

regards
Johan


----------

