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.
- Represent an email address with an optional name.
- Create an
Addressobject from a raw string (e.g.,"John Doe <john@example.com>"). - Validate email addresses.
- Format email addresses in a standard string format.
- Create multiple
Addressinstances from an array of data. - Follows SOLID principles with
AddressInterface.
composer require effectra/email-address-formatteruse Effectra\EmailAddressFormatter\Address;
$address = new Address("john@example.com", "John Doe");
echo $address->getEmail(); // john@example.com
echo $address->getName(); // John Doe
echo $address->format(); // John Doe <john@example.com>use Effectra\EmailAddressFormatter\Address;
$address = Address::createFrom("Jane Doe <jane@example.com>");
echo $address->getEmail(); // jane@example.com
echo $address->getName(); // Jane Doeuse Effectra\EmailAddressFormatter\AddressFactory;
// Single address
$address = AddressFactory::create("mike@example.com", "Mike");
// Multiple addresses
$addresses = AddressFactory::createFromArray([
"susan@example.com",
["email" => "david@example.com", "name" => "David"],
["email" => "emma@example.com"] // name is optional
]);
foreach ($addresses as $addr) {
echo $addr->format() . PHP_EOL;
}
// Output:
// susan@example.com <susan@example.com>
// David <david@example.com>
// emma@example.com <emma@example.com>use Effectra\EmailAddressFormatter\Address;
if (Address::emailValidation("valid@example.com")) {
echo "Valid email!";
} else {
echo "Invalid email!";
}__construct(string $email, string $name = '')static createFrom(string $addressText): staticgetName(): stringgetEmail(): stringstatic emailValidation(string $email): boolformat(): string__toString(): string
static create(string $email, string $name = ''): AddressInterfacestatic createFromArray(array $data): AddressInterface[]
getName(): stringgetEmail(): stringstatic emailValidation(string $email): boolformat(): string
$address = new Address("alex@example.com", "Alex");
echo $address;
// Output: Alex <alex@example.com>This package is open-source and available under the MIT License.