PHPMailer::createHeader PHP Method

createHeader() public method

Assemble message headers.
public createHeader ( ) : string
return string The assembled headers
    public function createHeader()
    {
        $result = '';
        if ($this->MessageDate == '') {
            $this->MessageDate = self::rfcDate();
        }
        $result .= $this->headerLine('Date', $this->MessageDate);
        // To be created automatically by mail()
        if ($this->SingleTo) {
            if ($this->Mailer != 'mail') {
                foreach ($this->to as $toaddr) {
                    $this->SingleToArray[] = $this->addrFormat($toaddr);
                }
            }
        } else {
            if (count($this->to) > 0) {
                if ($this->Mailer != 'mail') {
                    $result .= $this->addrAppend('To', $this->to);
                }
            } elseif (count($this->cc) == 0) {
                $result .= $this->headerLine('To', 'undisclosed-recipients:;');
            }
        }
        $result .= $this->addrAppend('From', array(array(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 (($this->Mailer == 'sendmail' or $this->Mailer == 'qmail' or $this->Mailer == 'mail') 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 ($this->Mailer != 'mail') {
            $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 (!is_null($this->Priority)) {
            $result .= $this->headerLine('X-Priority', $this->Priority);
        }
        if ($this->XMailer == '') {
            $result .= $this->headerLine('X-Mailer', 'PHPMailer ' . $this->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;
    }

Usage Example

Example #1
0
 /**
  * Miscellaneous calls to improve test coverage and some small tests.
  */
 public function testMiscellaneous()
 {
     $this->assertEquals('application/pdf', PHPMailer::_mime_types('pdf'), 'MIME TYPE lookup failed');
     $this->Mail->addCustomHeader('SomeHeader: Some Value');
     $this->Mail->clearCustomHeaders();
     $this->Mail->clearAttachments();
     $this->Mail->isHTML(false);
     $this->Mail->isSMTP();
     $this->Mail->isMail();
     $this->Mail->isSendmail();
     $this->Mail->isQmail();
     $this->Mail->setLanguage('fr');
     $this->Mail->Sender = '';
     $this->Mail->createHeader();
     $this->assertFalse($this->Mail->set('x', 'y'), 'Invalid property set succeeded');
     $this->assertTrue($this->Mail->set('Timeout', 11), 'Valid property set failed');
     $this->assertTrue($this->Mail->set('AllowEmpty', null), 'Null property set failed');
     $this->assertTrue($this->Mail->set('AllowEmpty', false), 'Valid property set of null property failed');
     //Test pathinfo
     $a = '/mnt/files/飛兒樂 團光茫.mp3';
     $q = PHPMailer::mb_pathinfo($a);
     $this->assertEquals($q['dirname'], '/mnt/files', 'UNIX dirname not matched');
     $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'UNIX basename not matched');
     $this->assertEquals($q['extension'], 'mp3', 'UNIX extension not matched');
     $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'UNIX filename not matched');
     $this->assertEquals(PHPMailer::mb_pathinfo($a, PATHINFO_DIRNAME), '/mnt/files', 'Dirname path element not matched');
     $this->assertEquals(PHPMailer::mb_pathinfo($a, PATHINFO_BASENAME), '飛兒樂 團光茫.mp3', 'Basename path element not matched');
     $this->assertEquals(PHPMailer::mb_pathinfo($a, 'filename'), '飛兒樂 團光茫', 'Filename path element not matched');
     $a = 'c:\\mnt\\files\\飛兒樂 團光茫.mp3';
     $q = PHPMailer::mb_pathinfo($a);
     $this->assertEquals($q['dirname'], 'c:\\mnt\\files', 'Windows dirname not matched');
     $this->assertEquals($q['basename'], '飛兒樂 團光茫.mp3', 'Windows basename not matched');
     $this->assertEquals($q['extension'], 'mp3', 'Windows extension not matched');
     $this->assertEquals($q['filename'], '飛兒樂 團光茫', 'Windows filename not matched');
     $this->assertEquals(PHPMailer::filenameToType('abc.jpg?xyz=1'), 'image/jpeg', 'Query string not ignored in filename');
     $this->assertEquals(PHPMailer::filenameToType('abc.xyzpdq'), 'application/octet-stream', 'Default MIME type not applied to unknown extension');
     //Line break normalization
     $eol = $this->Mail->LE;
     $b1 = "1\r2\r3\r";
     $b2 = "1\n2\n3\n";
     $b3 = "1\r\n2\r3\n";
     $this->Mail->LE = "\n";
     $t1 = "1{$this->Mail->LE}2{$this->Mail->LE}3{$this->Mail->LE}";
     $this->assertEquals($this->Mail->fixEOL($b1), $t1, 'Failed to normalize line breaks (1)');
     $this->assertEquals($this->Mail->fixEOL($b2), $t1, 'Failed to normalize line breaks (2)');
     $this->assertEquals($this->Mail->fixEOL($b3), $t1, 'Failed to normalize line breaks (3)');
     $this->Mail->LE = "\r\n";
     $t1 = "1{$this->Mail->LE}2{$this->Mail->LE}3{$this->Mail->LE}";
     $this->assertEquals($this->Mail->fixEOL($b1), $t1, 'Failed to normalize line breaks (4)');
     $this->assertEquals($this->Mail->fixEOL($b2), $t1, 'Failed to normalize line breaks (5)');
     $this->assertEquals($this->Mail->fixEOL($b3), $t1, 'Failed to normalize line breaks (6)');
     $this->Mail->LE = $eol;
 }
All Usage Examples Of PHPMailer::createHeader