使用 ActionMailer 在 Rails 中发送给多个收件人
我正在尝试根据数据库中的布尔值发送多封电子邮件。该应用程序是一个简单的调度应用程序,用户可以将他们的轮班标记为“replacement_needed”,这应该向所有请求接收这些电子邮件的用户发送电子邮件。问题是,它似乎只发送一封电子邮件。这是我当前的代码:
def request_replacement(shift)
@shift = shift
@user = shift.user
@recipients = User.where(:replacement_emails => true).all
@url = root_url
@recipients.each do |r|
@name = r.fname
mail(:to => r.email,
:subject => "A replacement clerk has been requested")
end
end
I'm trying to send multiple emails based on a boolean value in my database. The app is a simple scheduling app and user can mark their shift as "replacement_needed" and this should send out emails to all the users who've requested to receive these emails. Trouble is, it only every seems to send to one email. Here's my current code:
def request_replacement(shift)
@shift = shift
@user = shift.user
@recipients = User.where(:replacement_emails => true).all
@url = root_url
@recipients.each do |r|
@name = r.fname
mail(:to => r.email,
:subject => "A replacement clerk has been requested")
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Rails 指南(Action Mailer 基础知识)中,它对多封电子邮件说了以下内容:
因此
"[email protected]、[电子邮件受保护]"
和["[电子邮件受保护]", "[电子邮件受保护]"]
应该可以。查看更多信息:http://guides.rubyonrails.org/action_mailer_basics.html
In the Rails guides (Action Mailer Basics) it says the following regarding multiple emails:
So both
"[email protected], [email protected]"
and["[email protected]", "[email protected]"]
should work.See more at: http://guides.rubyonrails.org/action_mailer_basics.html
您可以像这样向多个收件人发送一封电子邮件。
这将获取您的所有
@recipients
电子邮件地址,并使用,
将它们连接起来。我认为您也可以将数组传递给:to
键,但不确定。唯一的问题是您将无法在模板中使用
@name
。 :(You can just send one email for multiple recipients like this.
This will take all your
@recipients
email addresses and join them with,
. I think you can also pass an array to the:to
key but not sure.The only problem is you won't be able to use
@name
in your template. :(我有同样的问题。我不知道这是什么交易。我通过以下方式回避它:
我不是
从控制器调用,
而是在邮件程序上定义一个类方法,然后调用它。然后该方法将迭代列表并调用“n”次传递。这似乎有效:
从控制器调用
I have the same problem. I don't know what is the deal is. I sidestep it by:
instead of calling
from my controller,
I'd define a class method on the mailer, and call that. That method would then iterate through the list and call deliver "n" times. That seems to work:
and from the controller, call
要防止每个收件人看到其他电子邮件地址:
To prevent each recipient from seeing the other email addresses:
我正在使用Rails 5,我也遇到了同样的情况,电子邮件仅发送给最后一个收件人,但也以纯文本形式发送,而不是 HTML 电子邮件。
在尝试了一些建议之后,我最终以这种方式修复了它:
邮件程序:
调用邮件程序的控制器:
在视图中,类似这样:
它就像一个魅力!
我不确定这是否是正确的方法或最佳的方法,但只是将其视为第二种可能性。
我希望它对其他人有用。
I'm using Rails 5 and I have the same situation, the email was sent only to the last recipient but also it was sent just as plain text not as HTML email.
After trying some advices, I ended up fixing it in this way:
The mailer:
The controller where the mailer is invoked:
And in the view, something like this:
And it works like a charm!
I'm not sure if this is the correct way or the most optimal way to go but just consider it as a second possibility.
I hope it could be useful for somebody else.