Skip to content

Commit

Permalink
add instructions to override settings using ActionMailer callbacks
Browse files Browse the repository at this point in the history
this commit also includes an update of .gitignore file for IntelliJ IDEs users
  • Loading branch information
Jean-Michel Gigault committed Jan 10, 2021
1 parent 9ea6fb5 commit 7f276fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ tmtags
\#*
.\#*


## VIM
*.swp

## INTELLIJ
.idea

## PROJECT::GENERAL
tags
coverage
Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,43 @@ class AwesomeMailer < ApplicationMailer
end
```

You also can override default settings using [ActionMailer after callback](https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-callbacks):

```ruby
class ApplicationMailer < ActionMailer::Base
before_action :set_user
after_action :override_mailjet_defaults
def awesome_mail
mail(to: @user.email)
end
private
def set_user
@user = params[:user]
end
def override_mailjet_defaults
mailjet_api_key = params[:mailjet_api_key].presence
mailjet_secret_key = params[:mailjet_secret_key].presence
unless mailjet_api_key.nil? || mailjet_secret_key.nil?
mail.delivery_method.settings.merge!(
user_name: mailjet_api_key,
password: mailjet_secret_key
)
end
end
end
# using default settings from initializer
ApplicationMailer.with(user: user).awesome_email.deliver_now
# temporary using other settings
ApplicationMailer.with(user: user, mailjet_api_key: 'api_key', mailjet_secret_key: 'secret_key').awesome_email.deliver_now
```

Keep in mind that to use the latest version of the Send API, you need to specify the version via `delivery_method_options`:

```ruby
Expand Down

0 comments on commit 7f276fc

Please sign in to comment.