public function PHPMailer::createHeader

5.x PHPMailer.php public PHPMailer::createHeader()

Assemble message headers.

Return value

string The assembled headers

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

File

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

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public function createHeader() 
 {
  $result = '';

  $result .= $this->headerLine('Date', '' == $this->MessageDate ? self::rfcDate() : $this->MessageDate);

  // To be created automatically by mail()
  if ($this->SingleTo) {
    if ('mail' != $this->Mailer) {
      foreach ($this->to as $toaddr) {
        $this->SingleToArray [] = $this->addrFormat($toaddr);
      }
    }
  }
  else {
    if (count($this->to) > 0) {
      if ('mail' != $this->Mailer) {
        $result .= $this->addrAppend('To', $this->to);
      }
    }
    elseif (count($this->cc) == 0) {
      $result .= $this->headerLine('To', 'undisclosed-recipients:;');
    }
  }

  $result .= $this->addrAppend('From', [[trim($this->From), $this->FromName]]);

  // sendmail and mail() extract Cc from the header before sending
  if (count($this->cc) > 0) {
    $result .= $this->addrAppend('Cc', $this->cc);
  }

  // sendmail and mail() extract Bcc from the header before sending
  if ((
  'sendmail' == $this->Mailer or 'qmail' == $this->Mailer or 'mail' == $this->Mailer
    )
     and count($this->bcc) > 0
    ) {
    $result .= $this->addrAppend('Bcc', $this->bcc);
  }

  if (count($this->ReplyTo) > 0) {
    $result .= $this->addrAppend('Reply-To', $this->ReplyTo);
  }

  // mail() sets the subject itself
  if ('mail' != $this->Mailer) {
    $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject)));
  }

  // Only allow a custom message ID if it conforms to RFC 5322 section 3.6.4
  // https://tools.ietf.org/html/rfc5322#section-3.6.4
  if ('' != $this->MessageID and preg_match('/^<.*@.*>$/', $this->MessageID)) {
    $this->lastMessageID = $this->MessageID;
  }
  else {
    $this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->serverHostname());
  }
  $result .= $this->headerLine('Message-ID', $this->lastMessageID);
  if (null !== $this->Priority) {
    $result .= $this->headerLine('X-Priority', $this->Priority);
  }
  if ('' == $this->XMailer) {
    $result .= $this->headerLine(
    'X-Mailer', 
    'PHPMailer ' . self::VERSION . ' (https://github.com/PHPMailer/PHPMailer)'
    );
  }
  else {
    $myXmailer = trim($this->XMailer);
    if ($myXmailer) {
      $result .= $this->headerLine('X-Mailer', $myXmailer);
    }
  }

  if ('' != $this->ConfirmReadingTo) {
    $result .= $this->headerLine('Disposition-Notification-To', '<' . $this->ConfirmReadingTo . '>');
  }

  // Add custom headers
  foreach ($this->CustomHeader as $header) {
    $result .= $this->headerLine(
    trim($header [0]), 
    $this->encodeHeader(trim($header [1]))
    );
  }
  if (!$this->sign_key_file) {
    $result .= $this->headerLine('MIME-Version', '1.0');
    $result .= $this->getMailMIME();
  }

  return $result;
}