19

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

4

5 回答 5

55

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宝石之类的东西来处理它。

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

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

如果您阅读 SendGrid API 文档,将会对您有很大帮助。它真的很有用,而且它们提供的功能非常强大。

于 2011-10-19T22:29:02.620 回答
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 数组设置为 < 1000 封电子邮件。

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

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

于 2012-09-13T23:06:13.540 回答
1

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

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

于 2011-10-19T22:24:04.873 回答
0

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

于 2011-10-19T22:18:47.970 回答
0

这就是我在 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
于 2015-07-16T01:31:58.210 回答