Integrations · E-commerce
Validate emails at Solidus checkout and sign-up
Refuse an email address that cannot receive mail at your Solidus store's checkout and sign-up. A Ruby gem (a Rails engine) adds one validation to Spree::Order and to your customer account class (Spree.user_class, for example Spree::User of solidus_auth_devise), so the address is checked with our API whenever it is set or changed: the guest's address at checkout, a sign-up, an email change, an order made in the admin or through the API. By default only a definite invalid is refused: the person sees one sentence on the email field, as Solidus shows its own errors, and can correct the address.
It never refuses an address because our side could not answer. No key, a timeout, a network error, a rate limit or any error answer lets the address through, with one warning line in your logger. Only a definite answer refuses.
It cannot check an address Solidus writes without validations. Solidus copies a signed-in customer's account email onto their order without running validations, so that address is checked on the account, when it signs up or changes its email (below). Accounts made before you installed the gem were not checked.
Status: set up from this page. The gem is not on RubyGems and is not listed among Solidus's extensions: you download it here and add it to your Gemfile from a folder of your app. Its tests run against stand-ins for Solidus's classes; it has not yet been tried in a real Solidus store, so try it on a development copy of your store first.
Download it: email-validation-solidus-1.0.0.zip holds the gem's folder email-validation-solidus/: its gemspec, its code in lib/, a short README, the CHANGELOG and the MIT licence.
What you need
- An API key of ours from the API keys page with Validate addresses (on by default), with a daily credit cap to limit what a busy store can spend. Keep it on your server.
- A Solidus 4 store on Rails 7.2 to 8.1, with Ruby 3.2 or newer.
- Bundler installs the official Ruby client library (
email-validation-sdk1.1 or a later 1.x) from RubyGems. The gem adds no Solidus gem of its own: it works with the Solidus your store already has.
Our key goes only to our two API hosts, and the client library never follows a redirect. Neither the address nor the key is ever logged.
Set it up
Unzip the download into your app's
vendor/folder, so that you havevendor/email-validation-solidus/with the gemspec in it. Bundler installs a gem from a folder that holds its.gemspec, and reads a relative path from the folder of your Gemfile (Bundler's Gemfile guide).Add it to your Gemfile, after
solidus(andsolidus_auth_deviseif you use it), as Solidus's guide to extensions asks, and runbundle install:gem "email-validation-solidus", path: "vendor/email-validation-solidus"There is no generator to run and no migration: the gem stores nothing.
Set your key in the server's environment as
EV_API_KEY, or read it from your credentials in the initializer below. Never put it in code.Restart the app. When it loads, the engine adds the check to
Spree::OrderandSpree.user_class(and again after each code reload in development).
Every setting is optional. To change one, add config/initializers/email_validation.rb:
EmailValidation::Solidus.configure do |config|
config.api_key = Rails.application.credentials.ev_api_key # default: ENV["EV_API_KEY"]
config.logger = Rails.logger # the one warning line when a check cannot finish
config.stop_on = %w[invalid disposable] # also refuse disposable addresses
config.region = "eu" # addresses validated in the EU, their results stored there
endHow it works
Only a new or changed email. The check runs when a record is new or its
emailchanges, never on the many other saves an order goes through at checkout. A record checked once for a value is not sent again for it in a row (valid?thensavesends one request). A checkout step that fails for another reason leaves the email unsaved, so the next try checks it again.Nothing sent when Solidus already answers. A blank or malformed email that Solidus refuses itself, a cart with no email yet, an order whose email is its own customer account's (while accounts are checked) and a soft-deleted record (
discarded?, ordeleted_atset) are not sent. solidus_auth_devise scrambles a deleted account's email and saves it; that save goes through.One request, one decision. The address goes to our API once, never retried. An answer whose status is in
stop_on(onlyinvalidby default) adds an error onemailwith a sentence for the person, which the checkout and the admin show as they show Solidus's own:This address cannot receive email. Please check it for typos.
Our own
invalidsentence gets the API's "Did you mean …?" suggestion when there is one. The error's type isemail_invalid(email_and the verdict) inorder.errors.details[:email]. Lists made witherrors.full_messagesput the field's name in front ("Email This address …"); pass your ownmessagesto word it for that.Fails open. When the check cannot finish, the address goes through and one warning line goes to your
loggerwith why, our error code, the HTTP status and the request id. Withfail_openset tofalse, the field gets "We could not check this address right now. Please try again in a moment." instead. Anunknownanswer that means our side ran out of time (reasontimeoutorinternal_error) never refuses, even withunknowninstop_on.A pause after a rate limit. After a 429 or 503 answer (other than
key_daily_cap), no check is sent until itsRetry-After(30 seconds without one, 300 at most), for the whole process; addresses pass meanwhile.
Signed-in customers. Solidus copies the account's email onto a signed-in customer's order without validations, so with check_users on (the default) that address is checked on the account instead. With check_users set to false, a signed-in customer's order email is in general not checked either; only an email typed on the checkout is.
EmailValidation::Solidus.check(email) checks an address you already have and returns an outcome with stop?, status, reason, did_you_mean, request_id, verdict and message (never the address). EmailValidation::Solidus.install!(order_class:, user_class:) adds the check to other classes with an email attribute.
Try it free first
First on your machine, with your key in EV_API_KEY, from your app's folder:
bin/rails runner 'o = EmailValidation::Solidus.check("no_mailbox@sandbox.email-validation.org"); puts [o.stop?, o.status, o.reason].join(" ")'
# prints: true invalid no_mailboxfalse with no status means the check did not finish (no key in EV_API_KEY, for example); set config.logger to see why. Sandbox checks cost no credit. That domain receives no mail, so send it nothing: never type a sandbox address into your store's checkout or sign-up, where an address that gets through can be saved and mailed.
Then on a development copy of your store: go through a guest checkout with your own email address. It passes, and that check uses one credit unless the answer is unknown.
Options
| Setting | Default | What it does |
|---|---|---|
api_key | EV_API_KEY | Our API key (Validate addresses), on the server only. Missing: nothing is checked. |
region | EV_REGION, else us | eu checks the address in the EU and stores its result there (same key and credits). |
stop_on | %w[invalid] | Statuses that refuse: invalid, disposable, catch_all, unknown. [] refuses nothing; valid never refuses. |
fail_open | true | false: a check that cannot finish refuses with the "unavailable" sentence. |
check_orders | true | false: orders are not checked. |
check_users | true | false: customer accounts are not checked. |
timeout | 5 | Seconds our API may spend on the check (5 to 15); keep client_timeout a few seconds above it. |
client_timeout | 8.0 | Seconds the request waits for the answer. |
messages | ours | Your sentences by verdict: invalid, disposable, catch_all, unknown, unavailable. |
logger | none | Anything with warn(String), such as Rails.logger, for the one warning line. |
transport | the client library's | A transport of the client library (a proxy). |
Credits and limits
Each check uses one credit; unknown results and sandbox addresses are free. The gem keeps no answers between requests: an email set again on another record (the same customer's next guest order, say) is checked again.
The request runs while the record is being saved, so a slow answer keeps that save waiting for up to client_timeout seconds. Imports, seeds and scripts that save orders or accounts with an email through validations are checked too, one request per address; set check_users or check_orders to false in that process to skip them (the setting is for the whole process).
It fails open: no key, a timeout, a network error, a rate limit or any error answer lets the address through.