RoR发送电子邮件和OpenSSL

时间:2015-06-21 06:41:22

标签: ruby-on-rails ruby email

我有一些类 settings_mailer.rb 用于发送电子邮件,代码:

require 'tlsmail'

Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
    :address =>  "mail.dcs.com",
    :port =>  "587",
    :domain => 'localhost.localdomain',
    :enable_starttls_auto =>  true,
    :authentication => :login,
    :user_name => "support@dcs.com",
    :password => "dcs00001",
    :enable_starttls_auto => false
}

class Mailer < ActionMailer::Base
  default :from =>  "support@dcs.com"

  def notification(user)
    @user = user
    mail(:to =>  user.email, :subject =>  "Welcome to My Awesome Site")
  end
end

我有一些控制器 settings_controller.rb ,其中包含当前方法 get_user_settings ,并且我将通知方法称为发送测试电子邮件。 代码:

def get_user_settings
    Mailer.notification('bad.kto@gmail.com').deliver
 end

但是,如果我运行我的应用程序并调用此方法,我会抓住下一个错误:

[2015-06-21 09:34:35] [/home/tk/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.0/lib/action_dispatch/middleware/debug_exceptions.rb] [FATAL] 
NameError (uninitialized constant Appdev::SettingsController::Mailer):
  app/controllers/appdev/settings_controller.rb:251:in `get_user_settings'

为什么会发生,以及如何解决?谢谢!

2 个答案:

答案 0 :(得分:1)

我的工作代码: 的 mailer.rb

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
    :address =>  "mail.dcs.com",
    :port =>  "587",
    :domain => 'localhost.localdomain',
    :enable_starttls_auto =>  true,
    :authentication => :login,
    :user_name => "support@dcs.com",
    :password => "dcs00001",
    :enable_starttls_auto => false
}

class Mailer < ActionMailer::Base
  default :from =>  "support@dcs.com"

  def notification(user)
    mail(:to =>  user, :subject =>  "Welcome to My Awesome Site")
  end
end

<强> 2。 ../视图/邮寄者/ notification.html.rb 这是一些电子邮件html模板

并拨打我的电子邮件方式发送消息:

Mailer.notification('bad.kto@gmail.com').deliver

答案 1 :(得分:0)

  

NameError(未初始化的常量Appdev :: SettingsController :: Mailer)

错误是因为您的 文件名 settings_mailer.rb Class 名称为 Mailer 。您需要将其更改为 SettingsMailer

class SettingsMailer < ActionMailer::Base
  default :from =>  "support@dcs.com"

  def notification(user)
    @user = user
    mail(:to =>  user.email, :subject =>  "Welcome to My Awesome Site")
  end
end

此外,您还需要更改get_user_settings方法

def get_user_settings
   SettingsMailer.notification('bad.kto@gmail.com').deliver
end