向大量收件人发送电子邮件的最佳做法(Rails + SendGrid)

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

SendGrid真的不是500条消息。 它甚至不是他们雷达上的一个昙花一现。 我曾在一家公司工作过一个月就发送了270万封电子邮件,即便如此,这只是一个短暂的问题。

使用SendGrid API的function,您将不会发送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_job或者resque gems这样的东西来处理它。

哦,顺便说一句,你仍然需要为这封电子邮件指定一个地址,以使Mailgem快乐。 我们有info@ourcompany.com

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

如果您阅读SendGrid API文档,它会为您带来很多好处。 它真的很有用,它们提供的function非常强大。

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

这是rails 3动作邮件示例的示例:

 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} " subject = "Important" mail({from: from_name, to: to, subject: subject}) end end 

和相应的调用代码。 建议将电子邮件arrays设为<1000封电子邮件。

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

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

延迟作业和SendGrid听起来像你说的最好的选择,但你有没有考虑使用像Mailchimp这样的竞选邮件? 如果您发送了大量基本相同的邮件,他们会让您设置和制作广告模板,然后在其中触发所有变量的CSV。 然后他们有效地邮寄合并并将它们全部解雇。

但是,如果你只说几百个,那么你就是正确的。 SendGrid可以轻松处理负载,并且您希望使用延迟作业,以便在不受欢迎时不受SendGrid API性能的影响。 或者,查看Resque而不是发送邮件,因为它可能更有效。

SendGrid提供了一些建议。 他们的博客上有可交付性和最佳实践的类别。

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

这就是我在Rails 4中的表现

 class NewsMailer < ApplicationMailer include SendGrid sendgrid_category :use_subject_lines default from: 'My App! ' 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