轨的ActionMailer多的SMTP服务器(Rails ActionMailer with mu

2019-07-23 01:09发布

我有一个需要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会使用付费的中继服务。

Answer 1:

基于该奥赖利文章中,我提出了我这里写的解决方案: 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


Answer 2:

解决方案为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

应用程序/邮寄/ 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

应用程序/邮寄/ 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


Answer 3:

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

轨道4允许动态递送选项。 上面的代码是直接从行动邮件基础知识手册,你可以在这里找到: http://guides.rubyonrails.org/v4.0/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options

有了这个,可以有不同的SMTP设置为您发送的每一封电子邮件,或者像在你的使用情况进行不同的子类,如UserMailer,OtherMailer等。



Answer 4:

对于任何人接近与后来的Rails的版本(3+)这个问题,试试这个

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



Answer 5:

试图用jkrall的选择使用Rails 3.2.1,但由于某种原因,它不会覆盖默认配置,但这样做的:

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

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



Answer 6:

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)


Answer 7:

恐怕这不是可行的本身。
但是你可以在模型中修改@@ smtp_settings变量欺骗了一点。

有一个关于奥赖利这也解释了它相当不错,即使他们的代码是不是干净,在所有的文章。 http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html



Answer 8:

https://github.com/AnthonyCaliendo/action_mailer_callbacks

我发现这个插件帮助解决这个问题对我来说很容易(如<5分钟)。 我简单地改变@@ smtp_settings在before_deliver特定的邮件,然后将其改回在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


Answer 9:

这里是另一种解决方案,这虽然看起来很可笑,我认为这是一个有点清洁和更容易在不同的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


Answer 10:

解决方案为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

解决方案的灵感来自于如何发送电子邮件使用on Rails的的ActionMailer / Ruby的多,动态SMTP



Answer 11:

当我想在控制台快速测试(Rails的5)我做了以下内容:

settings = { username: "" } # etc
mailer = MyMailer.some_method
mailer.delivery_method.settings.merge!(settings)
mailer.deliver


文章来源: Rails ActionMailer with multiple SMTP servers