Skip to content

coodde/Laravel-Email-Checker

Repository files navigation

Contributors Forks Stargazers Issues Apache 2 License LinkedIn


Laravel Email Checker

Validates email addresses belong to their domain.

Report Bug · Request Feature

Table of Contents
  1. About The Package
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact

About The Project

Flexible and simple library for checking email addresses. Usual framework validators are usually checking email correctness, but this library is implementing other kind of validation.

It can check is mail:

  • registered in mail provider from forbidden country;
  • registered in forbidden domain (all possible levels);
  • used for spam or scam (dangerous), temporary, had suspicious behaviour, is registered on paid or public (like gmail) mail provider.

Of course, you can always propose new domains to add into listed in the "data" directory.

Key feautures:

  • fast search - binary search in the pre-sorted dictionaries (in comparison with other popular libraries with linear search)
  • flexible configurations - not only one strict list
  • low memory usage - disctionaries are not loaded fully into memory (as in other popular libraries)
  • frequent updates
  • easy collaboration
  • large disctionary- 60k+ of domains (not only public email providers) in the dictionary

(back to top)

Built With

This library supports several languages and frameworks.

  • PHP
    • Vanilla
    • Laravel (current)
    • Symfony (planned)
    • Phalcon (planned)
  • JS / TS
    • Vanilla (planned)
    • Next (planned)
    • React (planned)
    • Vue (planned)
    • Angular (planned)
    • Svelte (planned)
  • Ruby
    • Ruby on Rails (planned)
  • Perl
    • Vanilla (planned)

(back to top)

Getting Started

Simple steps to start use the library.

Requires

Check that library works on your PHP version:

  • PHP 8.0+
  • Composer

Installation

Below is an simple list of step to install library.

  1. Open your project directory in the terminal
  2. Install package
composer require coodde/laravel-mail-checker
  1. Check your composer.json file
  2. Check your app.php, that ServiceProvider is installed
  3. Check your config directory, that config file mail-checker.php is created

(back to top)

Usage

This table will help to understand possible usage of library and all default values:

Params Validator classes Validator names Default Value Possible Values Description
categories Coodde\LaravelMailChecker\Rules\MailListedValidation
Coodde\LaravelMailChecker\Rules\MailValidation
mail_check_listed, mail_check [Regions::CATEGORY_DANGEROUS] MailListedValidator::CATEGORY_PUBLIC
MailListedValidator::CATEGORY_PAID
MailListedValidator::CATEGORY_TEMPORARY
MailListedValidator::CATEGORY_SUSPICIOUS
MailListedValidator::CATEGORY_DANGEROUS
For more comfortable usage all mail providers are split into several lists
domains Coodde\LaravelMailChecker\Rules\MailDomainsValidation
Coodde\LaravelMailChecker\Rules\MailValidation
mail_check_domains, mail_check [] * Any kind of domains, starting by top level domains like "com" or "net", and finishing by exact domains like "mail.ru"
regions Coodde\LaravelMailChecker\Rules\MailRegionsValidation
Coodde\LaravelMailChecker\Rules\MailValidation
mail_check_regions, mail_check [ Regions::RUSSIA, Regions::BELARUS, Regions::NORTH_KOREA, Regions::AFGHANISTAN, Regions::IRAN, Regions::SYRIA, Regions::SOVIET_UNION, Regions::CUBA ]
All Available Regions
  1. Regions::ASCENSION_ISLAND
  2. Regions::ANDORRA
  3. Regions::UNITED_ARAB_EMIRATES
  4. Regions::UAE
  5. Regions::AFGHANISTAN
  6. Regions::ANTIGUA_AND_BARBUDA
  7. Regions::ANGUILLA
  8. Regions::ALBANIA
  9. Regions::ARMENIA
  10. Regions::ANGOLA
  11. Regions::ANTARCTICA
  12. Regions::ARGENTINA
  13. Regions::AMERICAN_SAMOA
  14. Regions::AUSTRIA
  15. Regions::AUSTRALIA
  16. Regions::ARUBA
  17. Regions::ALAND
  18. Regions::AZERBAIJAN
  19. Regions::BOSNIA_AND_HERZEGOVINA
  20. Regions::BARBADOS
  21. Regions::BANGLADESH
  22. Regions::BELGIUM
  23. Regions::BURKINA_FASO
  24. Regions::BULGARIA
  25. Regions::BAHRAIN
  26. Regions::BURUNDI
  27. Regions::BENIN
  28. Regions::BERMUDA
  29. Regions::BRUNEI
  30. Regions::BOLIVIA
  31. Regions::CARIBBEAN_NETHERLANDS
  32. Regions::BRAZIL
  33. Regions::BAHAMAS
  34. Regions::BHUTAN
  35. Regions::BOTSWANA
  36. Regions::BELARUS
  37. Regions::BELIZE
  38. Regions::CANADA
  39. Regions::COCOS_ISLANDS
  40. Regions::DEMOCRATIC_REPUBLIC_OF_THE_CONGO
  41. Regions::CENTRAL_AFRICAN_REPUBLIC
  42. Regions::REPUBLIC_OF_THE_CONGO
  43. Regions::SWITZERLAND
  44. Regions::IVORY_COAST
  45. Regions::COOK_ISLANDS
  46. Regions::CHILE
  47. Regions::CAMEROON
  48. Regions::CHINA
  49. Regions::COLOMBIA
  50. Regions::COSTA_RICA
  51. Regions::CUBA
  52. Regions::CAPE_VERDE
  53. Regions::CURACAO
  54. Regions::CHRISTMAS_ISLAND
  55. Regions::CYPRUS
  56. Regions::CZECH_REPUBLIC
  57. Regions::GERMANY
  58. Regions::DJIBOUTI
  59. Regions::DENMARK
  60. Regions::DOMINICA
  61. Regions::DOMINICAN_REPUBLIC
  62. Regions::ALGERIA
  63. Regions::ECUADOR
  64. Regions::ESTONIA
  65. Regions::EGYPT
  66. Regions::WESTERN_SAHARA
  67. Regions::ERITREA
  68. Regions::SPAIN
  69. Regions::ETHIOPIA
  70. Regions::EUROPEAN_UNION
  71. Regions::FINLAND
  72. Regions::FIJI
  73. Regions::FALKLAND_ISLANDS
  74. Regions::FEDERATED_STATES_OF_MICRONESIA
  75. Regions::FAROE_ISLANDS
  76. Regions::FRANCE
  77. Regions::GABON
  78. Regions::GRENADA
  79. Regions::GEORGIA
  80. Regions::FRENCH_GUIANA
  81. Regions::GUERNSEY
  82. Regions::GHANA
  83. Regions::GIBRALTAR
  84. Regions::GREENLAND
  85. Regions::THE_GAMBIA
  86. Regions::GUINEA
  87. Regions::GUADELOUPE
  88. Regions::EQUATORIAL_GUINEA
  89. Regions::GREECE
  90. Regions::SOUTH_GEORGIA_AND_THE_SOUTH_SANDWICH_ISLANDS
  91. Regions::GUATEMALA
  92. Regions::GUAM
  93. Regions::GUINEA_BISSAU
  94. Regions::GUYANA
  95. Regions::HONG_KONG
  96. Regions::HEARD_ISLAND_AND_MCDONALD_ISLANDS
  97. Regions::HONDURAS
  98. Regions::CROATIA
  99. Regions::HAITI
  100. Regions::HUNGARY
  101. Regions::INDONESIA
  102. Regions::IRELAND
  103. Regions::ISRAEL
  104. Regions::ISLE_OF_MAN
  105. Regions::INDIA
  106. Regions::BRITISH_INDIAN_OCEAN_TERRITORY
  107. Regions::IRAQ
  108. Regions::IRAN
  109. Regions::ICELAND
  110. Regions::ITALY
  111. Regions::JERSEY
  112. Regions::JAMAICA
  113. Regions::JORDAN
  114. Regions::JAPAN
  115. Regions::KENYA
  116. Regions::KYRGYZSTAN
  117. Regions::CAMBODIA
  118. Regions::KIRIBATI
  119. Regions::COMOROS
  120. Regions::SAINT_KITTS_AND_NEVIS
  121. Regions::NORTH_KOREA
  122. Regions::SOUTH_KOREA
  123. Regions::KUWAIT
  124. Regions::CAYMAN_ISLANDS
  125. Regions::KAZAKHSTAN
  126. Regions::LAOS
  127. Regions::LEBANON
  128. Regions::SAINT_LUCIA
  129. Regions::LIECHTENSTEIN
  130. Regions::SRI_LANKA
  131. Regions::LIBERIA
  132. Regions::LESOTHO
  133. Regions::LITHUANIA
  134. Regions::LUXEMBOURG
  135. Regions::LATVIA
  136. Regions::LIBYA
  137. Regions::MOROCCO
  138. Regions::MONACO
  139. Regions::MOLDOVA
  140. Regions::MONTENEGRO
  141. Regions::MADAGASCAR
  142. Regions::MARSHALL_ISLANDS
  143. Regions::NORTH_MACEDONIA
  144. Regions::MALI
  145. Regions::MYANMAR
  146. Regions::MONGOLIA
  147. Regions::MACAU
  148. Regions::NORTHERN_MARIANA_ISLANDS
  149. Regions::MARTINIQUE
  150. Regions::MAURITANIA
  151. Regions::MONTSERRAT
  152. Regions::MALTA
  153. Regions::MAURITIUS
  154. Regions::MALDIVES
  155. Regions::MALAWI
  156. Regions::MEXICO
  157. Regions::MALAYSIA
  158. Regions::MOZAMBIQUE
  159. Regions::NAMIBIA
  160. Regions::NEW_CALEDONIA
  161. Regions::NIGER
  162. Regions::NORFOLK_ISLAND
  163. Regions::NIGERIA
  164. Regions::NICARAGUA
  165. Regions::NETHERLANDS
  166. Regions::NORWAY
  167. Regions::NEPAL
  168. Regions::NAURU
  169. Regions::NIUE
  170. Regions::NEW_ZEALAND
  171. Regions::OMAN
  172. Regions::PANAMA
  173. Regions::PERU
  174. Regions::FRENCH_POLYNESIA
  175. Regions::PAPUA_NEW_GUINEA
  176. Regions::PHILIPPINES
  177. Regions::PAKISTAN
  178. Regions::POLAND
  179. Regions::SAINT_PIERRE_AND_MIQUELON
  180. Regions::PITCAIRN_ISLANDS
  181. Regions::PUERTO_RICO
  182. Regions::PALESTINE
  183. Regions::PORTUGAL
  184. Regions::PALAU
  185. Regions::PARAGUAY
  186. Regions::QATAR
  187. Regions::RÉUNION
  188. Regions::ROMANIA
  189. Regions::SERBIA
  190. Regions::RUSSIA
  191. Regions::RWANDA
  192. Regions::SAUDI_ARABIA
  193. Regions::SOLOMON_ISLANDS
  194. Regions::SEYCHELLES
  195. Regions::SUDAN
  196. Regions::SWEDEN
  197. Regions::SINGAPORE
  198. Regions::SAINT_HELENA_ASCENSION_AND_TRISTAN_DA_CUNHA
  199. Regions::SAINT_HELENA
  200. Regions::SLOVENIA
  201. Regions::SLOVAKIA
  202. Regions::SIERRA_LEONE
  203. Regions::SAN_MARINO
  204. Regions::SENEGAL
  205. Regions::SOMALIA
  206. Regions::SURINAME
  207. Regions::SOUTH_SUDAN
  208. Regions::SAO_TOME_AND_PRINCIPE
  209. Regions::SOVIET_UNION
  210. Regions::EL_SALVADOR
  211. Regions::SINT_MAARTEN
  212. Regions::SYRIA
  213. Regions::ESWATINI
  214. Regions::TURKS_AND_CAICOS_ISLANDS
  215. Regions::CHAD
  216. Regions::FRENCH_SOUTHERN_AND_ANTARCTIC_LANDS
  217. Regions::TOGO
  218. Regions::THAILAND
  219. Regions::TAJIKISTAN
  220. Regions::TOKELAU
  221. Regions::EAST_TIMOR
  222. Regions::TURKMENISTAN
  223. Regions::TUNISIA
  224. Regions::TONGA
  225. Regions::TURKEY
  226. Regions::TRINIDAD_AND_TOBAGO
  227. Regions::TUVALU
  228. Regions::TAIWAN
  229. Regions::TANZANIA
  230. Regions::UKRAINE
  231. Regions::UGANDA
  232. Regions::UNITED_KINGDOM
  233. Regions::UNITED_STATES_OF_AMERICA
  234. Regions::USA
  235. Regions::URUGUAY
  236. Regions::UZBEKISTAN
  237. Regions::VATICAN_CITY
  238. Regions::SAINT_VINCENT_AND_THE_GRENADINES
  239. Regions::VENEZUELA
  240. Regions::BRITISH_VIRGIN_ISLANDS
  241. Regions::UNITED_STATES_VIRGIN_ISLANDS
  242. Regions::VIETNAM
  243. Regions::VANUATU
  244. Regions::WALLIS_AND_FUTUNA
  245. Regions::SAMOA
  246. Regions::YEMEN
  247. Regions::MAYOTTE
  248. Regions::SOUTH_AFRICA
  249. Regions::ZAMBIA
  250. Regions::ZIMBABWE
Recommended to use constants for easier code maintenance

Here you will find different cases of usage.

Checking that mail address registered in Russian mail provider:

use Coodde\LaravelMailChecker\Rules\MailRegionsValidation;
use Coodde\LaravelMailChecker\Regions;

public function store(Request $request): RedirectResponse
{
    $validated = $request->validate([
        'email' => ['required', new MailRegionsValidation([Regions::RUSSIA])],
    ]);
 
    // OR with configs from env, ex. (country code) - MAIL_CHECKER_REGIONS=ru

    $validated = $request->validate([
        'email' => ['required', new MailRegionsValidation()],
    ]);
 
    // Request is valid...
 
    return redirect('/list');
}

Checking that mail address registered in "ru" or "mail.by" domains:

use Coodde\LaravelMailChecker\Rules\MailDomainsValidation;
use Coodde\LaravelMailChecker\Regions;

public function store(Request $request): RedirectResponse
{
    $validated = $request->validate([
        'email' => ['required', new MailDomainsValidation(['ru', 'mail.by'])],
    ]);
 
    // OR with configs from env, ex. - MAIL_CHECKER_DOMAINS=ru,mail.by

    $validated = $request->validate([
        'email' => ['required', new MailDomainsValidation()],
    ]);

    // Request is valid...
 
    return redirect('/list');
}

Checking that mail address is placed in dangerous or suspicious lists:

use Coodde\LaravelMailChecker\Rules\MailListedValidation;
use Coodde\LaravelMailChecker\Regions;

public function store(Request $request): RedirectResponse
{
    $validated = $request->validate([
        'email' => ['required', new MailListedValidation([MailListedValidation::CATEGORY_DANGEROUS, MailListedValidation::CATEGORY_SUSPICIOUS])],
    ]);

    // OR with configs from env, ex. - MAIL_CHECKER_CATEGORIES=dangerous,suspicious

    $validated = $request->validate([
        'email' => ['required', new MailListedValidation()],
    ]);
 
    // Request is valid...
 
    return redirect('/list');
}

Complex validation to allow only corporate emails, :

use Coodde\LaravelMailChecker\Rules\MailValidation;
use Coodde\LaravelMailChecker\Regions;

public function store(Request $request): RedirectResponse
{
    $validated = $request->validate([
        'email' => [
        	'required',
        	new MailValidation(
        		[
        			MailListedValidation::CATEGORY_DANGEROUS,
        			MailListedValidation::CATEGORY_SUSPICIOUS,
        			MailListedValidation::CATEGORY_TEMPORARY,
        			MailListedValidation::CATEGORY_PAID,
        			MailListedValidation::CATEGORY_PUBLIC,
        		],
        		[
        			'fb.com',
        		],
        		[
        			Regions::RUSSIA,
        			Regions::BELARUS,
        		],
        	),
       	],
    ]);

    // OR with separate validators

    $validated = $request->validate([
        'email' => [
        	'required',
        	new MailListedValidation([
    			MailListedValidation::CATEGORY_DANGEROUS,
    			MailListedValidation::CATEGORY_SUSPICIOUS,
    			MailListedValidation::CATEGORY_TEMPORARY,
    			MailListedValidation::CATEGORY_PAID,
    			MailListedValidation::CATEGORY_PUBLIC,
        	]),
        	new MailDomainsValidation([
    			'fb.com',
    		]),
        	new MailRegionsValidation([
    			Regions::RUSSIA,
    			Regions::BELARUS,
    		]),
       	],
    ]);
 
    // Request is valid...
 
    return redirect('/list');
}

Of course you can combine restrictioned domains, countries, and categories

(back to top)

Roadmap

  • Checking by countries
  • Checking by domains
  • Prepared lists
    • Dangerous - usually scaming servers / domains
    • Suspicios - usually spaming servers / domains
    • Paid - mail providers with non-free subscription
    • Temporary - services for mails which will be removed soon after creation
    • Public - popular free services like gmail, outlook, etc
  • Checking by prepaired lists
    • From files - it uses binary search without file content buffering
    • From cache - cache files with lists compiled into php file
    • From memory - storing lists in memcache
    • From database - by using PDO library+

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

✅ Run refactors using Rector

composer refacto

⚗️ Run static analysis using PHPStan:

composer test:types

✅ Run unit tests using PEST

composer test:unit

🚀 Run the entire test suite:

composer test

Top contributors:

contrib.rocks image

(back to top)

License

Distributed under the Apache 2.0 License. See LICENSE for more information.

(back to top)

Contact

Svyatoslav Ryzhok - info@coodde.com

Platform Link: https://coodde.com

(back to top)