Skip to content

Commit

Permalink
Merge pull request heartcombo#1282 from apocalyptiq/master
Browse files Browse the repository at this point in the history
Allow to set default 'from' header of Devise mails in custom mailer class
  • Loading branch information
josevalim committed Aug 20, 2011
2 parents b9413ab + 7665cbf commit 0f3cef4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/devise/mailers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def headers_for(action)
end

def mailer_sender(mapping)
if Devise.mailer_sender.is_a?(Proc)
if default_params[:from].present?
default_params[:from]
elsif Devise.mailer_sender.is_a?(Proc)
Devise.mailer_sender.call(mapping.name)
else
Devise.mailer_sender
Expand Down Expand Up @@ -86,4 +88,4 @@ def translate(mapping, key)
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/generators/templates/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# four configuration values can also be set straight in your models.
Devise.setup do |config|
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in DeviseMailer.
# 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.
config.mailer_sender = "[email protected]"

# Configure the class responsible to send e-mails.
Expand Down
6 changes: 6 additions & 0 deletions test/mailers/confirmation_instructions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase

def setup
setup_mailer
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = '[email protected]'
end

Expand Down Expand Up @@ -35,6 +36,11 @@ def mail
assert_equal ['[email protected]'], mail.from
end

test 'setup sender from custom mailer defaults' do
Devise.mailer = 'Users::Mailer'
assert_equal ['[email protected]'], mail.from
end

test 'setup reply to as copy from sender' do
assert_equal ['[email protected]'], mail.reply_to
end
Expand Down
6 changes: 6 additions & 0 deletions test/mailers/reset_password_instructions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ResetPasswordInstructionsTest < ActionMailer::TestCase

def setup
setup_mailer
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = '[email protected]'
end

Expand Down Expand Up @@ -38,6 +39,11 @@ def mail
assert_equal ['[email protected]'], mail.from
end

test 'setup sender from custom mailer defaults' do
Devise.mailer = 'Users::Mailer'
assert_equal ['[email protected]'], mail.from
end

test 'setup reply to as copy from sender' do
assert_equal ['[email protected]'], mail.reply_to
end
Expand Down
6 changes: 6 additions & 0 deletions test/mailers/unlock_instructions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class UnlockInstructionsTest < ActionMailer::TestCase

def setup
setup_mailer
Devise.mailer = 'Devise::Mailer'
Devise.mailer_sender = '[email protected]'
end

Expand Down Expand Up @@ -38,6 +39,11 @@ def mail
assert_equal ['[email protected]'], mail.from
end

test 'setup sender from custom mailer defaults' do
Devise.mailer = 'Users::Mailer'
assert_equal ['[email protected]'], mail.from
end

test 'setup reply to as copy from sender' do
assert_equal ['[email protected]'], mail.reply_to
end
Expand Down
3 changes: 3 additions & 0 deletions test/rails_app/app/mailers/users/mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Users::Mailer < Devise::Mailer
default :from => '[email protected]'
end
3 changes: 2 additions & 1 deletion test/rails_app/config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# four configuration values can also be set straight in your models.
Devise.setup do |config|
# ==> Mailer Configuration
# Configure the e-mail address which will be shown in DeviseMailer.
# 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.
config.mailer_sender = "[email protected]"

# Configure the class responsible to send e-mails.
Expand Down

0 comments on commit 0f3cef4

Please sign in to comment.