omegavesko/email-parser

A library that makes it easy to get various information about a given email address.

3.0 2021-04-17 14:15 UTC

This package is auto-updated.

Last update: 2024-04-25 03:02:45 UTC


README

CI Test Coverage Maintainability Packagist Version GitHub

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

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 of Psr\Log\LoggerInterface. email-parser will use this to log things like non-fatal warnings.

  • $emailValidation: An instance of Egulias\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

License

This project is licensed under the MIT License - see the LICENSE file for details.