带有多个 SMTP 服务器的 Rails ActionMailer [英] Rails ActionMailer with multiple SMTP servers
问题描述
我需要在 Rails 应用程序中使用两个不同的 smtp 服务器.看来ActionMailer的构造方式,不可能有不同的smtp_settings一个子类.每当发送消息时,我都可以重新加载每个邮件程序类的 smtp 设置,但这会弄乱我无法控制的 ExceptionNotifier 插件(除非我也弄乱了它).有没有人有类似的解决方案/插件这个?
I have a need to use two different smtp servers in a Rails application. It appears that the way ActionMailer is constructed, it is not possible to have different smtp_settings for a subclass. I could reload the smtp settings for each mailer class whenever a message is being sent, but that messes up the ExceptionNotifier plugin which is outside my control (unless I mess with it too). Does anyone have a solution/plugin for something like this?
理想情况下我想拥有
class UserMailer < ActionMailer::Base; end
然后在environment.rb中设置
and then set in environment.rb
ActionMailer::Base.smtp_settings = standard_smtp_settings
UserMailer.smtp_settings = user_smtp_settings
因此,我的大多数邮件程序(包括 ExceptionNotifier)都会使用默认设置,但 UserMailer 会使用付费中继服务.
Thus, most of my mailers including ExceptionNotifier would pickup the default settings, but the UserMailer would use a paid relay service.
推荐答案
基于 Oreilly 的文章,我想出了我在这里写的解决方案:http://transfs.com/devblog/2009/12/03/custom-smtp-settings-for-a-specific-actionmailer-subclass
Based on the Oreilly article, I came up with the solution I wrote about here: 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
这篇关于带有多个 SMTP 服务器的 Rails ActionMailer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!