deliver_now on a mailer method?Consider a Rails mailer method that sends an email. What is the behavior when you call deliver_now on the mailer method?
UserMailer.welcome_email(user).deliver_now
Think about synchronous vs asynchronous email sending.
deliver_now sends the email immediately during the current request cycle, blocking until the email is sent.
In a Rails mailer class, how do you set the default sender email address for all emails?
class UserMailer < ApplicationMailer # set default from here end
Look for the correct method name and syntax for setting defaults.
The correct syntax is default from: 'email' inside the mailer class.
Given the following mailer method, why does calling UserMailer.notify(user).deliver_now raise an error?
class UserMailer < ApplicationMailer def notify(user) mail(to: user.email, subject: 'Notification') do |format| format.text { render plain: 'Hello!' } format.html { render html: '<strong>Hello!</strong>'.html_safe } end end end
Check how render works inside mailers.
render html: expects a template or partial, not a raw string. To send raw HTML, use format.html { render inline: ... } or format.html { render html: ... } with safe HTML.
@greeting in the mailer view after calling this method?Consider this mailer method:
def welcome(user)
@greeting = "Hello, #{user.name}!"
mail(to: user.email, subject: 'Welcome')
endWhat will @greeting contain in the mailer view?
Think about how instance variables work in mailers and views.
Instance variables set in the mailer method are available in the mailer views, so @greeting will contain the greeting string with the user's name.
You want to send emails without blocking the web request by using background jobs. Which configuration and method call combination achieves this in Rails?
Consider how to enable background job processing and which mailer method triggers async sending.
To send emails asynchronously, you must have a background job adapter configured (like Sidekiq or the default async adapter) and call deliver_later. The delivery method (like SMTP) must be set to actually send emails.