欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Rails里如何结合ExceptionNotification配置gmail账户发邮件 博客分类: Ruby GmailRailsRubySVNGoogle 

程序员文章站 2024-02-22 16:50:22
...
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.")