Search code examples
ruby-on-railsexception-notification

How do i send an email using exception_notifier gem manually?


I am trying the following code:

ExceptionNotifier::Notifier.exception_notification(env, exception).deliver

But this message keep on appearing:

A sender (Return-Path, Sender or From) required to send a message

Any idea why this is happening and how I can get around it?


Solution

  • You likely have not configured the gem in an initializer. In my controller action for notifying me of an exception I have the following

    ExceptionNotifier::Notifier.exception_notification(
      request.env, 
      env["action_dispatch.exception"]
    ).deliver
    

    I have the following in config/initializers/exception_notifier.rb

    if Rails.env.production?
      MyApp::Application.config.middleware.use ExceptionNotifier,
        email_prefix:         "[#{App.domain.pretty}] ",
        sender_address:       App.email.noreply,
        exception_recipients: App.email.exceptions,
        ignore_exceptions:    ExceptionNotifier.default_ignore_exceptions,
        normalize_subject:    true
    end
    

    MyApp, and App.____ should all be replaced by your own values.