問題一覧に戻る
上級バックグラウンドと通知
問題67: メール送信 - Action Mailer
RailsでAction Mailerを使用してメールを送信する方法を学習します。メール配信設定を構成し、テンプレート付きのメーラークラスを作成し、プレーンテキストとHTMLメールの両方を送信し、非同期配信のためのバックグラウンドジョブと統合します。
# メーラークラスを定義
class UserMailer <
default from: 'notifications@example.com'
def welcome_email(user)
@user = user
# mailメソッドでメールを作成
(to: @user.email, subject: 'Welcome!')
end
end
# Controller
# メールをすぐに送信
UserMailer.welcome_email(@user).
# バックグラウンドでメールを送信
UserMailer.welcome_email(@user).