| 946 | | } |
| 947 | | |
| 948 | | /** |
| 949 | | * Parse and validate a string containing one or more RFC822-style comma-separated email addresses |
| 950 | | * of the form "display name <address>" into an array of name/address pairs. |
| 951 | | * Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available. |
| 952 | | * Note that quotes in the name part are removed. |
| 953 | | * @param string $addrstr The address list string |
| 954 | | * @param bool $useimap Whether to use the IMAP extension to parse the list |
| 955 | | * @return array |
| 956 | | * @link http://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation |
| 957 | | */ |
| 958 | | public function parseAddresses($addrstr, $useimap = true) |
| 959 | | { |
| 960 | | $addresses = array(); |
| 961 | | if ($useimap and function_exists('imap_rfc822_parse_adrlist')) { |
| 962 | | //Use this built-in parser if it's available |
| 963 | | $list = imap_rfc822_parse_adrlist($addrstr, ''); |
| 964 | | foreach ($list as $address) { |
| 965 | | if ($address->host != '.SYNTAX-ERROR.') { |
| 966 | | if ($this->validateAddress($address->mailbox . '@' . $address->host)) { |
| 967 | | $addresses[] = array( |
| 968 | | 'name' => (property_exists($address, 'personal') ? $address->personal : ''), |
| 969 | | 'address' => $address->mailbox . '@' . $address->host |
| 970 | | ); |
| 971 | | } |
| 972 | | } |
| 973 | | } |
| 974 | | } else { |
| 975 | | //Use this simpler parser |
| 976 | | $list = explode(',', $addrstr); |
| 977 | | foreach ($list as $address) { |
| 978 | | $address = trim($address); |
| 979 | | //Is there a separate name part? |
| 980 | | if (strpos($address, '<') === false) { |
| 981 | | //No separate name, just use the whole thing |
| 982 | | if ($this->validateAddress($address)) { |
| 983 | | $addresses[] = array( |
| 984 | | 'name' => '', |
| 985 | | 'address' => $address |
| 986 | | ); |
| 987 | | } |
| 988 | | } else { |
| 989 | | list($name, $email) = explode('<', $address); |
| 990 | | $email = trim(str_replace('>', '', $email)); |
| 991 | | if ($this->validateAddress($email)) { |
| 992 | | $addresses[] = array( |
| 993 | | 'name' => trim(str_replace(array('"', "'"), '', $name)), |
| 994 | | 'address' => $email |
| 995 | | ); |
| 996 | | } |
| 997 | | } |
| 998 | | } |
| 999 | | } |
| 1000 | | return $addresses; |