Ruby on rails 4 Sidekiq+;设计具有多个子域的邮件程序
在我的一个项目中,在运行时更改ActionMailer::Base.default_url_options={:host=>host}时遇到问题 设置: 我有多个子域,它们在后端使用相同的Rails应用程序。我想通过Sidekiq队列向用户发送我的设计邮件。设计邮件(确认、重置密码)包含链接,这些链接需要特定子域才能正确 我的环境Ruby on rails 4 Sidekiq+;设计具有多个子域的邮件程序,ruby-on-rails-4,devise,sidekiq,devise-async,Ruby On Rails 4,Devise,Sidekiq,Devise Async,在我的一个项目中,在运行时更改ActionMailer::Base.default_url_options={:host=>host}时遇到问题 设置: 我有多个子域,它们在后端使用相同的Rails应用程序。我想通过Sidekiq队列向用户发送我的设计邮件。设计邮件(确认、重置密码)包含链接,这些链接需要特定子域才能正确 我的环境 rails (4.2.0) sidekiq (3.3.1) devise (3.4.1) devise-async (0.9.0) 我的应用程序\u控制器中有一个b
rails (4.2.0)
sidekiq (3.3.1)
devise (3.4.1)
devise-async (0.9.0)
我的应用程序\u控制器中有一个before\u操作
class ApplicationController < ActionController::Base
before_action :set_action_mailer_default_url_options
private
def set_action_mailer_default_url_options
host = "my-logic-to-get-the-correct-host"
ActionMailer::Base.default_url_options = {:host => host}
end
end
主机始终在控制器中设置正确。我已经调试过了。似乎主机没有传递给sidekiq。。你知道我该怎么解决这个问题吗
我无法将子域保存在某个地方,因为用户可以触发来自不同子域的电子邮件,但并不总是相同的。我需要一种方法来告诉Desive应该使用哪个主机来发送特定的电子邮件。我可以覆盖设计邮件器并传递主机或类似的东西吗
以下解决方案在我的情况下是不可能的:
顺便说一句:完整的工作流程与设计,设计异步和sidekiq本身的工作(在开发,分期和生产)。只有链接的主机不正确->这是我的大问题:-)…您可以在应用程序控制器中覆盖默认url选项,如下所示:
class ApplicationControllerhost}
结束
结束
或者您可以尝试以下答案:
我最终得到了以下解决方案。此解决方案包含这样的情况:您有多个客户,这些客户可以拥有自己的电子邮件设置以及邮件中URL的自己的域/子域 我使用了自己的mailer类,并连接到客户可能的自定义电子邮件设置中: config/initializers/designe.rb:
config.mailer = "DeviseMailer"
config/secrets.yml:
development:
mailer_settings:
customers:
customer_1_identifier_name:
send:
address: "smtp.gmail.com"
port: 587
domain: "example"
user_name: "test@example.com"
email: "noreply@example.com"
password: "secret"
authentication: "plain" # or "login"
ssl: false # or true
tls: false # or true
enable_starttls_auto: true # or false
app/mailers/designe_mailer.rb:
class DeviseMailer < Devise::Mailer
layout 'mailer'
after_action :set_email_settings_and_sender, only: [:reset_password_instructions, :confirmation_instructions]
def reset_password_instructions(record, token, opts={})
@account = record.current_account if record.class == User # set the account of current_user - current_account is an own helper method
super
end
def confirmation_instructions(record, token, opts={})
@account = record.current_account if record.class == User && record.unconfirmed_email.present?
super
end
private
# re-set the email settings for the given user and his account:
def set_email_settings_and_sender
if @account.present? && @account.custom_email_settings?
mail.reply_to = nil
settings = Rails.application.secrets.mailer_settings["customers"][@account.identifier_name]["send"].symbolize_keys
mail.delivery_method.settings.merge!(settings)
mail.from = "#{@account.display_name} <#{settings[:email]}>"
end
end
end
您好,您找到解决方案了吗?@codingaddited-我在下面添加了我的答案。谢谢您添加解决方案
development:
mailer_settings:
customers:
customer_1_identifier_name:
send:
address: "smtp.gmail.com"
port: 587
domain: "example"
user_name: "test@example.com"
email: "noreply@example.com"
password: "secret"
authentication: "plain" # or "login"
ssl: false # or true
tls: false # or true
enable_starttls_auto: true # or false
class DeviseMailer < Devise::Mailer
layout 'mailer'
after_action :set_email_settings_and_sender, only: [:reset_password_instructions, :confirmation_instructions]
def reset_password_instructions(record, token, opts={})
@account = record.current_account if record.class == User # set the account of current_user - current_account is an own helper method
super
end
def confirmation_instructions(record, token, opts={})
@account = record.current_account if record.class == User && record.unconfirmed_email.present?
super
end
private
# re-set the email settings for the given user and his account:
def set_email_settings_and_sender
if @account.present? && @account.custom_email_settings?
mail.reply_to = nil
settings = Rails.application.secrets.mailer_settings["customers"][@account.identifier_name]["send"].symbolize_keys
mail.delivery_method.settings.merge!(settings)
mail.from = "#{@account.display_name} <#{settings[:email]}>"
end
end
end
<% if @account.present? && @account.domain.present? %>
<% link = "#{@account.host_for_links}/auth/password/edit?reset_password_token=#{@token}" %>
<a href="<%= link %>"><%= link %></a>
<% else %>
<a href="<%= edit_password_url(@resource, reset_password_token: @token) %>"><%= edit_password_url(@resource, reset_password_token: @token) %></a>
<% end %>
config.action_mailer.default_url_options = { host: "your-host.dev", port: 3000 }