35

Rails アプリケーションで 2 つの異なる 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 に答える
16

Rails 4.2 以降のソリューション:

config/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

config/environments/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

Railsの新しいバージョン(3+)でこの問題に取り組んでいる人は、これを試してください

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

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

これはばかげているように見えますが、別の 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

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 email with multiple, dynamic smtp using Actionmailer/Ruby on Rails に触発されたソリューション

于 2016-06-09T06:01:43.903 に答える
0

ネイティブでは無理だと思います。
しかし、モデルの @@smtp_settings 変数を変更することで、少しだますことができます。

Oreilly に関する記事では、コードがまったくきれいではありませんが、かなりよく説明されています。 http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html

于 2009-10-13T14:28:43.313 に答える