Skip to content

Commit

Permalink
Merge pull request #3684 from consul/dynamic_from_mailer
Browse files Browse the repository at this point in the history
Evaluate mailer from address at runtime
  • Loading branch information
javierm committed Sep 10, 2019
2 parents f18ed2c + 66da02f commit e25d0eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ApplicationMailer < ActionMailer::Base
helper :settings
helper :application
default from: "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>"
default from: Proc.new { "#{Setting["mailer_from_name"]} <#{Setting["mailer_from_address"]}>" }
layout "mailer"
end
6 changes: 1 addition & 5 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
if Rails.env.test? || !ActiveRecord::Base.connection.data_source_exists?("settings")
config.mailer_sender = "noreply@consul.dev"
else
config.mailer_sender = "'#{Setting["mailer_from_name"]}' <#{Setting["mailer_from_address"]}>"
end
config.mailer_sender = Proc.new { "'#{Setting["mailer_from_name"]}' <#{Setting["mailer_from_address"]}>" }

# Configure the class responsible to send e-mails.
config.mailer = "DeviseMailer"
Expand Down
9 changes: 9 additions & 0 deletions spec/mailers/devise_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@

expect(email.subject).to include("confirmación")
end

it "reads the from address at runtime" do
Setting["mailer_from_name"] = "New organization"
Setting["mailer_from_address"] = "new@consul.dev"

email = DeviseMailer.confirmation_instructions(create(:user), "ABC")

expect(email).to deliver_from "'New organization' <new@consul.dev>"
end
end
end
9 changes: 9 additions & 0 deletions spec/mailers/mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@

expect(email.subject).to include("comentado")
end

it "reads the from address at runtime" do
Setting["mailer_from_name"] = "New organization"
Setting["mailer_from_address"] = "new@consul.dev"

email = Mailer.comment(create(:comment))

expect(email).to deliver_from "New organization <new@consul.dev>"
end
end
end

0 comments on commit e25d0eb

Please sign in to comment.