示例#1
0
 /**
  * Prepare message data for sending
  * @return $this Chaining
  */
 public function prepareEmail()
 {
     // Generate uniqid session id
     $this->session_id = md5(uniqid(time()));
     // Обработаем кирилицу в поле: От кого
     $from_user = $this->_encode($this->from_name, 'UTF-8');
     // Set MIME type
     $this->headers = '';
     //'MIME-Version: 1.0' . "\r\n";
     /*// Set subject header
       if (isset($this->subject{0})) {
           $this->headers = 'Subject: '.$this->encode($this->subject, 'UTF-8')."\r\n";
       }*/
     // Set from header
     $this->headers .= 'From: ' . $from_user . '<' . $this->from . '>' . "\r\n";
     // Copy header
     if (sizeof($this->cc)) {
         $this->headers .= 'Cc: ' . implode(',', $this->cc) . "\r\n";
     }
     // Set unsubscribe link if present
     $this->headers .= isset($this->unsubscribe[0]) ? 'List-Unsubscribe: <' . $this->unsubscribe . '>' . "\r\n" : '';
     // Set return path link if present
     $this->headers .= isset($this->returnTo[0]) ? 'Return-Path: ' . $this->returnTo . "\r\n" : '';
     $body = '';
     // If we have attachments
     if (sizeof($this->attachments)) {
         // Set mixed boundary header
         $this->headers .= 'Content-Type: multipart/mixed; boundary="mixed-' . $this->session_id . '"' . "\r\n" . "\r\n";
         // Start text boundary in message body
         $body .= '--mixed-' . $this->session_id . "\r\n";
         $body .= 'Content-type: text/html; charset="UTF-8"' . "\r\n";
         $body .= 'Content-Transfer-Encoding: 7bit' . "\r\n" . "\r\n";
     } else {
         // Simple HTML message - no boundary
         $this->headers .= 'Content-type: text/html; charset="UTF-8"' . "\r\n";
         $this->headers .= 'Content-Transfer-Encoding: 7bit' . "\r\n" . "\r\n";
     }
     // Make message as correct HTML
     $body .= $this->message . "\r\n";
     if (sizeof($this->attachments)) {
         $body .= "\r\n";
         // Iterate attached files
         foreach ($this->attachments as $file) {
             // If attached file exists
             if (file_exists($file)) {
                 // Generate attachment message part
                 $body .= '--mixed-' . $this->session_id . "\r\n";
                 $body .= 'Content-Type: ' . \samson\core\File::getMIME($file) . '; name="' . basename($file) . '"' . "\r\n";
                 $body .= 'Content-Transfer-Encoding: base64' . "\r\n";
                 $body .= 'Content-Disposition: attachment' . "\r\n" . "\r\n";
                 // Read and encode file content
                 $body .= chunk_split(base64_encode(file_get_contents($file))) . "\r\n";
                 $body .= "\r\n" . '--mixed' . $this->session_id . '--' . "\r\n";
             }
         }
     }
     $this->body = $body;
     $this->ready = true;
     return $this;
 }