PHPMailer\PHPMailer

PHPMailer::attachAll()protectedWP 1.0

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

Method of the class: PHPMailer{}

No Hooks.

Return

String.

Usage

// protected - for code of main (parent) or child class
$result = $this->attachAll( $disposition_type, $boundary );
$disposition_type(string) (required)
-
$boundary(string) (required)
-

PHPMailer::attachAll() code WP 6.5.2

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, true)) {
                continue;
            }
            $incl[] = $inclhash;
            $name = $attachment[2];
            $encoding = $attachment[3];
            $type = $attachment[4];
            $disposition = $attachment[6];
            $cid = $attachment[7];
            if ('inline' === $disposition && 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,
                    static::quotedString($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);
            }

            //Only set Content-IDs on inline attachments
            if ((string) $cid !== '' && $disposition === 'inline') {
                $mime[] = 'Content-ID: <' . $this->encodeHeader($this->secureHeader($cid)) . '>' . static::$LE;
            }

            //Allow for bypassing the Content-Disposition header
            if (!empty($disposition)) {
                $encoded_name = $this->encodeHeader($this->secureHeader($name));
                if (!empty($encoded_name)) {
                    $mime[] = sprintf(
                        'Content-Disposition: %s; filename=%s%s',
                        $disposition,
                        static::quotedString($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);
}