ActionMailer 3 without Rails

前端 未结 2 1249
执念已碎
执念已碎 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:28

    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

    this is an html email

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

    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 :)

提交回复
热议问题