Rails 申请的 Mailgun

Mailgun for Rails Application

目前我正在努力将 Mailgun 实施到我的 rails 应用程序中,但我似乎对最佳方法感到困惑。

我已经实现的方法是设置区域,添加 gemfile,然后通过 Active Job 设置后台处理器。由于不确定这是否是最好的方式,我还没有设置后台处理器。

我看到其他资源说我应该简单地使用 Heroku 插件,然后使用额外的、最少的编码。我还想提一下,我有 Devise 设置,所以我不需要为典型情况发送电子邮件。

如您所见,此时我很迷茫,只是想了解哪种方法最好。如果您碰巧了解自己或有好的资源可以使用,请告诉我=)

旁注:一路上我也被告知 "Don't trigger the email sending from an ActiveRecord callback - Do it from the controller." 我写下它是为了参考何时到了,我在这里 =P 你同意这个说法吗?

如果有帮助,我可以添加代码,但我对哪些代码真正有用感到困惑...

非常感谢!

编辑

还有一个问题:理想情况下,我希望能够向应用程序上的所有用户发送一封自定义电子邮件(即:每周更新、新站点功能等)。 Mailgun 甚至是执行此操作的正确方法吗?

Mailgun 与 Sendgrid 或 Mandrill 没有区别,因此在寻找一些指导时,您也可以考虑这些解决方案。为您的用例提供最佳指南是 Sending Emails in Rails Applications. You can also choose from the existing gems to Mailgun API Official gem or ActiveRecord integration.

The way I have already implemented is to setup the areas, add the gemfile and then setup the background processor through Active Job. I have not yet setup the background processor due to being unsure if this is the best way.

是的,这是正确的设置方法。在 Rails 4.2 之前,我相信大多数在 ActiveJob 不存在的情况下编码的人仍然更喜欢 djsidekiqresque 它们非常受欢迎,几乎在每个 Rails 应用程序中都可以找到它们。您可以将它们与 ActiveJob 的界面一起使用。不过,概念是一样的。

为了让您了解缺少的代码部分,您可以这样做(从第一个 link)

class ExampleMailer < ActionMailer::Base

  def sample_email(user)
    @user = user
    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {:from    => ENV['gmail_username'],
                      :to      => @user.email,
                      :subject => 'Sample Mail using Mailgun API',
                      :text    => 'This mail is sent using Mailgun API via mailgun-ruby'}
    mg_client.send_message ENV['domain'], message_params
  end
end

Side note: I was also told along the way "Don't trigger the email sending from an ActiveRecord callback - Do it from the controller." I wrote that down to refer to when the time came for it and here I am =P Would you agree with that statement?

我绝对同意这种说法。有一件事是,将所有内容链接到 ActiveRecord 回调乍一看似乎非常实用,但是随着应用程序的增长,您会发现自己放入条件语句或使用状态机之类的东西来掩盖。背后的情况是,您可能会从多个来源(用户注册、管理员导入等)在您的应用程序中创建记录,而不是在所有情况下您都希望发送电子邮件。此外,对于我和新团队成员来说,直接在控制器代码中查看它比通过模型的回调更实际。最后这个也算是个人喜好吧。

如果您使用的是 Heroku - 然后看看这个 Delayed Job guide. On Heroku, you will have to run these background processes in the separate workers (which are not free), for some cost effective solutions take a loot at Running delayed jobs on Heroku for free