A mail_processor plugin has been added for processing mail using a routing tree. Quick example:
class MailProcessor < Roda plugin :mail_processor route do |r| # Match based on the To header, extracting the ticket_id r.to /ticket\+(\d+)@example.com/ do |ticket_id| if ticket = Ticket[ticket_id.to_i] # Mark the mail as handled if there is a valid ticket # associated r.handle do ticket.add_note(text: mail_text, from: from) end end end end end
You can submit mail for processing by calling the process_mail method with a Mail instance:
The mail_processor routing tree uses routing methods specific to mail:
match on the mail From address
match on the mail To address
match on the mail CC address
match on the mail recipients (To and CC addresses by default)
match on the mail subject
match on the mail body
match on text extracted from the message (same as mail body by default)
match on a mail header
To mark a mail as having been handled, you call the r.handle method with a block, or one of the above methods prefixed by handle_ (e.g. r.handle_text).
The mail_processor plugin supports hooks that are called for handled mail, unhandled mail, and all mail (for archiving). It also supports the ability to configure how reply text is parsed out of mail, who to consider as the recipients of the email, and the ability to have separate routing blocks per recipient email address (with O(1) delegation to the appropriate block if the recipient addresses to match is a string).