Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configuration for a subject prefix for email #66

Merged
merged 9 commits into from
Oct 23, 2024
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ You can configure Solid Errors via the Rails configuration object, under the `so
* `sends_email` - Whether or not to send emails when an error occurs. See [Email notifications](#email-notifications) for more information.
* `email_from` - The email address to send a notification from. See [Email notifications](#email-notifications) for more information.
* `email_to` - The email address(es) to send a notification to. See [Email notifications](#email-notifications) for more information.
* `email_subject_prefix` - Prefix added to the subject line for email notifications. See [Email notifications](#email-notifications) for more information.

#### Database Configuration

Expand Down Expand Up @@ -208,23 +209,25 @@ end

#### Email notifications

Solid Errors _can_ send email notifications whenever an error occurs, if your application has ActionMailer already properly setup to send emails. However, in order to activate this feature you must define the email address(es) to send the notifications to. Optionally, you can also define the email address to send the notifications from (useful if your email provider only allows emails to be sent from a predefined list of addresses) or simply turn off this feature altogether.
Solid Errors _can_ send email notifications whenever an error occurs, if your application has ActionMailer already properly setup to send emails. However, in order to activate this feature you must define the email address(es) to send the notifications to. Optionally, you can also define the email address to send the notifications from (useful if your email provider only allows emails to be sent from a predefined list of addresses) or simply turn off this feature altogether. You can also define a subject prefix for the email notifications to quickly identify the source of the error.

There are two ways to configure email notifications. First, you can use environment variables:

```ruby
ENV["SOLIDERRORS_SEND_EMAILS"] = true # defaults to false
ENV["SOLIDERRORS_EMAIL_FROM"] = "errors@myapp.com" # defaults to "solid_errors@noreply.com"
ENV["SOLIDERRORS_EMAIL_TO"] = "devs@myapp.com" # no default, must be set
ENV["SOLIDERRORS_EMAIL_SUBJECT_PREFIX"] = "[Application name][Environment]" # no default, optional
```

Second, you can set the values via the configuration object:

```ruby
# Set authentication credentials for Solid Errors
# Set authentication credentials and optional subject prefix for Solid Errors
config.solid_errors.send_emails = true
config.solid_errors.email_from = "errors@myapp.com"
config.solid_errors.email_to = "devs@myapp.com"
config.solid_errors.email_subject_prefix = "[#{Rails.application.name}][#{Rails.env}]"
```

If you have set `send_emails` to `true` and have set an `email_to` address, Solid Errors will send an email notification whenever an error occurs. If you have not set `send_emails` to `true` or have not set an `email_to` address, Solid Errors will not send any email notifications.
Expand Down
7 changes: 5 additions & 2 deletions app/mailers/solid_errors/error_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ class ErrorMailer < ActionMailer::Base
def error_occurred(occurrence)
@occurrence = occurrence
@error = occurrence.error

subject = "#{@error.severity_emoji} #{@error.exception_class}"
if SolidErrors.email_subject_prefix.present?
subject = [SolidErrors.email_subject_prefix, subject].join(" ").squish!
end
mail(
subject: "#{@error.severity_emoji} #{@error.exception_class}",
subject: subject,
from: SolidErrors.email_from,
to: SolidErrors.email_to
)
Expand Down
5 changes: 5 additions & 0 deletions lib/solid_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module SolidErrors
mattr_writer :send_emails
mattr_writer :email_from
mattr_writer :email_to
mattr_writer :email_subject_prefix

class << self
# use method instead of attr_accessor to ensure
Expand All @@ -37,5 +38,9 @@ def email_from
def email_to
@email_to ||= ENV["SOLIDERRORS_EMAIL_TO"] || @@email_to
end

def email_subject_prefix
@email_subject_prefix ||= ENV["SOLIDERRORS_EMAIL_SUBJECT_PREFIX"] || @@email_subject_prefix
end
end
end