Skip to content

effectra/email-address-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Effectra Email Address Formatter

A lightweight PHP package for representing and formatting email addresses with optional names.
It provides validation, parsing, and factory methods to work with email addresses in a clean and consistent way.


Features

  • Represent an email address with an optional name.
  • Create an Address object from a raw string (e.g., "John Doe <[email protected]>").
  • Validate email addresses.
  • Format email addresses in a standard string format.
  • Create multiple Address instances from an array of data.
  • Follows SOLID principles with AddressInterface.

Installation

composer require effectra/email-address-formatter

Usage

Basic Example

use Effectra\EmailAddressFormatter\Address;

$address = new Address("[email protected]", "John Doe");

echo $address->getEmail(); // [email protected]
echo $address->getName();  // John Doe
echo $address->format();   // John Doe <[email protected]>

Create from String

use Effectra\EmailAddressFormatter\Address;

$address = Address::createFrom("Jane Doe <[email protected]>");

echo $address->getEmail(); // [email protected]
echo $address->getName();  // Jane Doe

Using AddressFactory

use Effectra\EmailAddressFormatter\AddressFactory;

// Single address
$address = AddressFactory::create("[email protected]", "Mike");

// Multiple addresses
$addresses = AddressFactory::createFromArray([
    "[email protected]",
    ["email" => "[email protected]", "name" => "David"],
    ["email" => "[email protected]"] // name is optional
]);

foreach ($addresses as $addr) {
    echo $addr->format() . PHP_EOL;
}
// Output:
// [email protected] <[email protected]>
// David <[email protected]>
// [email protected] <[email protected]>

Validation

use Effectra\EmailAddressFormatter\Address;

if (Address::emailValidation("[email protected]")) {
    echo "Valid email!";
} else {
    echo "Invalid email!";
}

API Reference

Address class

  • __construct(string $email, string $name = '')
  • static createFrom(string $addressText): static
  • getName(): string
  • getEmail(): string
  • static emailValidation(string $email): bool
  • format(): string
  • __toString(): string

AddressFactory class

  • static create(string $email, string $name = ''): AddressInterface
  • static createFromArray(array $data): AddressInterface[]

AddressInterface

  • getName(): string
  • getEmail(): string
  • static emailValidation(string $email): bool
  • format(): string

Example Output

$address = new Address("[email protected]", "Alex");

echo $address; 
// Output: Alex <[email protected]>

License

This package is open-source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages