Skip to content

Commit 4b32982

Browse files
Merge pull request #66 from francescob/main
configuration for a subject prefix for email
2 parents bd04b42 + 77f1c58 commit 4b32982

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ You can configure Solid Errors via the Rails configuration object, under the `so
160160
* `sends_email` - Whether or not to send emails when an error occurs. See [Email notifications](#email-notifications) for more information.
161161
* `email_from` - The email address to send a notification from. See [Email notifications](#email-notifications) for more information.
162162
* `email_to` - The email address(es) to send a notification to. See [Email notifications](#email-notifications) for more information.
163+
* `email_subject_prefix` - Prefix added to the subject line for email notifications. See [Email notifications](#email-notifications) for more information.
163164

164165
#### Database Configuration
165166

@@ -208,23 +209,25 @@ end
208209

209210
#### Email notifications
210211

211-
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.
212+
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.
212213

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

215216
```ruby
216217
ENV["SOLIDERRORS_SEND_EMAILS"] = true # defaults to false
217218
ENV["SOLIDERRORS_EMAIL_FROM"] = "[email protected]" # defaults to "[email protected]"
218219
ENV["SOLIDERRORS_EMAIL_TO"] = "[email protected]" # no default, must be set
220+
ENV["SOLIDERRORS_EMAIL_SUBJECT_PREFIX"] = "[Application name][Environment]" # no default, optional
219221
```
220222

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

223225
```ruby
224-
# Set authentication credentials for Solid Errors
226+
# Set authentication credentials and optional subject prefix for Solid Errors
225227
config.solid_errors.send_emails = true
226228
config.solid_errors.email_from = "[email protected]"
227229
config.solid_errors.email_to = "[email protected]"
230+
config.solid_errors.email_subject_prefix = "[#{Rails.application.name}][#{Rails.env}]"
228231
```
229232

230233
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.

app/mailers/solid_errors/error_mailer.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ class ErrorMailer < ActionMailer::Base
44
def error_occurred(occurrence)
55
@occurrence = occurrence
66
@error = occurrence.error
7-
7+
subject = "#{@error.severity_emoji} #{@error.exception_class}"
8+
if SolidErrors.email_subject_prefix.present?
9+
subject = [SolidErrors.email_subject_prefix, subject].join(" ").squish!
10+
end
811
mail(
9-
subject: "#{@error.severity_emoji} #{@error.exception_class}",
12+
subject: subject,
1013
from: SolidErrors.email_from,
1114
to: SolidErrors.email_to
1215
)

lib/solid_errors.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module SolidErrors
1212
mattr_writer :send_emails
1313
mattr_writer :email_from
1414
mattr_writer :email_to
15+
mattr_writer :email_subject_prefix
1516

1617
class << self
1718
# use method instead of attr_accessor to ensure
@@ -37,5 +38,9 @@ def email_from
3738
def email_to
3839
@email_to ||= ENV["SOLIDERRORS_EMAIL_TO"] || @@email_to
3940
end
41+
42+
def email_subject_prefix
43+
@email_subject_prefix ||= ENV["SOLIDERRORS_EMAIL_SUBJECT_PREFIX"] || @@email_subject_prefix
44+
end
4045
end
4146
end

0 commit comments

Comments
 (0)