How can I send mail with rails without a template?

后端 未结 4 1866
旧巷少年郎
旧巷少年郎 2021-02-01 01:29

In my Rails 3 project, I want to send some simple notification emails. I don\'t need to make a template for them or do any logic. I just want to fire them off from various place

4条回答
  •  后悔当初
    2021-02-01 02:11

    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
    

提交回复
热议问题