How can I send mail with rails without a template?
The simplest way to send mail in rails 3 without a template is to call the mail
method of ActionMailer::Base
directly followed by the deliver
method,
For ex, the following would send a plain text e-mail:
ActionMailer::Base.mail(
from: "[email protected]",
to: "[email protected]",
subject: "test",
body: "test"
).deliver
http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail gives you all the header options and also ideas about the how to send a multipart/alternative email with text/plain and text/html parts directly.
Rails 5 users may find the accepted answer (using format.text {...}
) doesn't work; at least I was getting an exception because Rails was looking for a view.
Turns out there's a section in the Rails Guide called Sending Emails without Template Renderingand all one needs to do is supply :content_type and :body options to mail()
. E.g.:
class UserMailer < ApplicationMailer
def welcome_email
mail(to: params[:user].email,
body: params[:email_body],
content_type: "text/html",
subject: "Already rendered!")
end
end