module Roda::RodaPlugins::ErrorEmail

  1. lib/roda/plugins/error_email.rb

The error_email plugin adds an error_email instance method that send an email related to the exception. This is most useful if you are also using the error_handler plugin:

plugin :error_email, to: '', from: ''
plugin :error_handler do |e|
  'Internal Server Error'

It is similar to the error_mail plugin, except that it uses net/smtp directly instead of using the mail library. If you are not already using the mail library in your application, it makes sense to use error_email instead of error_mail.



The From address to use in the email (required)


A hash of additional headers to use in the email (default: empty hash)


The SMTP server to use to send the email (default: localhost)


A prefix to use in the email’s subject line (default: no prefix)


The To address to use in the email (required)

The subject of the error email shows the exception class and message. The body of the error email shows the backtrace of the error and the request environment, as well the request params and session variables (if any). You can also call error_email with a plain string instead of an exception, in which case the string is used as the subject, and no backtrace is included.

Note that emailing on every error as shown above is only appropriate for low traffic web applications. For high traffic web applications, use an error reporting service instead of this plugin.


Public Class

  1. configure


DEFAULTS = { :headers=>OPTS, :host=>'localhost', # :nocov: :emailer=>lambda{|h| Net::SMTP.start(h[:host]){|s| s.send_message(h[:message], h[:from], h[:to])}}, # :nocov: :default_headers=>lambda do |h, e| subject = if e.respond_to?(:message) "#{e.class}: #{e.message}" else e.to_s end {'From'=>h[:from], 'To'=>h[:to], 'Subject'=>"#{h[:prefix]}#{subject}"} end, :body=>lambda do |s, e| format = lambda{|h|{|k, v| "#{k.inspect} => #{v.inspect}"}.sort.join("\n")} begin params = s.request.params params = (format[params] unless params.empty?) rescue params = 'Invalid Parameters!' end message = message << <  

Public Class methods

configure(app, opts=OPTS)

Set default opts for plugin. See ErrorEmail module RDoc for options.

[show source]
    # File lib/roda/plugins/error_email.rb
106 def self.configure(app, opts=OPTS)
107   email_opts = app.opts[:error_email] ||= DEFAULTS
108   email_opts = email_opts.merge(opts)
109   email_opts[:headers] = email_opts[:headers].dup
110   unless email_opts[:to] && email_opts[:from]
111     raise RodaError, "must provide :to and :from options to error_email plugin"
112   end
113   app.opts[:error_email] = email_opts
114   app.opts[:error_email][:headers].freeze
115   app.opts[:error_email].freeze
116 end