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 places in the system.
在我的Rails 3项目中,我想发送一些简单的通知邮件。我不需要为他们做一个模板或做任何逻辑。我只是想把它们从系统的不同位置发射出去。
If I were doing this in an arbitrary ruby script I would use pony. However, I'd like to still use the rails mail facilities and configuration, so that I get the same reliability and setup that I have for the rest of the mail in my system.
如果我在一个任意的ruby脚本中这样做,我会使用pony。但是,我仍然希望使用rails邮件设施和配置,以便获得与系统中其余邮件相同的可靠性和设置。
What's the most simple way to do this? Ideally there would be some method like
最简单的方法是什么?理想情况下会有一些类似的方法
ActionMailer.send(:to => 'foo@example.com', :subject =>"the subject", :body =>"this is the body")
4 个解决方案
#1
93
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,
在rails 3中不使用模板发送邮件的最简单的方法是调用ActionMailer: Base的邮件方法,
For ex, the following would send a plain text e-mail:
对于ex,以下将发送纯文本电子邮件:
ActionMailer::Base.mail(from: "me@example.com", to: "you@example.com", 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.
http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail为您提供了所有的标题选项以及如何使用文本/纯文本和文本/html部分直接发送多部分/替代电子邮件的想法。
#2
10
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:
下面是使用渲染方法的Rails指南中的一个小示例。我没有尝试过,但是如果它在cotrollers中起渲染作用,那么你可以使用:
render :text => "Your message"
or
或
render :text => my_message
Where my_message
is a parameter.
其中my_message是一个参数。
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:
在这个版本的Rails中,我必须这样做:
def raw_email( email, subject, body )
mail(
:to => email,
:subject => subject
) do |format|
format.text { render :text => body }
end
end
#3
7
You can try something like this:
你可以试试这样的方法:
class Notifier < ActionMailer::Base
def send_simple_message(options)
mail(options.except(:body)) do |format|
format.text { render :text => options[:body] }
end.deliver
end
end
#4
0
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.
Rails 5用户可以找到被接受的答案(使用格式)。文本{…})不工作;至少我得到了一个异常,因为Rails正在寻找一个视图。
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.:
Rails指南中有一个部分叫做发送电子邮件,没有模板渲染,你需要做的就是提供:content_type和:mail()的body选项。例如:
class UserMailer < ApplicationMailer
def welcome_email
mail(to: params[:user].email,
body: params[:email_body],
content_type: "text/html",
subject: "Already rendered!")
end
end
#1
93
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,
在rails 3中不使用模板发送邮件的最简单的方法是调用ActionMailer: Base的邮件方法,
For ex, the following would send a plain text e-mail:
对于ex,以下将发送纯文本电子邮件:
ActionMailer::Base.mail(from: "me@example.com", to: "you@example.com", 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.
http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail为您提供了所有的标题选项以及如何使用文本/纯文本和文本/html部分直接发送多部分/替代电子邮件的想法。
#2
10
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:
下面是使用渲染方法的Rails指南中的一个小示例。我没有尝试过,但是如果它在cotrollers中起渲染作用,那么你可以使用:
render :text => "Your message"
or
或
render :text => my_message
Where my_message
is a parameter.
其中my_message是一个参数。
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:
在这个版本的Rails中,我必须这样做:
def raw_email( email, subject, body )
mail(
:to => email,
:subject => subject
) do |format|
format.text { render :text => body }
end
end
#3
7
You can try something like this:
你可以试试这样的方法:
class Notifier < ActionMailer::Base
def send_simple_message(options)
mail(options.except(:body)) do |format|
format.text { render :text => options[:body] }
end.deliver
end
end
#4
0
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.
Rails 5用户可以找到被接受的答案(使用格式)。文本{…})不工作;至少我得到了一个异常,因为Rails正在寻找一个视图。
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.:
Rails指南中有一个部分叫做发送电子邮件,没有模板渲染,你需要做的就是提供:content_type和:mail()的body选项。例如:
class UserMailer < ApplicationMailer
def welcome_email
mail(to: params[:user].email,
body: params[:email_body],
content_type: "text/html",
subject: "Already rendered!")
end
end