ivaldi / brimir

Email helpdesk built using Ruby on Rails and Zurb Foundation
http://getbrimir.com
GNU Affero General Public License v3.0
1.38k stars 299 forks source link

Support for Mailgun #436

Closed svoop closed 5 years ago

svoop commented 6 years ago

I'm currently refactoring the way tickets are update by email. The post-mail script will be one way to do things, I'll implement Mailgun as another (since that's what I currently use). Adding more providers is as easy as updating the route constraints and adding one method to the TicketsController.

To secure these hooks a little, a key (derived from the base secret) will be required, which means that for existing users of post-mail, these are breaking changes. The update, however, will be dead simple, only the URL in the MTA's aliases file needs to be changed. Details will follow here as well as in the CHANGELOG and README.

PR should be ready later this week.

svoop commented 6 years ago

Mailgun parses emails and sends them as JSON payload via a webhook. Unfortunately, Brimir heavily relies on parsing a raw email and (since the EML export feature) also stores the raw message which would not be available via Mailgun. Furthermore, refactoring the way post-mail is handled (e.g. by creating an IncomingMailService which uses the Rails mailers to parse) is too big a task for me right now.

I'm therefore taking a simpler, yet less cool approach: When Mailgun receives a mail, it can be stored. The webhook then holds the API URL to fetch the mail which is available as raw MIME body as well. This can be fed into the same pipeline as post-mail. The upsides: Way less code to touch and EML export works. The downsides: The Mailgun API key has to be configured in Brimir's secrets.yml and one more point of failure (the REST call to Mailgun).

svoop commented 6 years ago

Here's the PR: #443

svoop commented 6 years ago

:warning: WARNING :warning:

The URL for the mail hooks had to be changed in order to accomodate other mail providers. This commit thus breaks things for those already using the post-mail script. However, the change needed is no biggie at all and mentioned in the CHANGELOG:

First find out the mail key which protects the mail hook:

rake secret:mail_key

Then update the aliases file of your MTA from the old to the new hook URL (replace {MAIL_KEY} with the output from above):

OLD: http://yoururl.com/tickets.json
NEW: http://yoururl.com/post_mail/{MAIL_KEY}/tickets.json