Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block email to certain email addresses or domain?

We have an application built in rails 3.2.8. We are sending emails to customers. I want to block certain emails addressses. Basically mailer just ignore those particular email addresses.

For example: My company name is abc and I dont want to send emails to all my employees ie [email protected] or [email protected] ie *@abc.com

How can I do this?

PS: I am using sendgrid, they are not providing anything like this.

EDIT:

Placing this code in initializers directory:

 class EmailAddressFilter
  def self.delivering_email(message)
    message.perform_deliveries = false

  end
end

ActionMailer::Base.register_interceptor(EmailAddressFilter)

Should block all the emails. But still I can see emails in my development log.

PS: I have restarted my server.

like image 982
Mohit Jain Avatar asked Jan 01 '26 03:01

Mohit Jain


1 Answers

In a file in config/initializers you can add something like this

class EmailAddressFilter
  def self.delivering_email(message)
    # permit or deny the message using its "to", "body" etc properties
    # note message.to is an array (multiple emails)
    message.perform_deliveries = Email.whitelisted?(message.to)
  end
end

ActionMailer::Base.register_interceptor(EmailAddressFilter)
like image 89
deefour Avatar answered Jan 02 '26 19:01

deefour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!