Ruby on Rails集成Mailjet遇问题:未定义投递方法及SSL错误
Hey there! Let's work through your Mailjet integration problems step by step — we'll tackle the two main errors you're seeing one at a time.
1. Resolving "undefined delivery method :mailjet"
This error means Rails doesn't recognize :mailjet as a valid way to send emails. Here's how to fix it:
Verify the Mailjet gem is installed and loaded
Make sure yourGemfileincludes:gem 'mailjet'Run
bundle installto install it, then restart your Rails server — gems don't load into running servers automatically.Check your initializer file
Yourconfig/initializers/mailjet.rbneeds to not only set your API credentials but also register the delivery method with Rails. Add this critical line if it's missing:Mailjet.configure do |config| config.api_key = ENV['MAILJET_API_KEY'] # Use environment variables for security config.secret_key = ENV['MAILJET_SECRET_KEY'] config.default_from = 'your-valid-email@example.com' # Optional but recommended end # Register the Mailjet delivery method with Rails ActionMailer::Base.add_delivery_method :mailjet, Mailjet::Rails::DeliveryMethodConfirm your development.rb configuration
Double-checkconfig/environments/development.rbhas these settings:config.action_mailer.delivery_method = :mailjet # Optional: If you didn't set credentials in the initializer, add them here config.action_mailer.mailjet_settings = { api_key: ENV['MAILJET_API_KEY'], secret_key: ENV['MAILJET_SECRET_KEY'] }
2. Fixing OpenSSL::SSL::SSLError (hostname "localhost" does not match...)
This SSL error usually happens when your app is trying to send an HTTPS request to localhost instead of Mailjet's official API endpoint. Try these fixes:
Ensure the Mailjet API endpoint is correctly set
Add this line to yourmailjet.rbinitializer to explicitly point to Mailjet's server (prevents accidental overrides):Mailjet.configure do |config| # ... existing config ... config.api_endpoint = 'https://api.mailjet.com/v3.1/' endCheck for unintended proxy or localhost routing
- If you're on a corporate network, it might be redirecting Mailjet API requests to
localhost. Verify your network settings or ask your IT team to allow traffic toapi.mailjet.com. - Make sure you haven't added any custom HTTP proxy configurations in your Rails app that could route requests locally.
- If you're on a corporate network, it might be redirecting Mailjet API requests to
Test with direct network access
Try sending an email from a non-corporate network (like your home Wi-Fi) to rule out proxy interference. If it works there, the issue is likely network-related.
Once you've applied these changes, restart your server and test sending an email again. Most of the time, the delivery method error is the root cause, so fix that first before troubleshooting the SSL issue.
内容的提问来源于stack exchange,提问作者prabhat

