`

Rails里如何结合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.xlsuite.org/trunk/vendor/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(mengwade@gmail.com),   
    :email_prefix   => "51hs",   
    :recipients     => %w(mengwade@gmail.com),   
    :smtp_settings  => {   
                        :address => "smtp.gmail.com",   
                        :port => 587,   
                        :domain => "51hs.net",   
                        :authentication => :login,   
                        :user_name => "mengwade@gmail.com",   
                        :password => "mengwade@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("mengwade@gmail.com", "email title", "email data.")  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics