使用 ActionMailer 在 Rails 中发送给多个收件人

发布于 2024-12-05 00:03:26 字数 499 浏览 1 评论 0原文

我正在尝试根据数据库中的布尔值发送多封电子邮件。该应用程序是一个简单的调度应用程序,用户可以将他们的轮班标记为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静若繁花 2024-12-12 00:03:26

在 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:

The list of emails can be an array of email addresses or a single string with the addresses separated by commas.

So both "[email protected], [email protected]" and ["[email protected]", "[email protected]"] should work.

See more at: http://guides.rubyonrails.org/action_mailer_basics.html

烟燃烟灭 2024-12-12 00:03:26

您可以像这样向多个收件人发送一封电子邮件。

def request_replacement(shift)
  @shift = shift
  @user = shift.user
  @recipients = User.where(:replacement_emails => true)
  @url  = root_url
  emails = @recipients.collect(&:email).join(",")
  mail(:to => emails, :subject => "A replacement clerk has been requested")
end

这将获取您的所有 @recipients 电子邮件地址,并使用 , 将它们连接起来。我认为您也可以将数组传递给 :to 键,但不确定。

唯一的问题是您将无法在模板中使用@name。 :(

You can just send one email for multiple recipients like this.

def request_replacement(shift)
  @shift = shift
  @user = shift.user
  @recipients = User.where(:replacement_emails => true)
  @url  = root_url
  emails = @recipients.collect(&:email).join(",")
  mail(:to => emails, :subject => "A replacement clerk has been requested")
end

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. :(

二手情话 2024-12-12 00:03:26

我有同样的问题。我不知道这是什么交易。我通过以下方式回避它:

我不是

Mailer.request_replacement(shift).deliver 

从控制器调用,

而是在邮件程序上定义一个类方法,然后调用它。然后该方法将迭代列表并调用“n”次传递。这似乎有效:

class Mailer

   def self.send_replacement_request(shift)
     @recipients = ...
     @recipients.each do |recipient|
       request_replacement(recipient, shift).deliver
     end
   end

   def request_replacement(recipient, shift)
     ...
     mail(...)
   end
end

从控制器调用

Mailer.send_replacement_request(shift)

I have the same problem. I don't know what is the deal is. I sidestep it by:

instead of calling

Mailer.request_replacement(shift).deliver 

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:

class Mailer

   def self.send_replacement_request(shift)
     @recipients = ...
     @recipients.each do |recipient|
       request_replacement(recipient, shift).deliver
     end
   end

   def request_replacement(recipient, shift)
     ...
     mail(...)
   end
end

and from the controller, call

Mailer.send_replacement_request(shift)
短叹 2024-12-12 00:03:26

要防止每个收件人看到其他电子邮件地址:

@recipients.each{ |recipient| Mailer.request_replacement(recipient, shift).deliver }

To prevent each recipient from seeing the other email addresses:

@recipients.each{ |recipient| Mailer.request_replacement(recipient, shift).deliver }
骑趴 2024-12-12 00:03:26

我正在使用Rails 5,我也遇到了同样的情况,电子邮件仅发送给最后一个收件人,但也以纯文本形式发送,而不是 HTML 电子邮件。

在尝试了一些建议之后,我最终以这种方式修复了它:

邮件程序:

class BrochureMailer < ApplicationMailer
    default from: "[email protected]"

    def newsletter(sponsor, brochures_list)
        @sponsor = sponsor
        @brochures = brochures_list

        mail(
            to: @sponsor.email,
            subject: "Interesting subject!"
        )
    end
end

调用邮件程序的控制器:

class Admin::DashboardController < Admin::BaseController
    def send_newsletter
        sponsors = params[:sponsor_ids]
        brochures = params[:brochure_ids]

        sponsors = Sponsor.where(id: sponsors)
        brochures = Brochure.where(id: brochures).to_a

        # Send Newsletter email to the given Sponsors
        sponsors.each do |sponsor|
            BrochureMailer.newsletter(sponsor, brochures).deliver_later
        end

        redirect_back(fallback_location: admin_root_path, success: 'Newsletter sent!')
    end
end

在视图中,类似这样:

<% @brochures.each do |brochure| %>
    <table width="280" border="0" cellpadding="0" cellspacing="0" align="left" valign="top" class="floater">
        <tr>
            <td align="center" valign="top">
                <a target="_blank" href="<%= brochure_url(brochure) %>">
                    <img border="0" vspace="0" hspace="0" src="<%= brochure.image.blank? ? 'default.png' : brochure.image.url(public: true) %>" width="250" height="142">
                    <b><%= brochure.title %></b>
                </a>
                <br>
                <%= brochure.description.truncate(60) %>
            </td>
        </tr>
    </table>
<% end %>

它就像一个魅力!
我不确定这是否是正确的方法或最佳的方法,但只是将其视为第二种可能性。

我希望它对其他人有用。

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:

class BrochureMailer < ApplicationMailer
    default from: "[email protected]"

    def newsletter(sponsor, brochures_list)
        @sponsor = sponsor
        @brochures = brochures_list

        mail(
            to: @sponsor.email,
            subject: "Interesting subject!"
        )
    end
end

The controller where the mailer is invoked:

class Admin::DashboardController < Admin::BaseController
    def send_newsletter
        sponsors = params[:sponsor_ids]
        brochures = params[:brochure_ids]

        sponsors = Sponsor.where(id: sponsors)
        brochures = Brochure.where(id: brochures).to_a

        # Send Newsletter email to the given Sponsors
        sponsors.each do |sponsor|
            BrochureMailer.newsletter(sponsor, brochures).deliver_later
        end

        redirect_back(fallback_location: admin_root_path, success: 'Newsletter sent!')
    end
end

And in the view, something like this:

<% @brochures.each do |brochure| %>
    <table width="280" border="0" cellpadding="0" cellspacing="0" align="left" valign="top" class="floater">
        <tr>
            <td align="center" valign="top">
                <a target="_blank" href="<%= brochure_url(brochure) %>">
                    <img border="0" vspace="0" hspace="0" src="<%= brochure.image.blank? ? 'default.png' : brochure.image.url(public: true) %>" width="250" height="142">
                    <b><%= brochure.title %></b>
                </a>
                <br>
                <%= brochure.description.truncate(60) %>
            </td>
        </tr>
    </table>
<% end %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文