Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ash-jc-allen/laravel-mailboxlayer

A lightweight Laravel package for validating email addresses using the Mailbox Layer API.
https://github.com/ash-jc-allen/laravel-mailboxlayer

laravel mailboxlayer

Last synced: about 2 months ago
JSON representation

A lightweight Laravel package for validating email addresses using the Mailbox Layer API.

Awesome Lists containing this project

README

        


Laravel Mailbox Layer


Latest Version on Packagist
Build Status
Total Downloads
PHP from Packagist
GitHub license

## Table of Contents

- [Overview](#overview)
- [Installation](#installation)
- [Requirements](#requirements)
- [Install the Package](#install-the-package)
- [Publish the Config](#publish-the-config)
- [Getting Your Mailbox Layer API Key](#getting-your-mailbox-layer-api-key)
- [Usage](#usage)
- [Methods](#methods)
- [Validating One Email Address](#validating-one-email-address)
- [Validating Multiple Email Addresses](#validating-multiple-email-addresses)
- [Facade](#facade)
- [Available Validation Result Properties](#available-validation-result-properties)
- [Caching](#caching)
- [Caching Validation Results](#caching-validation-results)
- [Busting the Cached Validation Results](#busting-the-cached-validation-results)
- [Options](#options)
- [Using HTTPS](#using-https)
- [Running an SMTP Check](#running-an-smtp-check)
- [Testing](#testing)
- [Security](#security)
- [Contribution](#contribution)
- [Credits](#credits)
- [Changelog](#changelog)
- [License](#license)

## Overview
Laravel Mailbox Layer is a lightweight wrapper Laravel package that can be used for validating email addresses via the
[Mailbox Layer API](https://mailboxlayer.com/). The package supports caching so that you can start validating email addresses instantly.

![Mailbox Layer Usage Image](/docs/images/check-image.png?v=1)

## Installation

### Requirements
The package has been developed and tested to work with the following minimum requirements:

- PHP 7.3
- Laravel 7

### Install the Package
You can install the package via Composer:

```bash
composer require ashallendesign/laravel-mailboxlayer
```

### Publish the Config
You can then publish the package's config file (so that you can make changes to them) by using the following command:
```bash
php artisan vendor:publish --provider="AshAllenDesign\MailboxLayer\Providers\MailboxLayerProvider"
```

### Getting Your Mailbox Layer API Key
To use this package and interact with the Mailbox Layer API, you'll need to register on the [Mailbox Layer API](https://mailboxlayer.com/)
website and get your API key. Once you have the key, you can set it in your ` .env ` file as shown below:

```
MAILBOX_LAYER_API_KEY=your-api-key-here
```

## Usage
### Methods
#### Validating One Email Address

To validate a single email address, you can use the ` check() ` method that is provided in the package. This method returns a ` ValidationResult ` object.

The example below shows how to validate a single email address:

```php
use AshAllenDesign\MailboxLayer\Classes\MailboxLayer;

$mailboxLayer = new MailboxLayer('api-key-here');
$validationResult = $mailboxLayer->check('[email protected]');
```

#### Validating Multiple Email Addresses

To validate multiple email addresses, you can use the ` checkMany() ` method that is provided in the package. This method returns a ` Collection ` of ` ValidationResult ` objects.

The example below shows how to validate multiple email addresses:

```php
use AshAllenDesign\MailboxLayer\Classes\MailboxLayer;

$mailboxLayer = new MailboxLayer('api-key-here');
$validationResults = $mailboxLayer->checkMany(['[email protected]', '[email protected]']);
```

### Facade
If you prefer to use facades in Laravel, you can choose to use the provided ` Mailbox Layer ` facade instead of instantiating the ``` AshAllenDesign\MailboxLayer\Classes\MailboxLayer ```
class manually.

The example below shows an example of how you could use the facade to validate an email address:

```php
use MailboxLayer;

return MailboxLayer::check('[email protected]');
```

### Available Validation Result Properties

| Field | Description |
|-------------|-------------------------------------------------------------------------------------------------------|
| email | The email address that the validation was carried out on. |
| didYouMean | A suggested email address in case a typo was detected. |
| user | The local part of the email address. Example: 'mail' in '[email protected]'. |
| domain | The domain part of the email address. Example: 'ashallendesign.co.uk' in '[email protected]'. |
| formatValid | Whether or not the syntax of the requested email is valid. |
| mxFound | Whether or not the MX records for the requested domain could be found. |
| smtpCheck | Whether or not the SMTP check of the requested email address succeeded. |
| catchAll | Whether or not the requested email address is found to be part of a catch-all mailbox. |
| role | Whether or not the requested email is a role email address. Example: '[email protected]'. |
| disposable | Whether or not the requested email is disposable. Example: '[email protected]'. |
| free | Whether or not the requested email is a free email address. |
| score | A score between 0 and 1 reflecting the quality and deliverability of the requested email address. |
| validatedAt | A ` Carbon ` object containing the date and time that the original validation API request was made. |

### Caching
#### Caching Validation Results
There might be times when you want to cache the validation results for an email. This can have significant performance benefits for if
you try to validate the email again, due to the fact that the results will be fetched from the cache rather than from a new API request.

As an example, if you were importing a CSV containing email addresses, you might want to validate each of the addresses. However, if the
CSV contains some duplicated email addresses, it could lead to unnecessary API calls being made. So, by using the caching, each unique
address would only be fetched once from the API. To do this, you can use the ` shouldCache() ` method.

Using caching is recommended as it reduces the chances of you reaching the monthly request limits or rate limits that are
used by Mailbox Layer. Read more about the [API limits here](https://mailboxlayer.com/documentation#rate_limits).

The example below shows how to cache the validation results:

```php
use AshAllenDesign\MailboxLayer\Classes\MailboxLayer;

$mailboxLayer = new MailboxLayer('api-key-here');

// Result fetched from the API.
$validationResults = $mailboxLayer->shouldCache()->check('[email protected]');

// Result fetched from the cache.
$validationResults = $mailboxLayer->shouldCache()->check('[email protected]');
```

#### Busting the Cached Validation Results
By default, the package will always try to fetch the validation results from the cache before trying to fetch them via the API.
As mentioned before, this can lead to multiple performance benefits.

However, there may be times that you want to ignore the cached results and make a new request to the API. As an example, you
might have a cached validation result that is over 6 months old and could possibly be outdated or inaccurate, so it's likely
that you want to update the validation data and ensure it is correct. To do this, you can use the ` fresh() ` method.

The example below shows how to fetch a new validation result:

```php
use AshAllenDesign\MailboxLayer\Classes\MailboxLayer;

$mailboxLayer = new MailboxLayer('api-key-here');

$validationResults = $mailboxLayer->fresh()->check('[email protected]');
```

### Options
#### Using HTTPS

By default, all the API requests are made using HTTPS. However, the [Mailbox Layer API](https://mailboxlayer.com/)
allows for requests to be made using HTTP if needed. This can be particularly useful when working in a local, development environment.
To use HTTP when making the API requests, you can use the ` withHttps() ` method.

Please note, it is not recommended making the requests over HTTP in a live, production environment!

The example below shows how to make the requests using HTTP rather than HTTPS:

```php
use AshAllenDesign\MailboxLayer\Classes\MailboxLayer;

$mailboxLayer = new MailboxLayer('api-key-here');

$validationResults = $mailboxLayer->withHttps(false)->check('[email protected]');
```

#### Running an SMTP Check

By default, all the API requests will run an SMTP check on the email address. Running this check can improve the accuracy
of the results and give better results. However, according to Mailbox Layer, running this checks take up around 75% of the
API's entire response time.

So, you can reduce the overall runtime before preventing the SMTP check from running by using the ` withSmtpCheck() ` method.

The example below shows how to validate an email address without running an SMTP check:

```php
use AshAllenDesign\MailboxLayer\Classes\MailboxLayer;

$mailboxLayer = new MailboxLayer('api-key-here');

$validationResults = $mailboxLayer->withSmtpCheck(false)->check('[email protected]');
```

Read more about the SMTP check in the [Mailbox Layer API docs](https://mailboxlayer.com/documentation#smtp_mx_check).

## Testing

```bash
vendor/bin/phpunit
```

## Security

If you find any security related issues, please contact me directly at [[email protected]](mailto:[email protected]) to report it.

## Contribution

If you wish to make any changes or improvements to the package, feel free to make a pull request.

To contribute to this library, please use the following guidelines before submitting your pull request:

- Write tests for any new functions that are added. If you are updating existing code, make sure that the existing tests
pass and write more if needed.
- Follow [PSR-2](https://www.php-fig.org/psr/psr-2/) coding standards.
- Make all pull requests to the ``` master ``` branch.

## Credits

- [Ash Allen](https://ashallendesign.co.uk)
- [Jess Pickup](https://jesspickup.co.uk) (Logo)
- [All Contributors](https://github.com/ash-jc-allen/laravel-mailboxlayer/graphs/contributors)

## Changelog

Check the [CHANGELOG](CHANGELOG.md) to get more information about the latest changes.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.