【问题标题】:Best practices for sending email to lots of recipients (Rails + SendGrid)向大量收件人发送电子邮件的最佳实践 (Rails + SendGrid)
【发布时间】:2011-12-11 03:31:19
【问题描述】:

我将从 Rails 应用程序发送大量电子邮件并计划使用 SendGrid。我假设最好向每个收件人发送一封单独​​的电子邮件(而不是为所有收件人使用密件抄送)。如果这是真的,我应该使用像 DelayedJob 这样的东西来排队发送到 SendGrid 的消息,还是一次抛出 500 条消息是否安全?谢谢!

【问题讨论】:

    标签: ruby-on-rails heroku sendgrid


    【解决方案1】:

    500 条消息对于 SendGrid 来说真的不算多。这甚至不是他们雷达上的一个小插曲。我在一家公司工作,该公司在一个月内发送了 270 万封电子邮件,即便如此,这也只是只是昙花一现。

    使用 SendGrid API 的功能,您不会发送 500 封电子邮件,您会发送 一封 具有特定 SendGrid API 标头集的电子邮件。为什么?因为您曾经尝试过发送 500 封单独的电子邮件,并计算出这需要多长时间? 一封电子邮件怎么样?单个电子邮件会更快。

    SendGrid API 有一个 Ruby 示例,如下所示: https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html.

    这是相当冗长和混乱,所以让我为你简化它。基本上,您在电子邮件中设置:

    headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json
    

    SendGrid 将解析此内容,然后将您发送的一封电子邮件发送给该收件人数组。我似乎记得他们要求您将其限制为每封电子邮件约 1000 个收件人,因此如果您愿意,最好将其拆分为多封电子邮件。 是你需要引入类似delayed_jobresque gems 来处理它的时候。

    哦,顺便说一句,您仍然需要为这封电子邮件指定一个to 地址,只是为了让 Mail gem 开心。为此,我们有 info@ourcompany.com

    SendGrid API 还将在其电子邮件中支持过滤器,因此您可以使用 {{ firstname }} 等占位符字符串,并且假设您使用 SMTPAPI 标头发送它,它将对电子邮件执行“邮件合并”并自定义他们。

    如果您阅读 SendGrid API 文档,您会受益匪浅。它真的很有用,而且它们提供的功能超级强大。

    【讨论】:

    • 关于虚拟to 地址的好提示。
    • SendGrid API 的链接已过期 - 我认为 sendgrid.com/docs/Code_Examples/SMTP_API_Header_Examples/… 现在是正确的。
    • “to”地址让我感到困惑,但你在这里说清楚了,谢谢!
    • 谢谢!一次使用 rails 将电子邮件实际批量处理到 1000 个的最佳方法是什么?
    【解决方案2】:

    我建议使用 sendgrid gem (https://github.com/stephenb/sendgrid),因为它可以简化您的调用代码。

    这是一个示例 rails 3 action mailer 示例:

    class UserAnnouncementMailer < ActionMailer::Base
      include SendGrid
      default reply_to: "test@test.com", return_path: "test@test.com", from: "Test"
    
      # bulk emailer
      # params - opts a hash of
      #            emails: array of emails
      #
      def notice(opts={})
        raise "email is nil" unless opts[:emails]
    
        sendgrid_category :use_subject_lines
        sendgrid_recipients opts[:emails]
    
        name = "The Man"
        to = "test@test.com"
        from_name = "#{name} <theman@test.com>"
        subject = "Important"
    
        mail({from: from_name, to: to, subject: subject})
      end
    end
    

    以及对应的调用代码。建议将 emails 数组设置为

    emails = ["alice@test.com", "bob@test.com"]
    UserAnnouncementMailer.notice({:emails => emails}).deliver
    

    有关更多详细信息,请参阅 sendgrid gem github 自述文件。

    【讨论】:

      【解决方案3】:

      就您所说的而言,Delayed Job 和 SendGrid 听起来是最好的选择,但您是否考虑过使用 Mailchimp 等竞选邮件程序之一?如果您要发送大量基本相同的邮件,它们将让您设置和活动模板,然后在其中触发所有变量的 CSV。然后他们有效地邮件合并并全部解雇。

      但是,如果您只说几百个,那么您就对了。 SendGrid 可以轻松处理负载,并且您希望使用延迟作业,这样您就不会受到 SendGrid API 性能不利的影响。或者,使用 Resque 代替发送邮件,因为它可能更有效。

      【讨论】:

        【解决方案4】:

        我想 SendGrid 可以处理这种负载。大多数中继系统都可以。另外我想如果你在 CC API 调用中发送 500,他们的系统会解析它并单独发送它们。我使用 Elastic 电子邮件 (http://elasticemail.com) - 我知道他们就是这样处理它的,而且效果很好。

        【讨论】:

          【解决方案5】:

          这就是我在 Rails 4 中的做法

          class NewsMailer < ApplicationMailer
            include SendGrid
          
            sendgrid_category :use_subject_lines
          
            default from: 'My App! <support@myapp.com>'
          
            def mass_mailer(news)
              # Pass it in template
              @news = news
          
              # Custom method to get me an array of emails ['user1@email.com', 'user2@email.com',...] 
              array_of_emails = @news.recipients.pluck(:email) 
          
              # You can still use
              # headers["X-SMTPAPI"] = { :to => array_of_emails }.to_json
              sendgrid_recipients array_of_emails
          
              mail to: 'this.will.be.ignored@ignore.me', subject: 'Weekly news'
            end
          
          end
          

          【讨论】:

          • 您好!您的示例似乎很有趣,您在 github 上是否有完整的示例?或者你能分享给我一篇文章或教程吗?我正在尝试使用Devise with Sendgrid and Delay_Job 作为邮件列表。您可以分享的任何帮助都会变得非常有用。
          猜你喜欢
          • 2010-12-10
          • 2010-11-19
          • 2019-06-26
          • 1970-01-01
          • 2019-01-09
          • 2011-02-16
          • 2021-09-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多