3

I have configured Redmine email integration and while it's awesome, a major annoyance is that people have signatures that include a company logo, which then gets posted to every ticket they update via email. I know this is not a perfect solution but I would like to pipe to a script that deletes attachments named "image001.png" from the message so I can then pipe it along to the handler. Are there tools available to help with this, or do I have to start from scratch?

Before: alias > mailhandler.rb

After: alias > parser.script > mailhandler.rb

tacos_tacos_tacos
  • 3,220
  • 16
  • 58
  • 97

2 Answers2

3

You may use MIMEDefang milter as an addon to postfix (or sendmail).

http://www.roaringpenguin.com/products/mimedefang

MIMEDefang can inspect and modify e-mail messages as they pass through your mail relay. MIMEDefang is written in Perl, and its filter actions are expressed in Perl, so it's highly flexible. Here are some things that you can do very easily with MIMEDefang:
* Delete or alter attachments based on file name, contents, results of a virus scan, attachment size, etc.
* Replace large attachments with links to a centrally-stored copy to ease the burden on POP3 users with slow modem links.
[...]

http://www.mimedefang.org/

MIMEDefang is free software: It's released under the terms of the GNU General Public License. It runs under Linux, FreeBSD, Solaris and most other UNIX or UNIX-like systems.

AnFi
  • 5,883
  • 1
  • 12
  • 26
2

I'd personally go for the MIMEDefang option suggested by Andrzej A. Filip but I wondered how I'd write this in a python script and came up with the following solution. In case MIMEDefang is not an option for your environment you might want to try it. No guarantees, only tested with a few sample messages

#!/usr/bin/python
import email
import sys

def remove_attachment(rootmsg,attachment_name):
    """return message source without the first occurence of the attachment named <attachment_name> or None if the attachment was not found"""
    for msgrep in rootmsg.walk():
        if msgrep.is_multipart():
            payload=msgrep.get_payload()
            indexcounter=0
            for attachment in payload:
                att_name = attachment.get_filename(None)
                if att_name==attachment_name:
                    del payload[indexcounter]
                    return rootmsg.as_string()
                indexcounter+=1

if __name__=='__main__':
    incontent=sys.stdin.read()
    try:
        rootmsg=email.message_from_string(incontent)
    except:
        sys.stderr.write("Message could not be parsed")
        sys.exit(1)
    src=remove_attachment(rootmsg,'image001.png')

    if src!=None:
        sys.stdout.write(src)
    else:
        sys.stdout.write(incontent)
Gryphius
  • 2,710
  • 1
  • 18
  • 19