Pergunta

I'm using mutt over ssh as my mail client. This works great, but looking at the attachments (mostly images) is a bit annoying. Now my idea is to parse the email, store the attachments on my server and create an URL for accessing the attachment and put this URL inside the email. Has anyone done this already? What is a simple way to achieve this?

I'm already using procmail for filtering stuff, can I use it to insert text into my email?

Thanks

Foi útil?

Solução

Sure you can. There is no direct support for MIME, though, so you probably need something more or less clever to make sure you don't mess up the MIME structure of the message you are processing.

As a simple starting example, suppose you have extracted an attachment to ~/public_html/prv/att000.jpg and have your web server set up to serve this at http://localhost:8080/~you/prv/att00.jpg, you could add a header like this;

:0fhw
| formail -I"X-Tracted: http://localhost:8080/~you/prv/att00.jpg"

Adding something inline to the message itself is harder, but my no means impossible.

Edit: Your script can be simplified to not use a temporary file (and by the by, avoid the multiple Useless Uses of Cat) if you extract the Message-Id from within your recipe:

# Whitespace in [square brackets] is tab, space
:0c
* ^Message-Id:[   ]*<\/[^@>]+
| (mkdir -p "webserver path"/"$MATCH"; munpack -C "webserver path"/"$MATCH" )

... but perhaps you want to keep your script (without the Useless Cats) and modify it so that it not only extracts the attachments, but also creates a modified message on standard output with the links to the attachments in-lined.

I can suggest three options, all of which are slightly cumbersome. Suppose your incoming message is a (multipart/mixed (text/plain (multipart/related text/html image/png image/png))); you could

  1. Wrap your message in a (multipart/related (text/plain: your list of links here) (message/rfc822: original message here: (multipart/mixed (text/plain (multipart/related text/html image/png image/png)))). This doesn't require you to parse the original message at all, but is awkward to view in most clients.

  2. Assume there is always a text/plain part, and attach the list of links to its end. This requires logic to find and extend the first text/plain part, and some sort of fallback if there isn't a text/plain part as the first part.

  3. Assume all your messages are multipart/* and just add a text/plain part near the beginning, as the very first part, or immediately after the first text/plain part. This requires the same logic, and might be awkward to view in some clients, but makes fewer assumptions about the incoming message, and so might be safer.

If you can code this up, invoking Procmail on it is quite simple; just omit the h flag from the earlier recipe (which specifies to only filter in the headers).

:0fw
| replace-entire-message-with-a-different-message

Outras dicas

At the moment I have this:

in procmail I do a copy like this:

:0c
| myscript.sh

then in myscript.sh

path="webserver path"

tee _temp >& /dev/null # is there a different way of doing this?

# extract message-id
mid=$(cat _tempmail | formail -c -x message-id - | sed 's/ <\(.*\)@.*>/\1/')

mkdir $path/$mid

# extract all attachments into directory depending on the message id
cat _tempmail | munpack -C $path/$mid

So the only thing I have to do is get the message-id from the mail and create my url.

As suggested by tripleee I could add this url as a header..

It would be great if there is a link in the message, maybe I'll do that some other time. At the moment, I'm fine with this solution.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top