gpt4 book ai didi

ruby-on-rails - 具有多个SMTP服务器的Rails ActionMailer

转载 作者:行者123 更新时间:2023-12-03 08:53:25 27 4
gpt4 key购买 nike

我需要在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将使用付费中继服务。

最佳答案

根据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

关于ruby-on-rails - 具有多个SMTP服务器的Rails ActionMailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1559879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com