Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 向许多收件人发送电子邮件的最佳实践(Rails+;SendGrid)_Ruby On Rails_Heroku_Sendgrid - Fatal编程技术网

Ruby on rails 向许多收件人发送电子邮件的最佳实践(Rails+;SendGrid)

Ruby on rails 向许多收件人发送电子邮件的最佳实践(Rails+;SendGrid),ruby-on-rails,heroku,sendgrid,Ruby On Rails,Heroku,Sendgrid,我将从Rails应用程序发送大量电子邮件,并计划使用SendGrid。我假设最好给每个收件人发送一封单独的电子邮件(而不是对所有收件人使用密件抄送)。如果这是真的,我应该使用DelayedJob之类的工具来对发送到SendGrid的消息进行排队,还是一次性抛出500条消息比较安全?谢谢 SendGrid提供了一些建议。他们的博客上有一个可交付性和最佳实践的分类。我想SendGrid可以处理这种负载。大多数中继系统都可以。我还可以想象,如果您在一个CC-API调用中发送500,那么他们的系统将解析

我将从Rails应用程序发送大量电子邮件,并计划使用SendGrid。我假设最好给每个收件人发送一封单独的电子邮件(而不是对所有收件人使用密件抄送)。如果这是真的,我应该使用DelayedJob之类的工具来对发送到SendGrid的消息进行排队,还是一次性抛出500条消息比较安全?谢谢

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

我想SendGrid可以处理这种负载。大多数中继系统都可以。我还可以想象,如果您在一个CC-API调用中发送500,那么他们的系统将解析它并单独发送它们。我使用弹性电子邮件()-我知道他们就是这样处理的,而且效果很好。

从你所说的来看,延迟工作和SendGrid听起来是最好的选择,但是你是否考虑过使用Mailchimp这样的竞选邮件?如果你发送了很多基本相同的邮件,他们会让你设置和创建活动模板,然后向它发送一个包含所有变量的CSV。然后,他们有效地将邮件合并并将其全部发送出去


然而,如果你只说了几百句话,那么你说的是对的。SendGrid可以轻松地处理负载,并且您希望使用延迟作业,以便在SendGrid API的性能不佳时不会受到影响。或者,在发送邮件时使用Resque,因为它可能更高效。

500条消息对SendGrid来说并不是那么重要。他们的雷达上连一个光点都没有。我为一家在一个月内发出270万封电子邮件的公司工作,即便如此,这也只是昙花一现

使用SendGrid API的功能,您将不会发送500封电子邮件,而是发送一封具有特定SendGrid API头集的电子邮件。为什么?因为你有没有试过发送500封个人电子邮件,并计算了需要多长时间?一封电子邮件怎么样?一封邮件会更快

SendGrid API有一个Ruby示例,如下所示:

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

headers["X-SMTPAPI"] = { :to => array_of_recipients }.to_json
然后SendGrid将对此进行解析,然后将您发送的一封电子邮件发送给该收件人数组。我似乎还记得,他们要求你将每封邮件的收件人限制在1000人左右,所以如果你愿意的话,最好将邮件分成多封。也就是说,你会引入一些类似于
delayed\u job
resque
gems的东西来处理它

哦,顺便说一句,你仍然需要为这封电子邮件指定一个
收件人
地址,这样才能让邮件变得愉快。我们有
info@ourcompany.com
,谢谢

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


如果您阅读SendGrid API文档,这将对您大有好处。它非常有用,而且它们提供的功能非常强大。

我建议使用sendgrid gem(),因为它简化了调用代码

下面是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

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

这是我在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
classnewsmailerarray_of_emails}.to_json
sendgrid\u收件人数组\u电子邮件
邮寄至:'this.will.be。ignored@ignore.me",主题:"每周新闻"
结束
结束

关于虚拟
收件人地址的好提示。指向SendGrid API的链接已过时-我认为现在是正确的。“收件人”地址让我感到困惑,但你在这里说得很清楚,谢谢!谢谢用rails一次批处理1000封电子邮件的最佳方法是什么?您好!你的例子似乎很有趣,你在github有完整的例子吗?或者你能和我分享一篇文章或是一篇教程吗?我正在尝试使用Sendgrid和Delay_Job的
设计邮件列表。您可以共享的任何帮助都将非常有用。该链接返回404。请发布实际内容,而不仅仅是一个链接。
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