How to Validate Email Address in Rust

Check if an email address is correct and really exists using the Rust lang and Real Email 🦀


Email Validation in a Nutshell

An Email Address can look right but still be wrong and bounce. Real Email uses in depth email address validation to check if emails really exist without sending any messages.

Real Email API Validation

To check if an address really exists you can use the Real Email API which does in depth Email Address inspection on the email server. In this example we use the Rust reqwest crate.

use serde::{Deserialize};
use reqwest::Error;

#[derive(Deserialize, Debug)]
struct Response {
status: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
let email = "foo@bar.com";

let request_url = format!("https://isitarealemail.com/api/email/validate?email={email}",
email = email);
let result: Response = reqwest::get(&request_url).await?.json().await?;

println!("{:?}", result.status);

Ok(())
}

You will be able to test 100 emails per day for free. If you have more that that you will need to signup and get an API key. An Email Address might have the unknown status if the email server is unresponsive.

Email Validation Using an API Key

With an API key you wont be limited by how many addresses you can check or how fast you can check them. In Rust the example changes to add the API key.

use serde::{Deserialize};
use reqwest::Error;

#[derive(Deserialize, Debug)]
struct Response {
status: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
let email = "foo@bar.com";
let api_key = ???;

let request_url = format!("https://isitarealemail.com/api/email/validate?email={email}",
email = email);
let result: Response = reqwest::get(&request_url)
.bearer_auth(api_key)
.send().await?.json().await?;

println!("{:?}", result.status);

Ok(())
}

Get started with Real Email validations today.