梅勒在发动机上发送网络邮件

时间:2014-01-28 12:31:09

标签: ruby-on-rails email ruby-on-rails-4 rails-engines

我正在使用rails 4为简单的电子商务系统编写新闻稿引擎。 为此,我在\app\mailers\aecs_newsletter\newsletter_mailer.rb

为我的引擎编写了这封邮件
module AecsNewsletter
    class NewsletterMailer < ::ActionMailer::Base

        default from: 'newsletter@ecommerce.de'

        def send_newsletter_to_subscribers(subscribers, newsletter)
            subscribers.each do |subscriber|
                @email = subscriber.email
                @newsletter = newsletter
                mail(to: @email, subject: newsletter.subject, template_name: 'newsletter')
            end
        end

    end
end

我从::ActionMailer::Base继承以获取主应用程序的邮件配置。这种配置效果很好(通过设计的确认邮件进行测试)。

但是当我在引擎的控制器中执行函数send_newsletter_to_subscribers时,函数结束时没有错误,也没有发送邮件。

以下是方法调用的日志:

Started GET "/newsletters/admin/1/send_newsletter" for x.x.x.x at 2014-01-28 13:19:47 +0100
Processing by AecsNewsletter::Admin::NewslettersController#send_newsletter as HTML
  Parameters: {"id"=>"1"}
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
   (0.0ms)  begin transaction
  SQL (0.0ms)  UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = 1  [["updated_at", Tue, 28 Jan 2014 12:19:47 UTC +00:00]]
   (203.1ms)  commit transaction
  Userrole Load (0.0ms)  SELECT "userroles".* FROM "userroles" WHERE "userroles"."id" = ? ORDER BY "userroles"."id" ASC LIMIT 1  [["id", 1]]
  AecsNewsletter::Newsletter Load (0.0ms)  SELECT "aecs_newsletter_newsletters".* FROM "aecs_newsletter_newsletters" WHERE "aecs_newsletter_newsletters"."id" = ? LIMIT 1  [["id", "1"]]
  AecsNewsletter::Subscriber Load (0.0ms)  SELECT "aecs_newsletter_subscribers".* FROM "aecs_newsletter_subscribers"
  Rendered /aecs_newsletter/app/views/aecs_newsletter/newsletter_mailer/newsletter.html.erb (0.0ms)
  Rendered /aecs_newsletter/app/views/aecs_newsletter/newsletter_mailer/newsletter.text.erb (0.0ms)
[...]
Redirected to http://x.x.x.x:3000/newsletters/admin/1
Completed 302 Found in 547ms (ActiveRecord: 375.0ms)

因此该函数呈现视图,但没有发送邮件。 我想,我没有继承邮件配置权。但是我应该以哪种方式使用主要的应用邮件配置?

有没有人解决我的问题? 谢谢!

1 个答案:

答案 0 :(得分:2)

您尚未要求交付。添加.deliver

module AecsNewsletter
    class NewsletterMailer < ::ActionMailer::Base

        default from: 'newsletter@ecommerce.de'

        def send_newsletter_to_subscribers(subscribers, newsletter)
            subscribers.each do |subscriber|
                @email = subscriber.email
                @newsletter = newsletter
                mail(to: @email, subject: newsletter.subject, template_name: 'newsletter').deliver
            end
        end

    end
end