How can I send mail with rails without a template?

后端 未结 4 1872
旧巷少年郎
旧巷少年郎 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 01:47

    Here is little example from Rails Guides which uses render method. I didn't try it, but if it works as render in cotrollers, then you can just use:

    render :text => "Your message"
    

    or

    render :text => my_message
    

    Where my_message is a parameter.

    You can just wrap it in a method which you can call from every place you want.

    Updated Rails 3.2.8

    In this version of Rails I had to do it like this:

    def raw_email( email, subject, body )
      mail(
        :to => email,
        :subject => subject
      ) do |format|
        format.text { render :text => body }
      end
    end
    

提交回复
热议问题