|
| 1 | +email-parse |
| 2 | +=========== |
| 3 | + |
| 4 | +Email\Parse is a multiple (and single) email address parser that is reasonably RFC822 / RFC2822 compliant. |
| 5 | + |
| 6 | +It parses a list of 1 to n email addresses separated by space or comma |
| 7 | + |
| 8 | +Installation: |
| 9 | +------------- |
| 10 | +Add this line to your composer.json "require" section: |
| 11 | + |
| 12 | +### composer.json |
| 13 | +```json |
| 14 | + "require": { |
| 15 | + ... |
| 16 | + "oodle/email-parse": "*" |
| 17 | +``` |
| 18 | + |
| 19 | +Usage: |
| 20 | +------ |
| 21 | + |
| 22 | +```php |
| 23 | +use Email\Parse; |
| 24 | + |
| 25 | +$result = Parse::getInstance()->parseEmailAddresses("[email protected] [email protected]"); |
| 26 | +``` |
| 27 | + |
| 28 | +Notes: |
| 29 | +====== |
| 30 | +This should be RFC 2822 compliant, although it will let a few obsolete RFC 822 addresses through such as test"test"[email protected] (note the quoted string in the middle of the address, which may be obsolete as of RFC 2822). However it wont allow escaping outside of quotes such as test\@[email protected]. This would have to be written as "test\@test"@xyz.com |
| 31 | + |
| 32 | +Here are a few other examples: |
| 33 | + |
| 34 | +``` |
| 35 | +"John Q. Public" <[email protected]> |
| 36 | + |
| 37 | +how-about-an-ip@[10.0.10.2] |
| 38 | +how-about-comments(this is a comment!!)@xyz.com |
| 39 | +``` |
| 40 | + |
| 41 | +####PHPDoc#### |
| 42 | +function parseEmailAddresses |
| 43 | +@param $emails - List of Email addresses separated by comma or space if multiple |
| 44 | +@param $multiple - Whether to parse for multiple email addresses or not |
| 45 | +@param $encoding - The encoding if not 'UTF-8' |
| 46 | +@return |
| 47 | +```php |
| 48 | + if ($multiple): |
| 49 | + array('success' => boolean, // whether totally successful or not |
| 50 | + 'reason' => string, // if unsuccessful, the reason why |
| 51 | + 'email_addresses' => |
| 52 | + array('address' => string, // the full address (not including comments) |
| 53 | + 'original_address' => string, // the full address including comments |
| 54 | + 'simple_address' => string, // simply local_part@domain_part (e.g. [email protected]) |
| 55 | + 'name' => string, // the name on the email if given (e.g.: John Q. Public), including any quotes |
| 56 | + 'name_parsed' => string, // the name on the email if given (e.g.: John Q. Public), excluding any quotes |
| 57 | + 'local_part' => string, // the local part (before the '@' sign - e.g. johnpublic) |
| 58 | + 'local_part_parsed' => string, // the local part (before the '@' sign - e.g. johnpublic), excluding any quotes |
| 59 | + 'domain' => string, // the domain after the '@' if given |
| 60 | + 'ip' => string, // the IP after the '@' if given |
| 61 | + 'domain_part' => string, // either domain or IP depending on what given |
| 62 | + 'invalid' => boolean, // if the email is valid or not |
| 63 | + 'invalid_reason' => string), // if the email is invalid, the reason why |
| 64 | + array( .... ) // the next email address matched |
| 65 | + ) |
| 66 | + else: |
| 67 | + array('address' => string, // the full address including comments |
| 68 | + 'name' => string, // the name on the email if given (e.g.: John Q. Public) |
| 69 | + 'local_part' => string, // the local part (before the '@' sign - e.g. johnpublic) |
| 70 | + 'domain' => string, // the domain after the '@' if given |
| 71 | + 'ip' => string, // the IP after the '@' if given |
| 72 | + 'invalid' => boolean, // if the email is valid or not |
| 73 | + 'invalid_reason' => string) // if the email is invalid, the reason why |
| 74 | + endif; |
| 75 | +``` |
| 76 | + |
| 77 | +Other Example Usage: |
| 78 | +-------------------- |
| 79 | +```php |
| 80 | + $email = "\"J Doe\" <[email protected]>"; |
| 81 | + $result = Email\Parse->getInstance()->parseEmailAddresses($email, false); |
| 82 | + |
| 83 | + $result == array('address' => '"JD" <[email protected]>', |
| 84 | + 'original_address' => '"JD" <[email protected]>', |
| 85 | + 'name' => '"JD"', |
| 86 | + 'name_parsed' => 'J Doe', |
| 87 | + 'local_part' => 'johndoe', |
| 88 | + 'local_part_parsed' => 'johndoe', |
| 89 | + 'domain_part' => 'xyz.com', |
| 90 | + 'domain' => 'xyz.com', |
| 91 | + 'ip' => '', |
| 92 | + 'invalid' => false, |
| 93 | + 'invalid_reason' => ''); |
| 94 | + |
| 95 | + $emails = "testing@[10.0.10.45] [email protected], testing-"test...2"@xyz.com (comment)"; |
| 96 | + $result = Email\Parse->getInstance()->parseEmailAddresses($emails); |
| 97 | + $result == array( |
| 98 | + 'success' => boolean true |
| 99 | + 'reason' => null |
| 100 | + 'email_addresses' => |
| 101 | + array( |
| 102 | + array( |
| 103 | + 'address' => 'testing@[10.0.10.45]', |
| 104 | + 'original_address' => 'testing@[10.0.10.45]', |
| 105 | + 'name' => '', |
| 106 | + 'name_parsed' => '', |
| 107 | + 'local_part' => 'testing', |
| 108 | + 'local_part_parsed' => 'testing', |
| 109 | + 'domain_part' => '10.0.10.45', |
| 110 | + 'domain' => '', |
| 111 | + 'ip' => '10.0.10.45', |
| 112 | + 'invalid' => false, |
| 113 | + 'invalid_reason' => ''), |
| 114 | + array( |
| 115 | + |
| 116 | + 'original_address' => '[email protected]', |
| 117 | + 'name' => '', |
| 118 | + 'name_parsed' => '', |
| 119 | + 'local_part' => 'testing', |
| 120 | + 'local_part' => 'testing', |
| 121 | + 'domain_part' => 'xyz.com', |
| 122 | + 'domain' => 'xyz.com', |
| 123 | + 'ip' => '', |
| 124 | + 'invalid' => false, |
| 125 | + 'invalid_reason' => '') |
| 126 | + array( |
| 127 | + 'address' => '"testing-test...2"@xyz.com', |
| 128 | + 'original_address' => 'testing-"test...2"@xyz.com (comment)', |
| 129 | + 'name' => '', |
| 130 | + 'name_parsed' => '', |
| 131 | + 'local_part' => '"testing-test2"', |
| 132 | + 'local_part_parsed' => 'testing-test...2', |
| 133 | + 'domain_part' => 'xyz.com', |
| 134 | + 'domain' => 'xyz.com', |
| 135 | + 'ip' => '', |
| 136 | + 'invalid' => false, |
| 137 | + 'invalid_reason' => '') |
| 138 | + ) |
| 139 | + ); |
| 140 | +``` |
0 commit comments