aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/action_mailer_basics.md
diff options
context:
space:
mode:
authorCristian Bica <cristian.bica@gmail.com>2014-08-20 15:34:37 +0300
committerCristian Bica <cristian.bica@gmail.com>2014-08-20 17:48:34 +0300
commit9e7f4a94caa9019b2ef0295ad242fbed0acf8bb6 (patch)
treec12dd82460b3d8e7b351effe4afd3b8f0e5f4cf8 /guides/source/action_mailer_basics.md
parentf4ee114746ddc68db606f63e17e6de28274fc2bd (diff)
downloadrails-9e7f4a94caa9019b2ef0295ad242fbed0acf8bb6.tar.gz
rails-9e7f4a94caa9019b2ef0295ad242fbed0acf8bb6.tar.bz2
rails-9e7f4a94caa9019b2ef0295ad242fbed0acf8bb6.zip
Updated rdoc / guides / release notes related to ActiveJob / ActionMailer
Diffstat (limited to 'guides/source/action_mailer_basics.md')
-rw-r--r--guides/source/action_mailer_basics.md32
1 files changed, 28 insertions, 4 deletions
diff --git a/guides/source/action_mailer_basics.md b/guides/source/action_mailer_basics.md
index 9ad9319255..6b6ce145e4 100644
--- a/guides/source/action_mailer_basics.md
+++ b/guides/source/action_mailer_basics.md
@@ -159,7 +159,10 @@ $ bin/rake db:migrate
Now that we have a user model to play with, we will just edit the
`app/controllers/users_controller.rb` make it instruct the `UserMailer` to deliver
an email to the newly created user by editing the create action and inserting a
-call to `UserMailer.welcome_email` right after the user is successfully saved:
+call to `UserMailer.welcome_email` right after the user is successfully saved.
+
+Action Mailer is nicely integrated with Active Job so you can send emails outside
+of the request-response cycle, so the user doesn't have to wait on it:
```ruby
class UsersController < ApplicationController
@@ -171,7 +174,7 @@ class UsersController < ApplicationController
respond_to do |format|
if @user.save
# Tell the UserMailer to send a welcome email after save
- UserMailer.welcome_email(@user).deliver
+ UserMailer.welcome_email(@user).deliver_later
format.html { redirect_to(@user, notice: 'User was successfully created.') }
format.json { render json: @user, status: :created, location: @user }
@@ -184,8 +187,29 @@ class UsersController < ApplicationController
end
```
-The method `welcome_email` returns a `Mail::Message` object which can then just
-be told `deliver` to send itself out.
+NOTE: By default Active Job is configured to execute the job `:inline`. So you can
+use `deliver_later` now to send the emails and when you decide to start sending the
+email from a background job you'll just have to setup Active Job to use a queueing
+backend (Sidekiq, Resque, etc).
+
+If you want to send the emails right away (from a cronjob for example) just
+call `deliver_now`:
+
+```ruby
+class SendWeeklySummary
+ def run
+ User.find_each do |user|
+ UserMailer.weekly_summary(user).deliver_now
+ end
+ end
+end
+```
+
+The method `welcome_email` returns a `ActionMailer::MessageDelivery` object which
+can then just be told `deliver_now` or `deliver_later` to send itself out. The
+`ActionMailer::MessageDelivery` object is just a wrapper around a `Mail::Message`. If
+you want to inspect, alter or do anything else with the `Mail::Message` object you can
+access it with the `message` method on the `ActionMailer::MessageDelivery` object.
### Auto encoding header values