开发者

ActionMailer 3 without Rails

开发者 https://www.devze.com 2023-02-09 08:26 出处:网络
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 in to issues. All the search

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 in to issues. All the searching I've done so far on using ActionMailer outside of Rails applies to versions prior to version 3. Could someone point me in the right direction of where to find resources on how to do this? Here's where I am so far on my mailer file:

# lib/bug_mailer.rb
require 'action_mailer'

ActionMailer::Base.delivery_method = :file

class BugMailer < ActionMailer::Base
  def daily_email
    mail(
            :to      => "example@mail.com",
            :from   开发者_JAVA技巧 => "example@mail.com",
            :subject => "testing mail"
    )
  end
end

BugMailer.daily_email.deliver

I'm definitely stuck on where to put my views. Every attempt I've made to tell ActionMailer where my templates are has failed.

I guess I should also ask if there's a different way to go about accomplishing this program. Basically, I'm doing everything from scratch at this point. Obviously what makes Rails awesome is it's convention, so is trying to use parts of Rails on their own a waste of time? Is there a way to get the Rails-like environment without creating a full-blown Rails app?


After some serious debugging, I found how to configure it.

file mailer.rb

require 'action_mailer'

ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
   :address   => "smtp.gmail.com",
   :port      => 587,
   :domain    => "domain.com.ar",
   :authentication => :plain,
   :user_name      => "test@domain.com.ar",
   :password       => "passw0rd",
   :enable_starttls_auto => true
  }
ActionMailer::Base.view_paths= File.dirname(__FILE__)

class Mailer < ActionMailer::Base

  def daily_email
    @var = "var"

    mail(   :to      => "myemail@gmail.com",
            :from    => "test@domain.com.ar",
            :subject => "testing mail") do |format|
                format.text
                format.html
    end
  end
end

email = Mailer.daily_email
puts email
email.deliver

file mailer/daily_email.html.erb

<p>this is an html email</p>
<p> and this is a variable <%= @var %> </p>

file mailer/daily_email.text.erb

this is a text email

and this is a variable <%= @var %>

Nice question! It helped me to understand a bit more how Rails 3 works :)


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/<GEM_NAME>/views/mailers/<MAILER_CLASS_NAME>/<MAILER_ACTION_NAME>.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
0

精彩评论

暂无评论...
验证码 换一张
取 消