Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 6 Action Mailbox and Gmail Integration How To

Rails 6 comes with Action Mailbox now. The documentation and community do not have great resources on how to integrate various services outside of the most common such as SendGrid.

Assuming a person uses Google's Gsuite Gmail:

  1. How could they integrate that with Action Mailbox?

  2. Would one use Gmail's API, or would that not be appropriate for Action Mailbox?

  3. If Gmail doesn't work, what is different about SendGrid that makes it integrate appropriately?

like image 774
karns Avatar asked Jun 22 '20 18:06

karns


People also ask

What is action mailbox in rails?

1 What is Action Mailbox? Action Mailbox routes incoming emails to controller-like mailboxes for processing in Rails. It ships with ingresses for Mailgun, Mandrill, Postmark, and SendGrid. You can also handle inbound mails directly via the built-in Exim, Postfix, and Qmail ingresses.

How to send emails from other controller actions in rails?

Once the Rails mailer and Gmail settings are properly configured, we can easily send other emails from other controller actions by generating and setting up new mailers in much the same way. :) I just wanted to thank you for writing out this detailed tutorial.

How to integrate action mailbox with Mailgun?

Give Action Mailbox your Mailgun Signing key (which you can find under Settings -> Security & Users -> API security in Mailgun), so it can authenticate requests to the Mailgun ingress.

How do I configure action mailbox to accept emails from postmark?

Tell Action Mailbox to accept emails from Postmark: Generate a strong password that Action Mailbox can use to authenticate requests to the Postmark ingress. Use bin/rails credentials:edit to add the password to your application's encrypted credentials under action_mailbox.ingress_password , where Action Mailbox will automatically find it:


2 Answers

Action Mailbox is built around receiving email from a Mail Transfer Agent (MTA) in real time, not periodically fetching email from a mailbox. That is, it receives mail sent via SMTP, it doesn't fetch mail (using IMAP or POP3) from another server that has already received it.

For this to work it is dependent on an external (to Rails) SMTP service receiving the email and then delivering the email to Action Mailbox. These external services are called "Ingresses" and, as at the time of writing, there are 5 available ingresses.

Of the five, four are commercial services that will run the required SMTP servers for you and then "deliver" the email to your application (usually as a JSON payload via a webhook).

  • Mailgun - scroll down to "Inbound"
  • Mandrill
  • Postmark
  • Sendgrid

You could already use those services in a Rails App and handle the webhooks yourself but Action Mailbox builds a standardised set of functionality on top. Almost like a set of rails to guide and speed the process.

In addition, the fifth ingress is the "Relay" ingress. This allows you to run your own supported MTA (SMTP server) on the same machine and for it to relay the received email to Action Mailbox (usually the raw email). The currently supported MTAs are:

  • Exim
  • Postfix
  • Qmail

To answer your specific questions about Gmail:

  1. How could they integrate that with Action Mailbox?

They couldn't directly. They would need to also set up one of the 7 MTAs listed above and then somehow deliver the emails to that. The delivery could be accomplished with:

  • Forwarding rules managed by the user at the mailbox level
  • Dual delivery, split delivery or some other advanced routing rule managed by the admin at the domain level
  1. Would one use Gmail's API, or would that not be appropriate for Action Mailbox?

Even if there were a way to have Gmail fire a webhook on incoming email (I'm not aware of any special delivery options outside the advanced routing rules above), there is currently no way to connect that theoretical webhook to Action Mailbox.

  1. If Gmail doesn't work, what is different about SendGrid that makes it integrate appropriately?

Sendgrid (to use your example, the others work more or less the same way) offers an inbound mail handling API. Just as importantly, the Rails Team has built an incoming email controller to integrate with that API.

Given the lack of Gmail APIs and the lack of a Rails ingress controller, the only way I can think of that you could connect Action Mailbox to an existing Gmail mailbox would be for some other bit of code to check the mailbox, reformat the fetched email and then pose as one of the supported MTAs to deliver it to Action Mailbox.

It would be an interesting exercise and would possibly become a popular gem but it would very much be a kludge. A glorious kludge if done well, but a kludge nonetheless.


Another option would be to leave your example.com domain delivering to Gmail as normal and set up another domain for your Action Mailbox emails. You could use a separate domain, example.org, or a subdomain, app.example.com.

This would involve setting up one of the 7 supported SMTP servers and pointing the MX records for example.org or app.example.com at those servers.


Bonus trivia: Another name for an MTA is a Mail eXchager, hence the name for a DNS mail record is an MX record.

like image 116
Matthew Avatar answered Nov 14 '22 23:11

Matthew


To integrate IMAP with Rails, have a look into the greate mail_room Gem

It's a daemon that you can start alongside your app, which listens onto couple of IMAP inboxes, and then convert those into either a Sidekiq Worker or push it via http to your app.

It's used by Gitlab for their mail interactions (Answer to thread, create issue by writing to an email).

They also have a section on how to integrate with ActionMailbox.

like image 25
stwienert Avatar answered Nov 14 '22 21:11

stwienert