| 1 |
#!/usr/bin/python |
|---|
| 2 |
""" |
|---|
| 3 |
emailfilter.py -- Email tickets to Trac. |
|---|
| 4 |
|
|---|
| 5 |
A simple MTA filter to create Trac tickets from inbound emails. |
|---|
| 6 |
|
|---|
| 7 |
Copyright 2005, Daniel Lundin <daniel@edgewall.com> |
|---|
| 8 |
Copyright 2005, Edgewall Software |
|---|
| 9 |
|
|---|
| 10 |
The scripts reads emails from stdin and inserts directly into a Trac database. |
|---|
| 11 |
MIME headers are mapped as follows: |
|---|
| 12 |
|
|---|
| 13 |
* From: => Reporter |
|---|
| 14 |
* Subject: => Summary |
|---|
| 15 |
* Body => Description |
|---|
| 16 |
|
|---|
| 17 |
How to use |
|---|
| 18 |
---------- |
|---|
| 19 |
* Set TRAC_ENV_PATH to the path of your project's Trac environment |
|---|
| 20 |
* Configure script as a mail (pipe) filter with your MTA |
|---|
| 21 |
typically, this involves adding a line like this to /etc/aliases: |
|---|
| 22 |
somename: |/path/to/email2trac.py |
|---|
| 23 |
Check your MTA's documentation for specifics. |
|---|
| 24 |
|
|---|
| 25 |
Todo |
|---|
| 26 |
---- |
|---|
| 27 |
* Configure target database through env variable? |
|---|
| 28 |
* Handle/discard HTML parts |
|---|
| 29 |
* Attachment support |
|---|
| 30 |
""" |
|---|
| 31 |
|
|---|
| 32 |
TRAC_ENV_PATH = '/var/trac/test' |
|---|
| 33 |
|
|---|
| 34 |
import email |
|---|
| 35 |
import sys |
|---|
| 36 |
|
|---|
| 37 |
from trac.env import Environment |
|---|
| 38 |
from trac.ticket import Ticket |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
class TicketEmailParser(object): |
|---|
| 42 |
|
|---|
| 43 |
env = None |
|---|
| 44 |
|
|---|
| 45 |
def __init__(self, env): |
|---|
| 46 |
self.env = env |
|---|
| 47 |
|
|---|
| 48 |
def parse(self, fp): |
|---|
| 49 |
msg = email.message_from_file(fp) |
|---|
| 50 |
tkt = Ticket(self.env) |
|---|
| 51 |
tkt['status'] = 'new' |
|---|
| 52 |
tkt['reporter'] = msg['from'] |
|---|
| 53 |
tkt['summary'] = msg['subject'] |
|---|
| 54 |
for part in msg.walk(): |
|---|
| 55 |
if part.get_content_type() == 'text/plain': |
|---|
| 56 |
tkt['description'] = part.get_payload(decode=1).strip() |
|---|
| 57 |
|
|---|
| 58 |
if tkt.values.get('description'): |
|---|
| 59 |
tkt.insert() |
|---|
| 60 |
|
|---|
| 61 |
if __name__ == '__main__': |
|---|
| 62 |
env = Environment(TRAC_ENV_PATH, create=0) |
|---|
| 63 |
tktparser = TicketEmailParser(env) |
|---|
| 64 |
tktparser.parse(sys.stdin) |
|---|