我正在使用mailgun-ruby在我的rails应用中发送电子邮件。
我已经在我的Mailgun帐户上激活了许多域,并且我希望选择一个特定的域来发送电子邮件来发送每个ActionMailer。
gem的文档仅解释了设置mailgun_settings的全局方式:
config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
api_key: 'api-myapikey',
domain: 'mydomain.com'
}
有任何建议如何根据ActionMailer进行此操作吗?
答案 0 :(得分:0)
我想出了一个自制设计:
class ApplicationMailer < ActionMailer::Base
before_action do
@layout = 'mailer'
@from = 'default@domain.com'
@to = 'default_to@domain.com'
@subject = 'Default Subject'
@domain = 'default.domain.com'
@params = {}
end
after_action do
ac = ActionController::Base.new()
mg = Mailgun::Client.new 'key-xxxxxxxxx'
message_params = {
from: @from,
to: @to,
subject: @subject,
text: ac.render_to_string("#{self.class.to_s.underscore}/#{action_name}.text", layout: @layout, locals: @params),
html: ac.render_to_string("#{self.class.to_s.underscore}/#{action_name}.html", layout: @layout, locals: @params)
}
mg.send_message @domain, message_params
end
end
class UserMailer < ApplicationMailer
before_action do
@from = 'Domain <updates@domain.com>'
@domain = 'updates.domain.com'
end
def test
@to = "yourself@gmail.com"
@subject = "Test"
@params = {}
end
end
自定义user_mailer/test.txt.erb
和user_mailer/test.html.erb
并发送电子邮件:
UserMailer.test