email-parser
is a PHP library that makes it easy to get various information
about an email address.
- ✔️ Configurable email validation (powered by egulias/email-validator)
- 🔍 Separate an email address into its segments
- 🌎 Get information about the email provider, based on the domain
- PHP >= 7.4
- Composer
composer require omegavesko/email-parser
To use the parser, create an instance of the EmailParser
class, and use the
parseEmail()
and parseEmails()
methods to parse emails into EmailInformation
instances.
<?php
use OmegaVesko\EmailParser\EmailParser;
$parser = new EmailParser();
$emailInformation = $parser->parseEmail("example@test.dev");
$emailInformation->email; // 'example@test.dev'
$emailInformation->domain; // 'test.dev'
$emailInformation->localPart; // 'example'
$emailInformation->emailService; // EmailServiceInformation instance (or null)
If an email uses the domain of a recognized popular public email provider
(e.g. Gmail), email-parser
will give you its name, the domains it knows about,
and a URL to the service's webmail interface.
One particularly useful application of this feature is linking a user directly to their email inbox, if, for example, you want to make it as easy as possible for them to get to an email you've just sent them.
<?php
use OmegaVesko\EmailParser\EmailParser;
$parser = new EmailParser();
$emailInformation = $parser->parseEmail("example@gmail.com");
$emailInformation->emailService->name; // 'Gmail'
$emailInformation->emailService->domains; // ['gmail.com', 'googlemail.com']
$emailInformation->emailService->webmailUrl; // 'https://mail.google.com/'
If the email isn't from a public email provider, or one email-parser
doesn't
recognize, getEmailService()
will return null
.
While email-parser
works perfectly fine out of the box with zero configuration,
there's a few things you can configure to better integrate it into your codebase,
or to adapt it to your needs.
The EmailParser
constructor takes the following optional arguments:
-
$logger
: An instance ofPsr\Log\LoggerInterface
.email-parser
will use this to log things like non-fatal warnings. -
$emailValidation
: An instance ofEgulias\EmailValidator\Validation\EmailValidation
.email-parser
will use this validation to validate all emails it parses. See the EmailValidator docs for available validations.If left blank, a simple
RFCValidation
will be used as a sane default.
vendor/bin/phpunit
- Veselin Romić (omegavesko@gmail.com)
This project is licensed under the MIT License - see the LICENSE file for details.