envelopeFrom)) return $this->envelopeFrom; return implode(', ', array_keys($this->from)); } public function setEnvelopeFrom($value) { $this->envelopeFrom = $value; return $this; } private $envelopeTo; public function getEnvelopeTo() { if(isset($this->envelopeTo)) return $this->envelopeTo; $to_header = $this->getHeader('To'); if(!is_null($to_header)) return is_array($to_header) ? implode(',', array_keys($to_header)) : $to_header; return null; } public function setEnvelopeTo($value) { $this->envelopeTo = $value; return $this; } public function getReturnPath() { throw new \Exception("getReturnPath() is deprecated and should not be used"); } public function setReturnPath($dummy) { throw new \Exception("setReturnPath() is deprecated and should not be used"); } private function formatEmail($email, $name) { if($name) return [$email => $name]; require_once 'Mail/RFC822.php'; $rfc822 = new \Mail_RFC822; $structure = $rfc822->parseAddressList($email, ''); if(count($structure) == 0) throw new \ErrorException("no address found"); if(count($structure) != 1) throw new \ErrorException("multiple addresses found"); $structure = $structure[0]; if(array_key_exists('message', $structure)) throw new \ErrorException("address could not be validated: ".$structure['message']); if(!array_key_exists('mailbox', $structure)) throw new \ErrorException("not a plain address"); $name = preg_replace('/^"(.*[^\\\\])?"\z/', '$1', $structure->personal); return [$structure->mailbox . '@' . $structure->host => $name]; } public function setFrom($email, $name = null) { $this->setHeader('From', $this->formatEmail($email, $name)); return $this; } public function addReplyTo($email, $name = null) { $this->setHeader('Reply-To', $this->formatEmail($email, $name), true); return $this; } public function addTo($email, $name = null) { $this->setHeader('To', $this->formatEmail($email, $name), true); return $this; } public function addCc($email, $name = null) { $this->setHeader('Cc', $this->formatEmail($email, $name), true); return $this; } public function addBcc($email, $name = null) { $this->setHeader('Bcc', $this->formatEmail($email, $name), true); return $this; } public function generateMessage() { $message_id = $this->getHeader('Message-ID'); $built = $this->build(); if(is_null($message_id)) $built->setHeader('Message-ID', "<{$this->uuid4()}@{$this->cfg->messageid_host}>"); else $built->setHeader('Message-ID', $message_id); return $built->getEncodedMessage(); } public function uuid4() { $uuid = openssl_random_pseudo_bytes(16); $uuid[8] = chr(ord($uuid[8]) & 0x3F | 0x80); $uuid = bin2hex($uuid); $uuid[12] = 4; return preg_replace('/^.{8}|.{4}/', '$0-', $uuid, 4); } }