在测试环境中为 Mandrill 配置 rails 应用程序
Posted
技术标签:
【中文标题】在测试环境中为 Mandrill 配置 rails 应用程序【英文标题】:Configuring rails app for Mandrill in Test environment 【发布时间】:2014-12-25 17:21:32 【问题描述】:我正在开发一个使用 Mandrill 生成其电子邮件的 Rails 应用程序。我一辈子都无法让它在测试环境中工作!我希望能够使用 ActionMailer 方法来传递、阅读和解析电子邮件,但我不断收到:Errno::ECONNREFUSED: Connection refused - connect(2)
message.rb:
class Message < ActionMailer::Base
require 'mandrill'
default from: "My App <messages@myapp.com>"
def send_welcome_email(to_user)
mandrill = Mandrill::API.new ENV['MANDRILL_API_KEY']
template = mandrill.templates.render "welcome-to-myapp", [], [
:name => 'username', :content => to_user.name,
:name => 'subject', :content => "Welcome to MyApp, #to_user.name"
]
@body = template['html']
mail(:subject => "Welcome to MyApp, #to_user.name", :to => to_user.email, "tags" => ["In Trial - Welcome"],)
headers['X-MC-Track'] = "opens"
headers['X-MC-Tags'] ="invite_sent"
end
end
production.rb
# Mailer configurations
config.action_mailer.default_url_options = :protocol => 'https', :host => 'app.myapp.com'
ActionMailer::Base.smtp_settings =
:address => "smtp.mandrillapp.com",
:port => 25, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY'], # SMTP password is any valid API key
:authentication => 'login', # Mandrill supports 'plain' or 'login'
:domain => 'myapp.com', # your domain to identify your server when connecting
test.rb
ActionMailer::Base.smtp_settings =
:address => "smtp.mandrillapp.com",
:port => 25, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY_TEST'], # SMTP password is any valid API key
:authentication => 'login', # Mandrill supports 'plain' or 'login'
:domain => 'myapp.com', # your domain to identify your server when connecting
config.action_mailer.default_url_options = :host => '192.168.11.44:3000'
config.action_mailer.delivery_method = :smtp
【问题讨论】:
您是否在 mandrill api 中添加了 ip 阻止?也许您将其修复到您的生产服务器并且来自其他 ip 的请求被阻止。 【参考方案1】:检查您用来连接 mandrill 的端口。应该是 2525 或 587。
我认为这个问题和你的一样:Can't get Mandrill to send emails from Rails App。
【讨论】:
以上是关于在测试环境中为 Mandrill 配置 rails 应用程序的主要内容,如果未能解决你的问题,请参考以下文章
Mandrill API 使用 Rails 4.2.5 Excon::Errors::SocketError
Mandrill + Rails 不是通过控制器发送而是通过控制台发送
如何在 Ruby on Rails 中为我的开发和生产环境设置不同的 api 密钥?