public function PHPMailer::DKIM_Add

5.x PHPMailer.php public PHPMailer::DKIM_Add($headers_line, $subject, $body)

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

Parameters

string $headers_line Header lines:

string $subject Subject:

string $body Body:

Return value

string

1 call to PHPMailer::DKIM_Add()
PHPMailer::preSend in inc/PHPMailer/src/PHPMailer.php
Prepare a message for sending.

File

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

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public function DKIM_Add($headers_line, $subject, $body) 
 {
  $DKIMsignatureType = 'rsa-sha256'; // Signature & hash algorithms
  $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
  $DKIMquery = 'dns/txt'; // Query method
  $DKIMtime = time(); // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
  $subject_header = "Subject: $subject";
  $headers = explode(static::$LE, $headers_line);
  $from_header = '';
  $to_header = '';
  $date_header = '';
  $current = '';
  $copiedHeaderFields = '';
  $foundExtraHeaders = [];
  $extraHeaderKeys = '';
  $extraHeaderValues = '';
  $extraCopyHeaderFields = '';
  foreach ($headers as $header) {
    if (strpos($header, 'From:') === 0) {
      $from_header = $header;
      $current = 'from_header';
    }
    elseif (strpos($header, 'To:') === 0) {
      $to_header = $header;
      $current = 'to_header';
    }
    elseif (strpos($header, 'Date:') === 0) {
      $date_header = $header;
      $current = 'date_header';
    }
    elseif (!empty($this->DKIM_extraHeaders)) {
      foreach ($this->DKIM_extraHeaders as $extraHeader) {
        if (strpos($header, $extraHeader . ':') === 0) {
          $headerValue = $header;
          foreach ($this->CustomHeader as $customHeader) {
            if ($customHeader [0] === $extraHeader) {
              $headerValue = trim($customHeader [0]) .
                ': ' .
                $this->encodeHeader(trim($customHeader [1]));
              break;
            }
          }
          $foundExtraHeaders [$extraHeader] = $headerValue;
          $current = '';
          break;
        }
      }
    }
    else {
      if (!empty($$current) and strpos($header, ' =?') === 0) {
        $$current .= $header;
      }
      else {
        $current = '';
      }
    }
  }
  foreach ($foundExtraHeaders as $key => $value) {
    $extraHeaderKeys .= ':' . $key;
    $extraHeaderValues .= $value . "\r\n";
    if ($this->DKIM_copyHeaderFields) {
      $extraCopyHeaderFields .= ' |' . str_replace('|', '=7C', $this->DKIM_QP($value)) . ";\r\n";
    }
  }
  if ($this->DKIM_copyHeaderFields) {
    $from = str_replace('|', '=7C', $this->DKIM_QP($from_header));
    $to = str_replace('|', '=7C', $this->DKIM_QP($to_header));
    $date = str_replace('|', '=7C', $this->DKIM_QP($date_header));
    $subject = str_replace('|', '=7C', $this->DKIM_QP($subject_header));
    $copiedHeaderFields = " z=$from\r\n" .
      " |$to\r\n" .
      " |$date\r\n" .
      " |$subject;\r\n" .
      $extraCopyHeaderFields;
  }
  $body = $this->DKIM_BodyC($body);
  $DKIMlen = strlen($body); // Length of body
  $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body))); // Base64 of packed binary SHA-256 hash of body
  if ('' == $this->DKIM_identity) {
    $ident = '';
  }
  else {
    $ident = ' i=' . $this->DKIM_identity . ';';
  }
  $dkimhdrs = 'DKIM-Signature: v=1; a=' .
    $DKIMsignatureType . '; q=' .
    $DKIMquery . '; l=' .
    $DKIMlen . '; s=' .
    $this->DKIM_selector .
    ";\r\n" .
    ' t=' . $DKIMtime . '; c=' . $DKIMcanonicalization . ";\r\n" .
    ' h=From:To:Date:Subject' . $extraHeaderKeys . ";\r\n" .
    ' d=' . $this->DKIM_domain . ';' . $ident . "\r\n" .
    $copiedHeaderFields .
    ' bh=' . $DKIMb64 . ";\r\n" .
    ' b=';
  $toSign = $this->DKIM_HeaderC(
  $from_header . "\r\n" .
    $to_header . "\r\n" .
    $date_header . "\r\n" .
    $subject_header . "\r\n" .
    $extraHeaderValues .
    $dkimhdrs
    );
  $signed = $this->DKIM_Sign($toSign);

  return static::normalizeBreaks($dkimhdrs . $signed) . static::$LE;
}