Skip to content

Instantly share code, notes, and snippets.

@barelyknown
Created September 10, 2012 17:42
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save barelyknown/3692433 to your computer and use it in GitHub Desktop.
Save barelyknown/3692433 to your computer and use it in GitHub Desktop.
Send Email From Rails Console
# Simple approach to sending email from the Rails console
# Implementation idea courtesy of Steve Klabnik
# http://blog.steveklabnik.com/posts/2012-09-09-random-ruby-tricks--class-new
# Create the mailer class with a block and assign to a variable
mailer = Class.new(ActionMailer::Base) do
def example_message
mail(to: "test@test.com", from: "test@test.com", subject: "Example Message") do |format|
format.text { render text: "Example message body" }
end
end
end
# Use the class just as you would a typical mailer
mailer.example_message.deliver
@sairam
Copy link

sairam commented Oct 3, 2018

the above does not work with rails 5.

  mailer = Class.new(ActionMailer::Base) do
      def example_message
        mail(to: "test@test.com", from: "test@test.com", subject: "Example Message", body: "Example message body")
     end
  end
  mailer.example_message.deliver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment