ActionMailer 3 without Rails

前端 未结 2 1253
执念已碎
执念已碎 2020-12-08 00:53

I\'m writing a small Ruby program that will pull records from a database and send an HTML email daily. I\'m attempting to use ActionMailer 3.0.3 for this, but I\'m running i

2条回答
  •  不知归路
    2020-12-08 01:31

    It took me a while to get this to work in (non-)Rails 4. I suspect it's just because I have ':require => false' all over my Gemfile, but I needed to add the following to make it work:

    require 'action_view/record_identifier'
    require 'action_view/helpers'
    require 'action_mailer'
    

    Without the above code, I kept getting a NoMethodError with undefined method 'assign_controller'.

    After that, I configured ActionMailer as follows:

    ActionMailer::Base.smtp_settings = {
      address: 'localhost', port: '25', authentication: :plain
    }
    ActionMailer::Base.default from: 'noreply@example.com'
    ActionMailer::Base.raise_delivery_errors = true
    ActionMailer::Base.logger = Logger.new(STDOUT)
    ActionMailer::Base.logger.level = Logger::DEBUG
    ActionMailer::Base.view_paths = [
      File.join(File.expand_path("../../", __FILE__), 'views', 'mailers')
      # Note that this is an Array
    ]
    

    The templates go in lib//views/mailers//.erb (MAILER_ACTION_NAME is the public instance method of your mailer class that you call to send the email).

    Lastly, don't forget to put this in your spec_helper:

    ActionMailer::Base.delivery_method = :test
    

提交回复
热议问题