-
Create solutions that bring maximum success and value under our expert product management guidanceEnsure your product idea viability and get the right market and development strategies to succeedGet a product that directly addresses your customer needs and matches the goals of your businessBring your customers an exceptional user experience and make your product stand out with our designGet designs that amplify your product features with clear, intuitive, and sales-boosting user experienceNurture customer loyalty, boost sales, and expand your market presence with a stand-out mobile appGet higher customer satisfaction, more conversions, and better competitiveness with our UX auditCreate a custom software solution to unlimit your business capabilitiesStart and grow your startup fast or effectively renovate your existing solution with our RoR expertsExtend your product line quicker and cheaper by creating a secure, reliable, and flexible API solutionGet expert technical assistance in building real-time React-based web applications and SPAsBuild a high-performing web application with our Vue.js development company.Create robust native or cross-platform mobile apps for your business with our expert assistanceGet native-like mobile apps for both Android and iOS using the best cross-platform technologiesCreate native iOS, iPadOS, and tvOS applications to cover any of the desired Apple user segmentsCreate 99.9% crash-free native Android applications for any available device and screen typesEnsure the top quality and bug-free performance of your products by fixing all issues at early stagesConsult our experts to build an efficient quality assurance strategy for your productEnable full lifecycle software testing services to detect and fix all product issues at the earliest stageReveal all non-typical performance, security, and usability issues with our manual QA servicesBoost your overall software development velocity with our robust automation testing solutionEnsure the usability, security, performance, compliance, and compatibility of your web appsProvide your customers with mobile apps free from any usability, security, and performance issuesUse our functional testing services to ensure every product feature works as expected in all scenarios.Ensure a seamless user experience across all digital environments with our compatibility testing servicesMake technologies work for your business growth and choose the right ones to achieve specific goalsImprove app performance, setup continuous delivery, cut infrastructure costs with our DevOps servicesKeep your product stably up, running and get timely feature upgrades with our maintenance servicesClean your software from code issues and uncover possible improvements to boost its performanceEnsure security of your healthcare products and achieve HIPAA compliance with our expert assistance
-
Multi-Vendor Marketplace Online Store Custom Marketplace Telemedicine Software Chat App Custom Booking System Video Conferencing For Enterprise For StartupsBuild a custom multi-vendor marketplace fast and cost-efficiently using our MarketAge solutionLaunch a custom B2B marketplace for any type of products with MarketAgeLaunch a unique, custom-functional B2C marketplace at minimum effort with MarketAge white-lable productReduce costs to build an easy-to-use and reliable C2C marketplace using our MarketAge solutionCreate an online store with unique design and features at minimal cost using our MarketAge solutionGet a unique, scalable, and cost-effective online marketplace with minimum time to marketGet a cost-efficient, HIPAA-compliant telemedicine solution tailored to your facility's requirementsGet a customizable chat solution to connect users across multiple apps and platformsImprove your business operations and expand to new markets with our appointment booking solutionAdjust our video conferencing solution for your business needsScale, automate, and improve business processes in your enterprise with our custom software solutionsTurn your startup ideas into viable, value-driven, and commercially successful software solutions
-
Streamline and scale your e-commerce business with a custom platform tailored to your product segmentsAutomate, scale, secure your financial business or launch innovative Fintech products with our helpCut paperwork, lower operating costs, and expand yout market with a custom e-learning platformUpgrade your workflow, enter e-health market, and increase marketability with the right custom software
-
Discover our software engineering culture, what principles we follow to make our clients succeedOur BA office helps clients choose the right development strategy and get maximum value at minimum riskFind out how we manage development risks, ensure on-time delivery, and prevent budget overrunsWe create clear, intuitive, and functional designs to solve specific business problems of our clientsSee what techniques and principles we follow to engineer top-tier software products at RubyGarageSee how our QA office ensures zero usability and functional issues in every product we deliver to clientsDiscover more of RubyGarage’s culture, values, and strengthsDevelop your product in a clear workflow aimed to save your time and budget and boost the qualityJoin our team to build a successful career in software development. See open positions at RubyGarage
- Case Studies
- Blog
How to Validate an Email with the Truemail Ruby Gem
Email validation can be a challenging task. You can use different approaches to validate an email address but all of them have to comply with the best practices to provide proper email validation. In this article, we shed some light on the 3 different email validation techniques and present you our gem that includes these techniques and allows you to automate email validation processes.
Why do we need to validate emails?
Email validation comes in handy when you’re building a product for the marketing sphere or that has marketing features. It’s especially important when sending a large number of emails. Here are three benefits that can persuade you to add email validation functionality to your product.
#1 Increases delivery rates
Email validation will help you remove invalid email addresses from your list. This will improve email deliverability, which means marketing campaigns will be more effective and cheaper.
#2 Helps you maintain a high sender reputation
Mailboxes rate senders by different metrics to form their reputation score. If your product sends emails en masse, you have to keep your sender score high. When a sender score is low, emails automatically go to spam or even get blocked.
One of the metrics that mailboxes track is the number of invalid email addresses a sender mails things to. More invalid emails mean a lover reputation.
Email validation allows you to eliminate invalid emails and keep your sender reputation high.
#3 Improves the conversion rate
A better sender score also means that more of your emails will arrive to users’ inboxes, more people will open them, and you’ll get more clicks and better performance.
Email validation can cut business expenses significantly and improve the results of marketing campaigns.
To validate an email properly, you need to perform regex, MX, and SMTP validation one by one.
Let’s take a look at these three email validation techniques separately and then check out how we implemented all of them in our gem.
Email validation techniques
1. Regex validation
Regex is the first, lowest-level email validation method. It’s based on checking the email address via a regex pattern.
A typical email address consists of three parts: a username, the @ symbol, and a domain name.
USER = /\A([a-z0-9]+[\w|\-|\.|\+]*/i
The username has to fulfill the following criteria:
- Must be one character or more
- May contain any letters, numbers and underscore
- Must be case insensitive
- May contain periods and hyphens, but not as the first character
DOMAIN = /[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,63}/i
The domain has to fulfill the following criteria:
- Must start with a letter or number
- May contain periods and hyphens
- The TLD should contain only letters and should be between 2 and 63 characters long
- Must be case insensitive
TYPICAL_EMAIL = /(?=\A.{6,255}\z)(#{USER})@(#{DOMAIN})/
Please note that this regex pattern doesn’t strictly follow the RFC 5322. You can’t validate internationalized emails or TLD emails via this regex, such as [email protected]
2. MX validation
Mail exchange (MX) record validation is the second, DNS-level validation method. The point of this method is to check the availability of the domain that’s used in the email address with the help of DNS records.

This is the schema of a typical MX domain lookup based on RFC 5321. It consists of three substeps: MX, CNAME, and A record resolution. Each resolver attempts to extract the mail servers from the email domain. If at least one server exists, validation is successful. Resolvers are checked in sequence until a resolver returns true or all resolvers fail.

The MX records resolver consists of Null MX record, and MX records check. Following RFC 7505, if a domain doesn’t accept email, it should have a Null MX record. This is an MX record with zero priority and with a period as the value. If Null MX exists, validation will fail. Otherwise, we have to check MX records. If no MX records are found, we proceed to the second substep: the CNAME records resolver.

The CNAME records resolver tries to extract hosts from domain CNAME records. If domain CNAME records exist, the CNAME resolver just transfers control to the MX resolver; otherwise, it transfers control to an A record resolver.

An A record resolver checks domain A, records its existence, and saves the result as an IP address in the list.
3. SMTP validation
SMTP validation is the last high-level email validation. This method tries to determine the existence of an email address.

SMTP validation consists of two parts: checking ports and checking SMTP sessions.
The operation will be iterated until the SMTP session returns true. Otherwise, validation fails.

An SMTP session consists of four substeps: opening the session and three SMTP commands (HELO, MAILFROM, and RCPT TO). If an email exists, each step of the SMTP session should return status code 250.
Note that a verifier IP should have a PTR record to the HELO host for the best validation results. Also, the HELO host should be real and should have an A record to a verifier IP address where it runs. The MAILFROM argument should also exist and must include the HELO host.
What’s Truemail?
Truemail is a gem that uses all the verification methods mentioned above. It’s a lightweight, configurable, and simple Ruby email validator.
How does Truemail work?
The Truemail gem allows you to validate emails via the regex pattern, by domain DNS records, and by the real existence of an email account on a mailbox provider.
Benefits of Truemail
When we were working on Truemail, we wanted to combine all email validation methods and make this gem very easy to use. Here are the benefits you get using Truemail:
- It’s configurable. Truemail allows you to validate only those metrics you need.
- It’s lightweight. Truemail is a Ruby library with zero runtime dependencies.
- It has a simple SMTP debugger. This debugger helps us find bugs easily.
What configurable options does Truemail have?
Here’s how you can configure Truemail.
Truemail host audit
As email validation based on the existence of an email address is a complex network process, you need to make sure that your verification host corresponds to modern SMTP metrics that mailbox providers use.
That’s why we added a host audit feature in Truemail. This feature allows us to perform an audit of the host where Truegem runs and helps us determine any current host issues.
So far, we’ve implemented only PTR record audit in Truemail. This feature helps us check:
- PTR existence
- PTR reference
- Reverse trace
Here’s how we perform a Truemail host audit:
Once we’ve checked that our host works correctly, we can start email validation.
Email validation with Truemail
Below, you can see how Truemail performs regex, MX, and SMTP validation.
Regex validation
Validation with the regex pattern is the first validation level. By default, this validation isn’t performed by strictly following the RFC 5322 standard, so you can override Truemail’s default regex pattern if you want.
MX validation
Validation by MX records is the second validation level.

Truemail performs regex validation and then the MX validation itself.
The Truemail MX validator doesn’t follow the RFC 5321 standard strictly for the best validation outcome. So operation will be iterated even if one of the MX records has an unresolvable host.
SMTP validation
SMTP validation is the final, third level of validation. This type of validation tries to check the existence of an email account on a mail server.

If the total number of MX servers equals one, the Truemail::Smtp validator will use the value from Configuration.connection_attempts.
Also, you don't need to pass with-parameter to use this validation.
This is an example with default settings (smtp_safe_check param equals to false)
If there are no SMTP errors found, the validation is successful.
But what is the SMTP safe check option? By default, it’s set to false, meaning that if any SMTP errors are detected, the validation will fail.
Here you can see an example of an SMTP error with the option smtp_safe_check = false.
You can find all request instances with SMTP errors in SMTP debugger.
Now, let’s see what happens with smtp_safe_check = true.

In this case we have SMTP errors, but validation is successful. Why? Because the SMTP server doesn’t return the exact response that the current email doesn’t exist. By default, SMTP safe check is disabled and is available for SMTP validation only. So what will we receive if the server returns an RCPT TO error?

If an SMTP RCPT TO error is found, validation will fail. The SMTP error body pattern is configurable, which means you can define your own regex pattern if you need.
Truemail aims at making email validation easier and faster. You can find the full source code on our GitHub account. Feel free to ask a question or start a conversation below.


Subscribe via email and know it all first!