Re: Check for attachment in maildrop recipe
Milan Obuch <[email protected]>
| Newsgroups | gmane.mail.maildrop |
|---|---|
| Message-ID | <[email protected]> |
On Thu, 06 Jun 2013 07:17:31 -0400 Sam Varshavchik <[email protected]> wrote: > Milan Obuch writes: > > > Hi, > > > > I use pythonfiler framework for various tasks in email processing, > > and currently I need to transform use of attachments filter to > > conditional delivery instruction in maildrop recipe. Basically I > > need something like > > > > if (attachment_name =~ /<some regexp>/) > > to '<foldername>' > > > > The reason is attachment filter is global per server setting and I > > need to apply this condition on an per mailbox basis. > > > > Has anybody any idea how to accomplish this task? Alternatively, is > > anybody using any python script in maildrop recipe? It should not be > > There are several issues here. First, is that the attachment name can > be formed in a couple of different ways, and different mail clients > have slightly different rules of what's the name of the attachment. > Most will look at the "filename" attribute of the > Content-Disposition: header. Some may also look at Content-Name: > first, others will look at Content- Description:, and override > everything else with what they find in there. I took logic from attachments.py filter and created standalone check as follows. Maybe it is not complete, but works. Message is read on stdin, some diagnostic is on stderr and exit code is 1 when there is an attachments with filename with banned extension. Any comments/additions welcome. import sys import re import email.Utils blockedPattern = re.compile(r'^.*\.(scr|exe|com|bat|pif|lnk|sys|mid|vb|js|ws|shs|ceo|cmd|cpl|hta|vbs)$',re.I) if __name__ == '__main__': try: msg = email.message_from_file(sys.stdin) except Exception, e: sys.stderr.write("exception " + str(e)) sys.exit(0) for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue filename = part.get_filename() if not filename: rawname = part.get_param('name') try: filename = email.Utils.collapse_rfc2231_value(rawname) except: pass if filename and blockedPattern.match(filename): sys.stderr.write("The extension of the attached file is blacklisted\n") sys.exit(1) > Secondly, maildrop does not really have sufficient knowledge of MIME > to be able to manipulate those headers easily. That's why I created external script to check for given criteria. With attachments.py in $HOME directory, check in maildrop recipe is simple: `$HOME/attachments.py` if ($RETURNCODE) echo R1 else echo R0 In real usage, echo will be replaced with deliveru instruction, notification, etc. This is just to test and log result of the test. > As far as running python goes, maildrop can run an external program > using the backticks or the "cc" operator, and make the contents of > the email message available on standard input. There are some > important caveats, see the maildropfilter man page for more info. I used some perl scripts and some binaries in maildrop already, so both backticks and 'to'/'cc' usage is known to me. I just would like to hear any practical experience of python usage in maildrop recipe. Regards, Milan ------------------------------------------------------------------------------ How ServiceNow helps IT people transform IT departments: 1. A cloud service to automate IT design, transition and operations 2. Dashboards that offer high-level views of enterprise services 3. A single system of record for all IT processes http://p.sf.net/sfu/servicenow-d2d-j