protected function PHPMailer::attachAll

5.x PHPMailer.php protected PHPMailer::attachAll($disposition_type, $boundary)

Attach all file, string, and binary attachments to the message. Returns an empty string on failure.

Parameters

string $disposition_type:

string $boundary:

Return value

string

1 call to PHPMailer::attachAll()
PHPMailer::createBody in inc/PHPMailer/src/PHPMailer.php
Assemble the message body. Returns an empty string on failure.

File

inc/PHPMailer/src/PHPMailer.php, line 2901

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

protected function attachAll($disposition_type, $boundary) 
 {
  // Return text of body
  $mime = [];
  $cidUniq = [];
  $incl = [];

  // Add all attachments
  foreach ($this->attachment as $attachment) {
    // Check if it is a valid disposition_filter
    if ($attachment [6] == $disposition_type) {
      // Check for string attachment
      $string = '';
      $path = '';
      $bString = $attachment [5];
      if ($bString) {
        $string = $attachment [0];
      }
      else {
        $path = $attachment [0];
      }

      $inclhash = hash('sha256', serialize($attachment));
      if (in_array($inclhash, $incl)) {
        continue;
      }
      $incl [] = $inclhash;
      $name = $attachment [2];
      $encoding = $attachment [3];
      $type = $attachment [4];
      $disposition = $attachment [6];
      $cid = $attachment [7];
      if ('inline' == $disposition and array_key_exists($cid, $cidUniq)) {
        continue;
      }
      $cidUniq [$cid] = true;

      $mime [] = sprintf('--%s%s', $boundary, static::$LE);
      //Only include a filename property if we have one
      if (!empty($name)) {
        $mime [] = sprintf(
        'Content-Type: %s; name="%s"%s', 
        $type, 
        $this->encodeHeader($this->secureHeader($name)), 
        static::$LE
        );
      }
      else {
        $mime [] = sprintf(
        'Content-Type: %s%s', 
        $type, 
        static::$LE
        );
      }
      // RFC1341 part 5 says 7bit is assumed if not specified
      if (static::ENCODING_7BIT != $encoding) {
        $mime [] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, static::$LE);
      }

      if (!empty($cid)) {
        $mime [] = sprintf(
        'Content-ID: <%s>%s', 
        $this->encodeHeader($this->secureHeader($cid)), 
        static::$LE
        );
      }

      // If a filename contains any of these chars, it should be quoted,
      // but not otherwise: RFC2183 & RFC2045 5.1
      // Fixes a warning in IETF's msglint MIME checker
      // Allow for bypassing the Content-Disposition header totally
      if (!empty($disposition)) {
        $encoded_name = $this->encodeHeader($this->secureHeader($name));
        if (preg_match('/[ \(\)<>@,;:\\"\/\[\]\?=]/', $encoded_name)) {
          $mime [] = sprintf(
          'Content-Disposition: %s; filename="%s"%s', 
          $disposition, 
          $encoded_name, 
          static::$LE . static::$LE
          );
        }
        else {
          if (!empty($encoded_name)) {
            $mime [] = sprintf(
            'Content-Disposition: %s; filename=%s%s', 
            $disposition, 
            $encoded_name, 
            static::$LE . static::$LE
            );
          }
          else {
            $mime [] = sprintf(
            'Content-Disposition: %s%s', 
            $disposition, 
            static::$LE . static::$LE
            );
          }
        }
      }
      else {
        $mime [] = static::$LE;
      }

      // Encode as string attachment
      if ($bString) {
        $mime [] = $this->encodeString($string, $encoding);
      }
      else {
        $mime [] = $this->encodeFile($path, $encoding);
      }
      if ($this->isError()) {
        return '';
      }
      $mime [] = static::$LE;
    }
  }

  $mime [] = sprintf('--%s--%s', $boundary, static::$LE);

  return implode('', $mime);
}