Getting started with Midflight in Rails
Welcome to Midflight's Ruby on Rails integration guide. By following the steps below, you'll be ready to send your emails with Midflight in no time. This guide assumes that you are familiar with Rails and its ActionMailer.
1. Before we start
Make sure you have your API key provided by Midflight. You can find it in the onboarding page of your project or in the settings page.
2. Update your ApplicationMailer
Integrating Midflight with your Rails app is straightforward. Here's an example snippet to add to your ApplicationMailer
class:
class ApplicationMailer < ActionMailer::Base
# Rails >= 7.1
after_deliver :send_copy_to_midflight
# Rails < 7.1
# after_action :send_copy_to_midflight
private
def send_copy_to_midflight
return unless Rails.env.production?
url = URI.parse("https://mailbox.midflight.io/YOUR_API_KEY/ingress")
data = mail.to_s
headers = {
"content-type" => "message/rfc822",
"X-Midflight-Template" => "#{mailer_name}##{action_name}",
"X-Midflight-Locale" => locale.to_s
}
Net::HTTP.post(url, data, headers)
end
end
Detailed explanation:
https://mailbox.midflight.io/YOUR_API_KEY/ingress
: This is your unique ingress URL. Don't forget to replace the API key with the one provided by Midflight.
headers["X-Midflight-Template"]
: Set this to a unique identifier for each mail template, constructed from the mailer's class name and the specific action.
headers["X-Midflight-Locale"]
: This should be set to the current locale of the email, allowing for proper localization tracking within Midflight.
3. Test your integration
After setting up the code, send a test email to verify that Midflight is capturing it. You should see the email appear on your Midflight dashboard.
Troubleshooting
If your emails aren't appearing in Midflight:
- Double-check your API key.
- Make sure you're setting the headers correctly in every request.
- Review your environment settings to ensure emails are being sent.
If the styles are broken and you're using Premailer to inline CSS, you're probably sending the copy to Midflight before the styles are applied.
For further assistance, reach out to us at support@midflight.io.