Class: MailGate::Filter
- Inherits:
-
Object
- Object
- MailGate::Filter
- Defined in:
- lib/mail_gate/filter.rb
Overview
MailGate class restricts email delivery to non-whitelisted emails.
Each element within the whitelist needs to be a Regex object.
To use MailGate within a Rails project, configure a specific environment file or global configuration using the mail_gate delivery method:
config.action_mailer.delivery_method = :mail_gate
config.action_mailer.mail_gate_settings = {
:whitelist => /evil.com|[email protected]/
}
If you aren’t using MailGate within a Rails application, you can still configure Mail to use it as it’s default delivery_method.
Mail.defaults do
delivery_method MailGate::Filter, :whitelist => /application.com/
end
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#whitelist ⇒ Object
The Regexp to compare emails against.
Instance Method Summary collapse
-
#deliver!(mail) ⇒ Object
Public: Filter out recipients who may match the whitelist regex.
-
#initialize(settings = {}) ⇒ Filter
constructor
A new instance of Filter.
Constructor Details
#initialize(settings = {}) ⇒ Filter
Returns a new instance of Filter.
27 28 29 30 31 32 33 |
# File 'lib/mail_gate/filter.rb', line 27 def initialize(settings = {}) self.settings = settings @whitelist = Regexp.new(settings.fetch(:whitelist, /.*/)) delivery_method = settings.fetch(:delivery_method, :test) delivery_settings = settings.fetch(:delivery_settings, {}) @delivery_method = Mail::Configuration.instance.lookup_delivery_method(delivery_method).new(delivery_settings) end |
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
22 23 24 |
# File 'lib/mail_gate/filter.rb', line 22 def settings @settings end |
#whitelist ⇒ Object
The Regexp to compare emails against.
25 26 27 |
# File 'lib/mail_gate/filter.rb', line 25 def whitelist @whitelist end |
Instance Method Details
#deliver!(mail) ⇒ Object
Public: Filter out recipients who may match the whitelist regex. If no emails are present after being filtered, don’t deliver the email.
mail - Mail object containing headers and body.
Returns instance of Mail::Message.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mail_gate/filter.rb', line 41 def deliver!(mail) unless settings[:append_emails] == false original_emails = email_list(mail) end %w{ to cc bcc }.each do |field| mail.send(:"#{field}=", filter_emails(mail.send(field))) end unless settings[:append_emails] == false new_emails = original_emails - email_list(mail) if !new_emails.empty? mail.body = "#{mail.body}\n\nExtracted Recipients: #{new_emails.join(', ')}" end end if settings[:subject_prefix] mail.subject = settings[:subject_prefix] + mail.subject end @delivery_method.deliver!(mail) unless mail.to.blank? mail end |