Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add instructions to override settings using ActionMailer callbacks #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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