PHPMailer\PHPMailer

PHPMailer::DKIM_Add()publicWP 1.0

Create the DKIM header and body in a new message header.

Метод класса: PHPMailer{}

Хуков нет.

Возвращает

Строку.

Использование

$PHPMailer = new PHPMailer();
$PHPMailer->DKIM_Add( $headers_line, $subject, $body );
$headers_line(строка) (обязательный)
Header lines
$subject(строка) (обязательный)
Subject
$body(строка) (обязательный)
Body

Код PHPMailer::DKIM_Add() WP 6.5.2

public function DKIM_Add($headers_line, $subject, $body)
{
    $DKIMsignatureType = 'rsa-sha256'; //Signature & hash algorithms
    $DKIMcanonicalization = 'relaxed/simple'; //Canonicalization methods of header & body
    $DKIMquery = 'dns/txt'; //Query method
    $DKIMtime = time();
    //Always sign these headers without being asked
    //Recommended list from https://tools.ietf.org/html/rfc6376#section-5.4.1
    $autoSignHeaders = [
        'from',
        'to',
        'cc',
        'date',
        'subject',
        'reply-to',
        'message-id',
        'content-type',
        'mime-version',
        'x-mailer',
    ];
    if (stripos($headers_line, 'Subject') === false) {
        $headers_line .= 'Subject: ' . $subject . static::$LE;
    }
    $headerLines = explode(static::$LE, $headers_line);
    $currentHeaderLabel = '';
    $currentHeaderValue = '';
    $parsedHeaders = [];
    $headerLineIndex = 0;
    $headerLineCount = count($headerLines);
    foreach ($headerLines as $headerLine) {
        $matches = [];
        if (preg_match('/^([^ \t]*?)(?::[ \t]*)(.*)$/', $headerLine, $matches)) {
            if ($currentHeaderLabel !== '') {
                //We were previously in another header; This is the start of a new header, so save the previous one
                $parsedHeaders[] = ['label' => $currentHeaderLabel, 'value' => $currentHeaderValue];
            }
            $currentHeaderLabel = $matches[1];
            $currentHeaderValue = $matches[2];
        } elseif (preg_match('/^[ \t]+(.*)$/', $headerLine, $matches)) {
            //This is a folded continuation of the current header, so unfold it
            $currentHeaderValue .= ' ' . $matches[1];
        }
        ++$headerLineIndex;
        if ($headerLineIndex >= $headerLineCount) {
            //This was the last line, so finish off this header
            $parsedHeaders[] = ['label' => $currentHeaderLabel, 'value' => $currentHeaderValue];
        }
    }
    $copiedHeaders = [];
    $headersToSignKeys = [];
    $headersToSign = [];
    foreach ($parsedHeaders as $header) {
        //Is this header one that must be included in the DKIM signature?
        if (in_array(strtolower($header['label']), $autoSignHeaders, true)) {
            $headersToSignKeys[] = $header['label'];
            $headersToSign[] = $header['label'] . ': ' . $header['value'];
            if ($this->DKIM_copyHeaderFields) {
                $copiedHeaders[] = $header['label'] . ':' . //Note no space after this, as per RFC
                    str_replace('|', '=7C', $this->DKIM_QP($header['value']));
            }
            continue;
        }
        //Is this an extra custom header we've been asked to sign?
        if (in_array($header['label'], $this->DKIM_extraHeaders, true)) {
            //Find its value in custom headers
            foreach ($this->CustomHeader as $customHeader) {
                if ($customHeader[0] === $header['label']) {
                    $headersToSignKeys[] = $header['label'];
                    $headersToSign[] = $header['label'] . ': ' . $header['value'];
                    if ($this->DKIM_copyHeaderFields) {
                        $copiedHeaders[] = $header['label'] . ':' . //Note no space after this, as per RFC
                            str_replace('|', '=7C', $this->DKIM_QP($header['value']));
                    }
                    //Skip straight to the next header
                    continue 2;
                }
            }
        }
    }
    $copiedHeaderFields = '';
    if ($this->DKIM_copyHeaderFields && count($copiedHeaders) > 0) {
        //Assemble a DKIM 'z' tag
        $copiedHeaderFields = ' z=';
        $first = true;
        foreach ($copiedHeaders as $copiedHeader) {
            if (!$first) {
                $copiedHeaderFields .= static::$LE . ' |';
            }
            //Fold long values
            if (strlen($copiedHeader) > self::STD_LINE_LENGTH - 3) {
                $copiedHeaderFields .= substr(
                    chunk_split($copiedHeader, self::STD_LINE_LENGTH - 3, static::$LE . self::FWS),
                    0,
                    -strlen(static::$LE . self::FWS)
                );
            } else {
                $copiedHeaderFields .= $copiedHeader;
            }
            $first = false;
        }
        $copiedHeaderFields .= ';' . static::$LE;
    }
    $headerKeys = ' h=' . implode(':', $headersToSignKeys) . ';' . static::$LE;
    $headerValues = implode(static::$LE, $headersToSign);
    $body = $this->DKIM_BodyC($body);
    //Base64 of packed binary SHA-256 hash of body
    $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body)));
    $ident = '';
    if ('' !== $this->DKIM_identity) {
        $ident = ' i=' . $this->DKIM_identity . ';' . static::$LE;
    }
    //The DKIM-Signature header is included in the signature *except for* the value of the `b` tag
    //which is appended after calculating the signature
    //https://tools.ietf.org/html/rfc6376#section-3.5
    $dkimSignatureHeader = 'DKIM-Signature: v=1;' .
        ' d=' . $this->DKIM_domain . ';' .
        ' s=' . $this->DKIM_selector . ';' . static::$LE .
        ' a=' . $DKIMsignatureType . ';' .
        ' q=' . $DKIMquery . ';' .
        ' t=' . $DKIMtime . ';' .
        ' c=' . $DKIMcanonicalization . ';' . static::$LE .
        $headerKeys .
        $ident .
        $copiedHeaderFields .
        ' bh=' . $DKIMb64 . ';' . static::$LE .
        ' b=';
    //Canonicalize the set of headers
    $canonicalizedHeaders = $this->DKIM_HeaderC(
        $headerValues . static::$LE . $dkimSignatureHeader
    );
    $signature = $this->DKIM_Sign($canonicalizedHeaders);
    $signature = trim(chunk_split($signature, self::STD_LINE_LENGTH - 3, static::$LE . self::FWS));

    return static::normalizeBreaks($dkimSignatureHeader . $signature);
}