How to validate email addresses in Swift

Our free API to validate e-mail addresses is really easy to use in Swift.

 

Validate e-mail in Swift


import Foundation

let headers = [
	"x-rapidapi-host": "mailcheck.p.rapidapi.com",
	"x-rapidapi-key": "YOUR-API-KEY"
]

let request = NSMutableURLRequest(url: NSURL(string: "https://mailcheck.p.rapidapi.com/?domain=EMAIL-OR-DOMAIN")! as URL,
                                    cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 15.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
	if (error != nil) {
		print(error)
	} else {
		let httpResponse = response as? HTTPURLResponse
		print(httpResponse)
	}
                            })

dataTask.resume()
                

This will return a JSON-array with information about the domain, and if you should block it or not.

 

You can of course just send it as a regular GET-request to that URL also, in your favorite way, as long as you have the API-key in the header.

You will receive a JSON response, telling you if this is a disposable e-mail to block, or if there are other issues with the domain:

 


{
   "valid": true,
   "block": true,
   "disposable": true,
   "domain": "iiron.us",
   "text": "Disposable e-mail",
   "reason": "Heuristics (4a)",
   "mx_host": "mail.iiron.us",
   "mx_info": "Using MX pointer mail.iiron.us from DNS with priority: 0",
   "mx_ip": "107.191.99.34"
}
               

This will tell you if you should block the domain iiron.us or not, and the reason to why the API thinks so (disposable e-mail, etc).

To get started for free, and get an API key, click here!