[MlMt] automatically handle attachments

Benny Kjær Nielsen mailinglist at freron.com
Fri Nov 29 17:51:51 EST 2013


Hi,

This message contains documentation by example for an experimental 2.0 
feature. It may or may not work in future revisions of MailMate, but 
it's a good example of the flexibility of combining smart mailboxes, 
rules, and commands. It's like playing with Legos.

It also happens to be a rare example of how the default mailbox named 
“All Messages ▸ All Body Parts” can be used.

On 29 Nov 2013, at 21:05, Thomas Koch wrote:

> I have the wish to handle some mail attachments automatically, at 
> least save them at a certain directory. The the processing of the 
> resulting file can be performed by Hazel, so no problem here.

Interesting problem (and a challenge).

> But I did not find a combination to set up a rule, a mailbox, an 
> action or whatever concept would be the right one (I am new to 
> MailMate, but love it) to get some mails selected based on the usual 
> filter inputs and get the attachments stored in a directory which is 
> monitored by Hazel. I was not able to dig up something from the 
> manual, mailing list history or tracker that gives me a hint.

I didn't even think this was possible myself at first since there is no 
“save” action for rules yet.

> Or is there a directory that would be save to be monitored by Hazel in 
> the MailMate directory trees? I imagine then a script taking a copy of 
> the attachment and perform other actions, but only once per 
> attachment/file that apply to the filter rules.

No, such a location does not exist.

You are at the frontier of what has been done with MailMate here, but 
the following should work. None of this has been documented before.

First, I assume you have a mailbox of incoming messages. We'll name it 
Incoming. Now, create a smart mailbox as follows:

	Mailboxes: “All Messages” ▸ “All Body Parts”

	Condition: “Root Body Part” is in “Incoming” “Body Part Id”

This smart mailbox is going to contain any body parts in any messages in 
the “Incoming” mailbox. Next you can create rules for this smart 
mailbox. For example:

	Condition: “Content-Type ▸ Type” is “image”
	Action: “Run Script” “My Saver”

The last piece of the puzzle is the script. You need a bundle for that 
and those are currently non-trivial to create. You should find an 
existing one and replace all the UUID values with new values. All you 
need is a single file in the “Commands” folder which should have 
content similar to this:

	{
		name    = 'My Saver';
		input   = 'decoded';
		script  = '#!/bin/bash\ncat >> /tmp/image.png\n';
		uuid = '88A186D7-1452-4857-941D-DA96E612F835';
	}

This command tells MailMate to provide a decoded body part (binary image 
data in this case) to the script. The script simply pipes the input to a 
temporary file.

This should be improved to use a non-hardcoded filename. It could be 
based on message headers, but this can be non-trivial and I think it 
would be better if MailMate provided a useful filename (based on the 
headers). I'll put that on the ToDo. Even better, MailMate should (also) 
provide a simple “Save” action with a folder location argument to 
avoid creating a command (and bundle) at all. This could also ensure a 
unique filename. Until that is available then you could do the following 
in the command given above:

	environment   = 
'MY_FILENAME=${content-disposition.parameter.split.filename:${content-type.parameter.split.name}}\n';
	script  = '#!/bin/bash\ncat >> "/tmp/${MY_FILENAME}"\n';

That'll probably work well most of the time.

You can test it by dragging a message with an image attachment to the 
“Incoming” mailbox. You can also apply the command to any message by 
selecting it in the Command menu.

Have a nice weekend everybody. I have little time until Monday morning. 
Be patient if you have any questions.

-- 
Benny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freron.com/pipermail/mailmate/attachments/20131129/022242b1/attachment.html>


More information about the mailmate mailing list