3

We are using Rails 7 to build an application which, amongst other features, should perform some actions when e-mails are sent to one of its e-mail addresses (which have, for instance, the format ticket-{uuid}@ourdomain.com).

Rails' ActionMailbox's routing works fine for direct e-mails. However, when e-mails are forwarded, they are not recognized by ActionMailbox at all.

How can we ensure that forwarded e-mails are also handled and routed correctly with ActionMailbox?


EDIT: A simplified version of the code we are using:

class ApplicationMailbox < ActionMailbox::Base  
  routing /^ticket-(.+)@ourdomain.com$/i => :service_tickets
end
class ServiceTicketsMailbox < ApplicationMailbox
  def process
    puts "processing email: #{mail.inspect}"
    # ... and then we extract its fields
    # and store some of them in the database.
  end
end
Qqwy
  • 5,214
  • 5
  • 42
  • 83
  • 1
    Could you share some code? For example `application_mailbox.rb` and the mailbox file where your e-mail is routed to? – Chris Jan 31 '22 at 10:27
  • @Chris I have added the code snippets you requested – Qqwy Feb 01 '22 at 17:06
  • Don't see anything weird yet. Could you try `routing all: :service_tickets` Maybe the regexp is not working properly. – Chris Feb 02 '22 at 08:03
  • @Chris There is nothing weird. This is very standard code which is very similar to Rails' examples in the guide. 'Direct' emails to the listed address work. Emails which are forwarded do *not* work. – Qqwy Feb 02 '22 at 16:47
  • What do you use for `config.action_mailbox.ingress =` ? Maybe the problem is somewhere else in the chain for example a spam filter? – Chris Feb 03 '22 at 08:28
  • @Chris `config.action_mailbox.ingress = :sendgrid`. – Qqwy Feb 03 '22 at 08:45
  • `routing all: :service_tickets` "works" in the sense that forwarded emails are then passed on to the `ServiceTicketsMailbox`. **However**, how to figure out which ticket it belongs to? We need to extract the UUID from the e-mail address it is sent to to process the e-mail. And besides this, in our actual app, we have multiple mailboxes set up (`ticket-{uuid}`, `project-{uuid}` and some more). Using `routing all:` we would strip out ActionMailbox' own routing + abstractions and we'd have write our own from scratch instead. I don't think this is the right approach. – Qqwy Feb 03 '22 at 08:53
  • Figuring out the ticket is not that hard. In your `process` method you have access to `mail.from` and you can pass it to another object to process the mail based on the e-mail address. I agree it is not as clean as using the routing but since this is not working this would be a workable solution. Another solution is to not put the uuid in the email but in the subject or the body. – Chris Feb 03 '22 at 10:52

2 Answers2

0

Ok I think I found the issue:

When you send a normal e-mail the To header looks like this To: ticket-123@@ourdomain.com and this matches with /^ticket-(.+)@ourdomain.com$/i

However when you forward the e-mail the header looks like this To: John Doe <ticket-123@ourdomain.com> and this will not match with your regex.

Change the regex to /ticket-(.+)@ourdomain.com/i and it should work.

You can try it out on https://regexr.com/

Chris
  • 3,795
  • 3
  • 17
  • 23
-1

The API never includes functionality to see any other mail-address than the latest one in the delivery stack. The only option the API offers by itself is to use InboundEmail::source to get the raw message for further parsing.

After this process I could imagine to use RoutingJob to forward the mail to the correct receiver.

I'm not sure if Callbacks could help.

Also concerning MessageId I don't know if it's possible to extract the correct Ids.

As far as I see the whole challenge requires at least some work and I see there no simple solution.

David
  • 5,882
  • 3
  • 33
  • 44