1. Go to this page and download the library: Download mmucklo/email-parse library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mmucklo / email-parse example snippets
use Email\Parse;
// Array-based API (v2.x-compatible)
$result = Parse::getInstance()->parse("[email protected][email protected]");
// Typed value objects (v3.1+, recommended for new code)
$address = Parse::getInstance()->parseSingle('[email protected]');
echo $address->localPart; // "john"
echo $address->domain; // "example.com"
if ($address->invalid) {
echo $address->invalidReasonCode->value;
}
$result = Parse::getInstance()->parseMultiple('[email protected], [email protected]');
foreach ($result->emailAddresses as $addr) { /* ... */ }
// Streaming for large batches (v3.2+) — yields one address at a time.
foreach (Parse::getInstance()->parseStream($csvRows) as $addr) {
if ($addr->invalid) continue;
// ...
}
// Serialization (v3.3+)
$parsed = Parse::getInstance()->parseSingle('"J Doe" <[email protected]>');
(string) $parsed; // "[email protected]" — Stringable returns simple_address
$parsed->canonical(); // 'J Doe <[email protected]>' — minimal RFC 5322 quoting
$parsed->toArray(); // legacy array shape, for mixed-API code
$parsed->toJson(); // JSON string
use Email\Parse;
use Email\ParseOptions;
// Example 1: Use comma and semicolon as separators (default behavior aa.com; [email protected], [email protected]");
// Example 2: Disable whitespace as separator (only comma and semicolon work)
$options = new ParseOptions([], [',', ';'], false);
$parser = new Parse(null, $options);
$result = $parser->parse("[email protected]; [email protected]"); // Works - uses semicolon
$result = $parser->parse("[email protected][email protected]"); // Won't split - whitespace not a separator
// Example 3: Names with spaces always work regardless of whitespace separator setting
$options = new ParseOptions([], [',', ';'], false);
$parser = new Parse(null, $options);
$result = $parser->parse("John Doe <[email protected]>, Jane Smith <[email protected]>");
// Returns 2 valid emails with names preserved
use Email\Parse;
use Email\ParseOptions;
// RFC 5321 — Strict ASCII (SMTP Mailbox syntax)
$options = ParseOptions::rfc5321();
$parser = new Parse(null, $options);
// RFC 6531 — Strict Internationalized (full UTF-8 + NFC normalization)
$options = ParseOptions::rfc6531();
$parser = new Parse(null, $options);
$result = $parser->parseSingle('müller@münchen.de'); // Valid UTF-8 address
// RFC 5322 — Standard with obsolete syntax support (recommended)
$options = ParseOptions::rfc5322();
$parser = new Parse(null, $options);
// RFC 2822 — Maximum compatibility
$options = ParseOptions::rfc2822();
$parser = new Parse(null, $options);
// Legacy — v2.x default behavior
$options = new ParseOptions();
$parser = new Parse(null, $options);
// UTF-8 email address validation
$options = ParseOptions::rfc6531();
$parser = new Parse(null, $options);
$result = $parser->parseSingle('José.García@españa.es');
// Valid: UTF-8 characters allowed in rfc6531() preset
$result = $parser->parseSingle('[email protected]');
// Invalid: Leading dot not allowed (dot-atom restrictions still apply)
// v2.x default (legacy behavior — still works in v3.0)
$parser = Parse::getInstance();
// v3.0 recommended default
$options = ParseOptions::rfc5322();
$parser = new Parse(null, $options);
// Use the rfc6531() preset for full internationalized email support
$options = ParseOptions::rfc6531();
$parser = new Parse(null, $options);
$result = $parser->parseSingle('müller@münchen.de');
/**
* function parse($emails, $multiple = true, $encoding = 'UTF-8')
* @param string $emails List of Email addresses separated by configured separators (comma, semicolon, whitespace by default)
* @param bool $multiple (optional, default: true) Whether to parse for multiple email addresses or not
* @param string $encoding (optional, default: 'UTF-8') The encoding if not 'UTF-8'
* @return: see below: */
if ($multiple):
array('success' => boolean, // whether totally successful or not
'reason' => string|null, // if unsuccessful, the reason why; null if successful
'email_addresses' =>
array('address' => string, // the full address (not including comments)
'original_address' => string, // the full address including comments
'simple_address' => string, // simply local_part@domain_part (e.g. [email protected])
'name' => string, // the name on the email if given (e.g.: John Q. Public), including any quotes
'name_parsed' => string, // the name on the email if given (e.g.: John Q. Public), excluding any quotes
'local_part' => string, // the local part (before the '@' sign - e.g. johnpublic)
'local_part_parsed' => string, // the local part (before the '@' sign - e.g. johnpublic), excluding any quotes
'domain' => string, // the domain after the '@' if given (may be Unicode)
'domain_ascii' => string|null, // punycode ASCII domain (when ll if valid
'comments' => array) // array of extracted comments (e.g. ['comment1', 'comment2'])
endif;