Rails · Ruby
Block disposable email in Rails
Custom ActiveModel validator on User#email. Works with Devise, Sorcery, or hand-rolled signup controllers.
The code
# app/validators/not_disposable_email_validator.rb
class NotDisposableEmailValidator < ActiveModel::EachValidator
CHECK_URL = "https://api.piisend.com/api/v1/check/email"
def validate_each(record, attribute, value)
return if value.blank?
begin
response = HTTParty.get(
"#{CHECK_URL}/#{URI.encode_www_form_component(value)}",
headers: {
"Authorization" => "Bearer #{ENV['PIISEND_API_KEY']}",
},
timeout: 5,
)
# reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
if response.success? && response.parsed_response['recommendation'] == 'block'
record.errors.add(attribute, "Please use a permanent email address.")
end
rescue StandardError
# fail open
end
end
end
# app/models/user.rb
class User < ApplicationRecord
validates :email, not_disposable_email: true
endAPI key
Set `PIISEND_API_KEY` in credentials or ENV — use `dotenv-rails` locally.
Fail-open vs fail-closed
Rescue block skips validation on errors.
Pair with verification
Add the validator before Devise's `:validatable` or inside a custom RegistrationsController.
FAQ
Where does the Piisend API key go in Rails?
Set `PIISEND_API_KEY` in credentials or ENV — use `dotenv-rails` locally.
What happens if Piisend is slow or down?
Rescue block skips validation on errors.
How does this fit with the rest of Rails signup?
Add the validator before Devise's `:validatable` or inside a custom RegistrationsController.
What does this snippet block?
Addresses on disposable domains: about 10,900 listed domains, plus new ones whose mail servers belong to a known throwaway service. Free providers such as Gmail and forwarding aliases such as duck.com pass.
Can I get a full allow / review / block verdict instead?
Yes, and we recommend it. Call GET /api/v1/check/email/{email} instead. Both endpoints need an API key with the check:read scope and use one check from your monthly allowance per call, but the check endpoint also returns forwarding relays, phishing lists, missing MX records, new domains, typo suggestions and an allow / review / block verdict.
Send verification email with Piisend
After blocking disposable addresses, send OTP and verification mail through the same Piisend account. One API key for intelligence checks and transactional sends.