/** * Sends the message and returns a boolean value indicating whether or not * the sending took place. The reasons why this method might return a false * value include the following: * * 1) This system does not support sending email via PHP's mail() function * 2) This email was already sent, without the subject, message or * attachment content having been changed * 3) PHP's mail() function returned a false value * * @return boolean */ public function mail() { $attachmentCount = count($this->_attachmentContent); if ($this->_sent) { return false; } elseif (PFX_UNIT_TEST) { $GLOBALS['__lastEmailSent'] = array('sent_time' => time(), 'to' => $this->getRecipient(), 'subject' => $this->getSubject(), 'from' => $this->getFromAddress(), 'from_name' => $this->getFromName(), 'reply_to' => $this->getReplyTo(), 'charset' => $this->getCharset(), 'message' => $this->getMessage(), 'attachment_filenames' => $this->_attachmentFilenames, 'attachment_content' => array()); foreach ($this->_attachmentContent as $attachment) { $GLOBALS['__lastEmailSent']['attachment_content'][] = base64_decode($attachment); } $result = true; } elseif (!PFXUtils::hasMail()) { echo 'Mailable content (' . $attachmentCount . ' attachments):' . PHP_EOL . PHP_EOL . $this->_message . PHP_EOL; if ($attachmentCount) { $cwd = getcwd(); echo 'Attempting to dump attachments in ' . $cwd . PHP_EOL; for ($i = 0; $i < $attachmentCount; $i++) { $file = $cwd . DIRECTORY_SEPARATOR . $this->_attachmentFilenames[$i]; file_put_contents($file, base64_decode($this->_attachmentContent[$i])); } } $result = false; } else { $uid = uniqid(); $content = sprintf("From: %s <%s>\r\nReply-To: %s\r\nMIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed; boundary=\"%s\"\r\n" . "This is a multi-part message in MIME format.\r\n" . "--%s\r\nContent-type: text/plain; charset=%s\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n%s\r\n", $this->getFromName(), $this->getFromAddress(), $this->getReplyTo(), $uid, $uid, $this->getCharset(), $this->getMessage()); for ($i = 0; $i < $attachmentCount; $i++) { $content .= sprintf("--%s\r\n" . "Content-Type: application/octet-stream; name=\"%s\"\r\n" . "Content-Transfer-Encoding: base64\r\n" . "Content-Disposition: attachment; filename=\"%s\"\r\n" . "\r\n%s\r\n", $uid, $this->_attachmentFilenames[$i], $this->_attachmentFilenames[$i], $this->_attachmentContent[$i]); } $content .= '--' . $uid . '--'; $result = mail($this->getRecipient(), $this->getSubject(), '', $content); } $this->_sent = true; return $result; }