Skip to content

Instantly share code, notes, and snippets.

@leastbad
Last active April 26, 2023 04:30
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save leastbad/aaf30684858f296563d520732b036c14 to your computer and use it in GitHub Desktop.
Save leastbad/aaf30684858f296563d520732b036c14 to your computer and use it in GitHub Desktop.
Action Mailbox: The Missing Manual

This is all you really need to know in order to make Action Mailbox work in development.

  1. Fire up ngrok http 3000 and make note of your subdomain for steps 3 and 8.
  2. Create a Mailgun account because they offer sandbox addresses; grab your domain from the Dashboard.
  3. Go into Receiving and create a catch-all route pointing to: https://XXX.ngrok.io/rails/action_mailbox/mailgun/inbound_emails/mime
  4. Add your Mailgun API key to your credentials:
action_mailbox:
  mailgun_api_key: API KEY HERE
  1. rails action_mailbox:install
  2. rake db:migrate
  3. rails generate mailbox incoming
  4. Add the following to your config/environments/development.rb
  config.action_mailbox.ingress = :mailgun
  config.hosts << "XXX.ngrok.io"
  1. Make sure your config/sidekiq.yml is monitoring the action_mailbox_routing queue.
  2. Configure your application_mailbox.rb with whatever routing you need; example below.
  3. Configure your incoming_mailbox.rb as you wish; example below.

You should now be able to send an email to test@MAILGUN-SANDBOX-DOMAIN.

class ApplicationMailbox < ActionMailbox::Base
# routing /^info@/i => :incoming
routing all: :incoming
end
class IncomingMailbox < ApplicationMailbox
# before_processing
# around_processing
# after_processing
# before_processing do
# unless User.exist?(email: mail.from)
# bounce_with Mailer.user_not_found(mail)
# end
# end
def process
# puts mail.methods - Object.methods
# mail.content, mail.subject, mail.has_attachments?
puts mail.inspect
end
end
@stuarthub
Copy link

One small change needed here..
You shouldn't call #deliver_later on the ActionMailer message when passing it into bounce_with because #deliver_later gets called on the message internally.

Here's the source from ActionMailbox::Base#bounce_with

# Enqueues the given +message+ for delivery and changes the inbound email's status to +:bounced+.
    def bounce_with(message)
      inbound_email.bounced!
      message.deliver_later
    end

@leastbad
Copy link
Author

leastbad commented Feb 8, 2020

@stuarthub thanks so much! I'll update right now.

@lengnuanzizhi-Lee
Copy link

Thank you very much, I will take everyone's suggestions seriously

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment