Search code examples
pop3tracticket-system

Open ticket via email POP3 in Trac


I'm searching for a way to let people open Trac ticket by email.

The only solution I've found so far is email2trac | https://oss.trac.surfsara.nl/email2trac/wiki The problem with this solution is that I don't want to install & setup a mailserver. I would like a less invasive solution.

I was thinking about a cron script that download messages from a POP3 account and open/update tickets by parsing the content.

Is this possible ?


Solution

  • I was thinking about a cron script that download messages from a POP3 account and open/update tickets by parsing the content. Is this possible ?

    I think it would be possible yes. Certainly once you had the data from a POP3 account, you could iterate over it and create/update tickets as appropriate with the Trac API.

    For the data retrieval step, you could create a new plugin, with a Component which implements the IAdminCommandProvider interface. How you actually retrieve and parse the data is an implementation detail for you to decide, but you could probably use the email/poplib modules and follow some of the parsing structure from email2trac.

    For some untested boilerplate to get you started...

    from trac.admin import IAdminCommandProvider
    from trac.core import Component, implements
    from trac.ticket import Ticket
    
    def EmailToTicket(Component):
        implements(IAdminCommandProvider)
    
        def get_admin_commands(self):
            yield ('emailtoticket retrieve',
                   'Retrieve emails from a mail server.'
                   None, self._do_retrieve_email)
    
        def _do_retrieve_email(self):
            # TODO - log into the mail server, then parse data.
            # It would be nice to have a tuple of dictionaries, 
            # with keys like id, summary, description etc
    
            # iterate over the data and create/update tickets
            for email in emails:
                if 'id' in email: # assuming email is a dictionary
                    self._update_ticket(email)
                else:
                    self._create_ticket(email)
    
        def _update_ticket(self, data):
            ticket = Ticket(self.env, data[id])
            for field, value in data.iteritems():
                ticket[field] = value
            ticket.save_changes(author, comment, when)
    
        def _create_ticket(self, data):
            ticket = Ticket(self.env)
            for field, value in data.iteritems():
                ticket[field] = value
            ticket.insert()
    

    You could then have Cron tab execute this command via TracAdmin (the frequency is up to you - the below example runs every minute)

    * * * * * trac-admin /path/to/projenv emailtoticket retrieve
    

    The find out more about plugin development, you should read this Trac wiki page.