omegavesko / email-parser
A library that makes it easy to get various information about a given email address.
Requires
- php: >=7.4
- ext-json: *
- egulias/email-validator: ^2.1
- psr/log: ^1.1
- symfony/property-access: ^4.3
- symfony/serializer: ^4.3
Requires (Dev)
- phpunit/phpunit: ^9
- symfony/var-dumper: ^4.3
This package is auto-updated.
Last update: 2025-01-25 04:30:59 UTC
README
email-parser
is a PHP library that makes it easy to get various information
about an email address.
Features
- ✔️ 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
Getting Started
Prerequisites
- PHP >= 7.4
- Composer
Installing
composer require omegavesko/email-parser
Usage
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)
Getting email provider information
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
.
Configuration
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.
Development
Running the tests
vendor/bin/phpunit
Authors
- Veselin Romić (omegavesko@gmail.com)
License
This project is licensed under the MIT License - see the LICENSE file for details.