🗑️ nondisposable - Block disposable email addresses from signing up to your Rails app
nondisposable is a Ruby gem that prevents users from signing up to your Rails app with disposable email addresses.
Simply add to your User model:
validates :email, nondisposable: trueThat's it! You're done.
The gem also provides a job you can run daily to keep your disposable domain list up to date.
Installation
Add this line to your application's Gemfile:
gem 'nondisposable'And then execute:
bundle installAfter installing the gem, run the installation generator:
rails generate nondisposable:installThis will create the necessary migration file, initializer, and a job for scheduled updates. Run the migration:
rails db:migrateFinally, populate the initial list of disposable domains:
Nondisposable::DomainListUpdater.updateUsage
To use nondisposable in your models, simply add the validation:
class User < ApplicationRecord
validates :email, nondisposable: true
endYou can customize the error message:
class User < ApplicationRecord
validates :email, nondisposable: { message: "is a disposable email address, please use a permanent email address." }
endThe validation works seamlessly with other Rails validations:
class User < ApplicationRecord
validates :email,
presence: true,
format: { with: URI::MailTo::EMAIL_REGEXP },
nondisposable: true
endIf you're validating a different attribute name:
class User < ApplicationRecord
validates :backup_email, nondisposable: true
endConfiguration
You can customize the gem's behavior by creating an initializer:
# config/initializers/nondisposable.rb
Nondisposable.configure do |config|
config.error_message = "provider is not allowed. Please use a non-disposable email address."
config.additional_domains = ['custom-disposable-domain.com']
config.excluded_domains = ['false-positive-domain.com']
endDirect Check
You can also check if an email is disposable directly:
Nondisposable.disposable?('user@example.com') # => false
Nondisposable.disposable?('user@disposable-email.com') # => trueUpdating disposable domains
To manually update the list of disposable domains, run:
Nondisposable::DomainListUpdater.updateIt's important you keep your disposable domain list up to date. nondisposable will read from the latest version of the disposable-email-domains list, which is typically updated every few days.
For this, nondisposable provides you with an Active Job (DisposableEmailDomainListUpdateJob) that you can use to schedule daily updates. How you do that, exactly, depends on the queueing system you're using.
If you're using solid_queue (the Rails 8 default), you can easily add it to your schedule in the config/recurring.yml file like this:
production:
refresh_disposable_domains:
class: DisposableEmailDomainListUpdateJob
queue: default
schedule: every day at 3am US/PacificDevelopment
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/rameerez/nondisposable. Our code of conduct is: just be nice and make your mom proud of what you do and post online.
License
The gem is available as open source under the terms of the MIT License.