Ruby 如何在没有rails的情况下使用Actionmailer4

Ruby 如何在没有rails的情况下使用Actionmailer4,ruby,actionmailer,Ruby,Actionmailer,我在没有rails的情况下使用过ActionMailer2.3,在使用4.0组件时,使用actionmailer4的ruby代码不起作用。我现在有了有效的解决方案,我将在回答部分作出回应。需要“rubygems” require 'rubygems' require 'action_mailer' ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = {

我在没有rails的情况下使用过ActionMailer2.3,在使用4.0组件时,使用actionmailer4的ruby代码不起作用。我现在有了有效的解决方案,我将在回答部分作出回应。

需要“rubygems”
    require 'rubygems'
    require 'action_mailer'

    ActionMailer::Base.delivery_method = :smtp
    ActionMailer::Base.smtp_settings = {
       :enable_starttls_auto => :true,
       :address => "smtp.gmail.com",
       :port => 587,
       :domain => "xxxx.com",
       :authentication => :plain,
       :user_name => "aaaa@xxxx.com",
       :password => "ppppp"
    }

    ActionMailer::Base.view_paths= File.dirname(__FILE__)

    class TEST_CLASS_email < ActionMailer::Base
      default( {from: "aaaa@xxxx.com"} )

      def sendemail(s_email)
        @var = 'TESTING VARIABLE PASSING TO VIEW'
        mail(
          to: "bbbb.cccc@dddd.com",
          subject:  "Testing out from ACTIONMAILER4",
          bcc:  s_email
        )
      end
    end

    def testemailpubmethod(email)
      begin
      e=TEST_CLASS_email.sendemail(email)
      if e.present?
        e.deliver_now 
      end
      rescue Exception => exception
        puts "Email:Exception Message: #{exception.message}" 
        puts "Error sending the email #{exception.backtrace.join("\n")}"
      end
    end

    testemailpubmethod("eeee@ffff.com")    


Now create the template files in the following subdirectory.
============================================================
./test_class_email/sendemail.text.erb
=====================================
    This is a text email from <%= @var %>
    -Have fun using Actionmailer4 :)
要求采取“行动” ActionMailer::Base.delivery\u方法=:smtp ActionMailer::Base.smtp\u设置={ :enable_starttls_auto=>:true, :address=>“smtp.gmail.com”, :端口=>587, :domain=>“xxxx.com”, :身份验证=>:普通, :user_name=>“aaaa@xxxx.com", :password=>“ppppp” } ActionMailer::Base.view\u path=File.dirname(\uuuu File\uuuu) 类测试\u类\u电子邮件异常 放入“电子邮件:异常消息:#{Exception.Message}” 放置“发送电子邮件时出错#{exception.backtrace.join(“\n”)}” 结束 结束 测试方法(“eeee@ffff.com") 现在在下面的子目录中创建模板文件。 ============================================================ ./test\u class\u email/sendmail.text.erb ===================================== 这是一封来自 -使用Actionmailer4:)享受乐趣