2008-06-19

Rails里如何结合ExceptionNotification配置gmail账户发邮件

关键字: exceptionnotification gmail
1,安装ExceptionNotification
ruby script\plugin install http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/

光安装这个插件是不能利用gmail发送邮件的,因为gmail需要https,所以还需要安装一个插件

2,安装action_mailer_tls
ruby script/plugin install http://svn.nanorails.com/plugins/action_mailer_tls  


3,修改exception_notifier.rb,添加一个方法
# line 40
def exception_notification
  # ...
end

def sys_email(recipients, subject, data={})
  subject    subject
  recipients recipients
  from       sender_address
  body       data
end


4,config目录写一个sys_config.rb文件
class SysConfig

  EXCEPTION_NOTIFIER = {
    :delivery_method => :smtp,
    :sender_address => %w(beyondrails@gmail.com),
    :email_prefix   => "BeyondRails",
    :recipients     => %w(hideto.bj@gmail.com),
    :smtp_settings  => {
                        :address => "smtp.gmail.com",
                        :port => 587,
                        :domain => "beyondrails.com",
                        :authentication => :login,
                        :user_name => "beyondrails@gmail.com",
                        :password => "beyondrails@gmail.com的密码"
                          },

  }

end


5,修改environment.rb
# ExceptionNotifier settings
ExceptionNotifier.sender_address =  SysConfig::EXCEPTION_NOTIFIER[:sender_address]
ExceptionNotifier.email_prefix = SysConfig::EXCEPTION_NOTIFIER[:email_prefix]
ExceptionNotifier.exception_recipients = SysConfig::EXCEPTION_NOTIFIER[:recipients]
ActionMailer::Base.delivery_method = SysConfig::EXCEPTION_NOTIFIER[:delivery_method]
ActionMailer::Base.smtp_settings = SysConfig::EXCEPTION_NOTIFIER[:smtp_settings]
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.default_charset = "utf-8"


好了!,可以在ruby script\console下面试试发送一封email:
 ExceptionNotifier.deliver_sys_email("hideto.bj@gmail.com", "email title", "email data.")
评论
kfw5313 2008-09-02   回复
D:\InstantRails2.0\rails_apps\gmailtest>ruby script/console
Loading development environment (Rails 2.0.2)
D:/InstantRails2.0/rails_apps/gmailtest/config/environment.rb:16:NameError: uninitialized constant ExceptionNotifier
D:/InstantRails2.0/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:88:NoMethodError: undefined method `cattr_accessor' for ActionController::Dispatcher:Class
D:/InstantRails2.0/rails_apps/gmailtest/app/controllers/application.rb:4:NameError: uninitialized constant ActionController::Base
请问之前遇到过这个错误码  知道如何解决吗
yangzhihuan 2008-06-28   回复
收藏了,小工具,好东西.
发表评论

您还没有登录,请登录后发表评论

hideto
搜索本博客
我的相册
A6bdc31c-c66e-468e-961e-9cc721e82adc-thumb
screenshot
共 1 张
存档
最新评论