Integrations · E-commerce
Validate emails at Spree Commerce checkout and sign-up
Refuse an email address that cannot receive mail at your Spree Commerce store's checkout and sign-up. A Ruby gem (a Rails engine) adds one validation to Spree's checkout model and to its customer class, so the address is checked with our API whenever it is set or changed: the guest's address at checkout (the storefront, the Storefront or Store API), 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 Spree 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 Spree writes without validations. Spree copies a signed-in customer's account email onto their checkout 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 Spree's integrations: you download it here and add it to your Gemfile from a folder of your app. Its tests run against stand-ins for the classes of Spree 4.10, 5.6 and 6.0; it has not yet been tried in a real Spree store, so try it on a development copy of your store first.
Download it: email-validation-spree-1.0.0.zip holds the gem's folder email-validation-spree/: 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 Spree 4.10, 5.x or 6.0 store, 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 Spree gem of its own: it works with the Spree your store already has.
What it checks on each version:
| Item | Spree 4.10 and 5.x | Spree 6.0 |
|---|---|---|
| Where the guest types the email | Spree::Order (a cart is an order in the cart state) | Spree::Cart |
| Orders | the same Spree::Order | Spree::Order made in the admin (one completed from a cart is not checked again) |
| Customer accounts | Spree.user_class (for example Spree::User of spree_auth_devise) | Spree.customer_class |
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 the
vendor/folder of your Rails app, so that you havevendor/email-validation-spree/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 and run
bundle install:gem "email-validation-spree", path: "vendor/email-validation-spree"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, after your initializers, the engine adds the check to Spree's classes (and again after each code reload in development), so the customer class
config/initializers/spree.rbnames is the one checked.
A Spree 6 project made with create-spree-app runs a prebuilt Docker image that cannot load new gems. From the project's folder, run npx spree eject once, so that the backend builds from its server/ folder, then do the steps above in server/: unzip into server/vendor/, add the line to server/Gemfile and run npx spree exec bundle install. Put EV_API_KEY in the project's .env, then stop npx spree dev with Ctrl+C and run it again: a new gem or a changed .env needs that, not spree restart (Spree's CLI commands).
Every setting is optional. To change one, add config/initializers/email_validation.rb:
EmailValidation::Spree.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
endInside module EmailValidation, Spree means this gem: write Spree's own classes as ::Spree there.
How 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 a checkout goes through. 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 Spree already answers. A blank or malformed email that Spree refuses itself, a cart with no email yet, a checkout whose email is its own customer account's (while accounts are checked), on Spree 6 an order whose email is the cart's it was completed from (checked on the cart), and a soft-deleted record (
deleted_atset, ordiscarded?) are not sent. spree_auth_devise scrambles a deleted account's email and saves it; that save goes through.Answered where the email is typed. On Spree 4 and 5, Spree checks an order's email format only from a later checkout step; this check answers at the
addressstep already. What is not an address is never sent: Spree refuses it at its step.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, the API and the admin show as they show Spree'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_messages(Spree 6's Store API answers with those) put 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. Spree copies the account's email onto a signed-in customer's order or cart 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 checkout email is not checked either; only an email typed on the checkout is.
EmailValidation::Spree.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::Spree.install!(order_class:, cart_class:, user_class:) adds the check to other classes with an email attribute. Newsletter sign-ups are not checked by default; in a config.to_prepare block, EmailValidation::Spree.install!(order_class: nil, cart_class: nil, user_class: Spree::NewsletterSubscriber) checks them too, under check_users.
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::Spree.check("no_mailbox@sandbox.email-validation.org"); puts [o.stop?, o.status, o.reason].join(" ")'
# prints: true invalid no_mailboxIn a Spree 6 project made with create-spree-app, put npx spree exec in front of bin/rails runner. false 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: the checkout (orders, and carts on Spree 6) is 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 checkout, say) is checked again.
The request runs inside the save's database transaction, so a slow answer holds a database connection for up to client_timeout seconds.
Imports, seeds and scripts that save accounts, carts or orders with an email through validations are checked too, one request per address, in that process. Spree 5 and 6's admin customer import saves each row with validations, so a refused row fails with the sentence, and after a 429 the rest of the import goes through unchecked until the pause ends. Setting check_users to false for an import turns the check off for the whole process, the live store's requests in it included.
It fails open: no key, a timeout, a network error, a rate limit or any error answer lets the address through.