35

我需要在 Rails 应用程序中使用两个不同的 smtp 服务器。看来 ActionMailer 的构造方式,子类不可能有不同的 smtp_settings。每当发送消息时,我都可以重新加载每个邮件程序类的 smtp 设置,但这会弄乱我无法控制的 ExceptionNotifier 插件(除非我也弄乱了它)。有没有人有这样的解决方案/插件?

理想情况下,我想拥有

class UserMailer < ActionMailer::Base; end

然后在 environment.rb 中设置

ActionMailer::Base.smtp_settings = standard_smtp_settings
UserMailer.smtp_settings = user_smtp_settings

因此,我的大多数邮件程序(包括 ExceptionNotifier)都会使用默认设置,但 UserMailer 会使用付费中继服务。

4

11 回答 11

21

基于 Oreilly 文章,我想出了我在这里写的解决方案:http: //transfs.com/devblog/2009/12/03/custom-smtp-settings-for-a-specific-actionmailer-subclass

以下是相关代码:

class MailerWithCustomSmtp < ActionMailer::Base
  SMTP_SETTINGS = {
    :address => "smtp.gmail.com",
    :port => 587,
    :authentication => :plain,
    :user_name => "custom_account@transfs.com",
    :password => 'password',
  }

  def awesome_email(bidder, options={})
     with_custom_smtp_settings do
        subject       'Awesome Email D00D!'
        recipients    'someone@test.com'
        from          'custom_reply_to@transfs.com'
        body          'Hope this works...'
     end
  end

  # Override the deliver! method so that we can reset our custom smtp server settings
  def deliver!(mail = @mail)
    out = super
    reset_smtp_settings if @_temp_smtp_settings
    out
  end

  private

  def with_custom_smtp_settings(&block)
    @_temp_smtp_settings = @@smtp_settings
    @@smtp_settings = SMTP_SETTINGS
    yield
  end

  def reset_smtp_settings
    @@smtp_settings = @_temp_smtp_settings
    @_temp_smtp_settings = nil
  end
end
于 2009-12-05T02:09:32.690 回答
21
class UserMailer < ActionMailer::Base
  def welcome_email(user, company)
    @user = user
    @url  = user_url(@user)
    delivery_options = { user_name: company.smtp_user,
                         password: company.smtp_password,
                         address: company.smtp_host }
    mail(to: @user.email,
         subject: "Please see the Terms and Conditions attached",
         delivery_method_options: delivery_options)
  end
end

Rails 4 允许动态交付选项。上面的代码直接来自action mailer basics guide,你可以在这里找到:http: //guides.rubyonrails.org/v4.0/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options

有了这个,可以为您发送的每封电子邮件设置不同的 smtp 设置,或者像在您的用例中为不同的子类(如 UserMailer、OtherMailer 等)设置不同的 smtp 设置。

于 2016-01-22T13:43:46.410 回答
16

Rails 4.2+ 的解决方案:

配置/secrets.yml

production:
  gmail_smtp:
    :authentication: "plain"
    :address: "smtp.gmail.com"
    :port: 587
    :domain: "zzz.com"
    :user_name: "zzz@zzz.com"
    :password: "zzz"
    :enable_starttls_auto: true
  mandrill_smtp:
    :authentication: "plain"
    :address: "smtp.mandrillapp.com"
    :port: 587
    :domain: "zzz.com"
    :user_name: "zzz@zzz.com"
    :password: "zzz"
    :enable_starttls_auto: true
  mailgun_smtp:
    :authentication: "plain"
    :address: "smtp.mailgun.org"
    :port: 587
    :domain: "zzz.com"
    :user_name: "zzz@zzz.com"
    :password: "zzz"
    :enable_starttls_auto: true

配置/环境/production.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = Rails.application.secrets.gmail_smtp

app/mailers/application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  default from: '"ZZZ" <zzz@zzz.com>'

  private

  def gmail_delivery
    mail.delivery_method.settings = Rails.application.secrets.gmail_smtp
  end

  def mandrill_delivery
    mail.delivery_method.settings = Rails.application.secrets.mandrill_smtp
  end

  def mailgun_delivery
    mail.delivery_method.settings = Rails.application.secrets.mailgun_smtp
  end
end

app/mailers/user_mailer.rb

class UserMailer < ApplicationMailer
  # after_action :gmail_delivery, only: [:notify_user]
  after_action :mandrill_delivery, only: [:newsletter]
  after_action :mailgun_delivery, only: [:newsletter2]

  def newsletter(user_id); '...' end # this will be sent through mandrill smtp
  def newsletter2(user_id); '...' end # this will be sent through mailgun smtp
  def notify_user(user_id); '...' end # this will be sent through gmail smtp
end
于 2016-02-28T22:50:30.170 回答
10

对于使用更高版本(3+)的 Rails 解决此问题的任何人,请尝试此操作

http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options

于 2013-11-29T12:09:14.050 回答
6

尝试在 Rails 3.2.1 中使用 jkrall 的选项,但由于某种原因,它不会覆盖默认配置,而是这样做:

MyMailer.my_email.delivery_method.settings.merge!(SMTP_SETTINGS).deliver

类似于http://www.scottw.com/multiple-smtp-servers-with-action-mailer,让它工作。

于 2012-02-16T13:15:57.700 回答
2

Rails-2.3.*

# app/models/user_mailer.rb
class UserMailer < ActionMailer::Base
  def self.smtp_settings
    USER_MAILER_SMTP_SETTINGS
  end

  def spam(user)
    recipients user.mail
    from 'spam@example.com'
    subject 'Enlarge whatever!'
    body :user => user
    content_type 'text/html'
  end
end

# config/environment.rb
ActionMailer::Base.smtp_settings = standard_smtp_settings
USER_MAILER_SMTP_SETTINGS = user_smtp_settings

# From console or whatever...
UserMailer.deliver_spam(user)
于 2013-03-23T16:49:31.390 回答
0

这是另一种解决方案,虽然它看起来很荒谬,但我认为它更干净,更容易在不同的 AM::Base 类中重用:

    module FTTUtilities
      module ActionMailer
        module ClassMethods
          def smtp_settings
            dict = YAML.load_file(RAILS_ROOT + "/config/custom_mailers.yml")[self.name.underscore]
            @custom_smtp_settings ||= HashWithIndifferentAccess.new(dict)
          end
        end

        module InstanceMethods
          def smtp_settings
            self.class.smtp_settings
          end
        end
      end
    end

邮件程序示例:

    class CustomMailer < ActionMailer::Base
        extend FTTUtilites::ActionMailer::ClassMethods
        include FTTUtilites::ActionMailer::InstanceMethods
    end
于 2011-02-25T23:31:49.207 回答
0

恐怕这在本地是不可行的。
但是你可以通过修改模型中的@@smtp_settings 变量来欺骗它。

有一篇关于 Oreilly 的文章很好地解释了它,即使他们的代码根本不干净。 http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html

于 2009-10-13T14:28:43.313 回答
0

https://github.com/AnthonyCaliendo/action_mailer_callbacks

我发现这个插件很容易帮我解决了这个问题(如 < 5 分钟)。我只需在 before_deliver 中更改特定邮件程序的 @@smtp_settings,然后在 after_deliver 中将其更改回默认值。使用这种方法,我只需要将回调添加到需要不同于默认值的@@smtp_settings 的邮件程序。

class CustomMailer < ActionMailer::Base

  before_deliver do |mail|
    self.smtp_settings = custom_settings
  end

  after_deliver do |mail|
    self.smtp_settings = default_settings
  end

  def some_message
    subject "blah"
    recipients "blah@blah.com"
    from "ruby.ninja@ninjaness.com"
    body "You can haz Ninja rb skillz!"
    attachment some_doc
  end

end
于 2010-12-22T17:16:38.630 回答
0

当我想在控制台(Rails 5)中进行快速测试时,我执行了以下操作:

settings = { username: "" } # etc
mailer = MyMailer.some_method
mailer.delivery_method.settings.merge!(settings)
mailer.deliver
于 2017-01-20T14:57:07.103 回答
0

Rails 3.2 的解决方案:

class SomeMailer < ActionMailer::Base

  include AbstractController::Callbacks
  after_filter :set_delivery_options

  private
  def set_delivery_options
    settings = {
      :address => 'smtp.server',
      :port => 587,
      :domain => 'your_domain',
      :user_name => 'smtp_username',
      :password => 'smtp_password',
      :authentication => 'PLAIN' # or something
    }

    message.delivery_method.settings.merge!(settings)
  end
end

受How to send emails with multiple, dynamic smtp using Actionmailer/Ruby on Rails启发的解决方案

于 2016-06-09T06:01:43.903 回答