PHPMailer\PHPMailer

PHPMailer::createBody()publicWP 1.0

Assemble the message body. Returns an empty string on failure.

Method of the class: PHPMailer{}

No Hooks.

Return

String. The assembled message body

Usage

$PHPMailer = new PHPMailer();
$PHPMailer->createBody();

PHPMailer::createBody() code WP 6.4.3

public function createBody()
{
    $body = '';
    //Create unique IDs and preset boundaries
    $this->setBoundaries();

    if ($this->sign_key_file) {
        $body .= $this->getMailMIME() . static::$LE;
    }

    $this->setWordWrap();

    $bodyEncoding = $this->Encoding;
    $bodyCharSet = $this->CharSet;
    //Can we do a 7-bit downgrade?
    if (static::ENCODING_8BIT === $bodyEncoding && !$this->has8bitChars($this->Body)) {
        $bodyEncoding = static::ENCODING_7BIT;
        //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit
        $bodyCharSet = static::CHARSET_ASCII;
    }
    //If lines are too long, and we're not already using an encoding that will shorten them,
    //change to quoted-printable transfer encoding for the body part only
    if (static::ENCODING_BASE64 !== $this->Encoding && static::hasLineLongerThanMax($this->Body)) {
        $bodyEncoding = static::ENCODING_QUOTED_PRINTABLE;
    }

    $altBodyEncoding = $this->Encoding;
    $altBodyCharSet = $this->CharSet;
    //Can we do a 7-bit downgrade?
    if (static::ENCODING_8BIT === $altBodyEncoding && !$this->has8bitChars($this->AltBody)) {
        $altBodyEncoding = static::ENCODING_7BIT;
        //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit
        $altBodyCharSet = static::CHARSET_ASCII;
    }
    //If lines are too long, and we're not already using an encoding that will shorten them,
    //change to quoted-printable transfer encoding for the alt body part only
    if (static::ENCODING_BASE64 !== $altBodyEncoding && static::hasLineLongerThanMax($this->AltBody)) {
        $altBodyEncoding = static::ENCODING_QUOTED_PRINTABLE;
    }
    //Use this as a preamble in all multipart message types
    $mimepre = '';
    switch ($this->message_type) {
        case 'inline':
            $body .= $mimepre;
            $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding);
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            $body .= $this->attachAll('inline', $this->boundary[1]);
            break;
        case 'attach':
            $body .= $mimepre;
            $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding);
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            $body .= $this->attachAll('attachment', $this->boundary[1]);
            break;
        case 'inline_attach':
            $body .= $mimepre;
            $body .= $this->textLine('--' . $this->boundary[1]);
            $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
            $body .= $this->textLine(' boundary="' . $this->boundary[2] . '";');
            $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"');
            $body .= static::$LE;
            $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding);
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            $body .= $this->attachAll('inline', $this->boundary[2]);
            $body .= static::$LE;
            $body .= $this->attachAll('attachment', $this->boundary[1]);
            break;
        case 'alt':
            $body .= $mimepre;
            $body .= $this->getBoundary(
                $this->boundary[1],
                $altBodyCharSet,
                static::CONTENT_TYPE_PLAINTEXT,
                $altBodyEncoding
            );
            $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
            $body .= static::$LE;
            $body .= $this->getBoundary(
                $this->boundary[1],
                $bodyCharSet,
                static::CONTENT_TYPE_TEXT_HTML,
                $bodyEncoding
            );
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            if (!empty($this->Ical)) {
                $method = static::ICAL_METHOD_REQUEST;
                foreach (static::$IcalMethods as $imethod) {
                    if (stripos($this->Ical, 'METHOD:' . $imethod) !== false) {
                        $method = $imethod;
                        break;
                    }
                }
                $body .= $this->getBoundary(
                    $this->boundary[1],
                    '',
                    static::CONTENT_TYPE_TEXT_CALENDAR . '; method=' . $method,
                    ''
                );
                $body .= $this->encodeString($this->Ical, $this->Encoding);
                $body .= static::$LE;
            }
            $body .= $this->endBoundary($this->boundary[1]);
            break;
        case 'alt_inline':
            $body .= $mimepre;
            $body .= $this->getBoundary(
                $this->boundary[1],
                $altBodyCharSet,
                static::CONTENT_TYPE_PLAINTEXT,
                $altBodyEncoding
            );
            $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
            $body .= static::$LE;
            $body .= $this->textLine('--' . $this->boundary[1]);
            $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
            $body .= $this->textLine(' boundary="' . $this->boundary[2] . '";');
            $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"');
            $body .= static::$LE;
            $body .= $this->getBoundary(
                $this->boundary[2],
                $bodyCharSet,
                static::CONTENT_TYPE_TEXT_HTML,
                $bodyEncoding
            );
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            $body .= $this->attachAll('inline', $this->boundary[2]);
            $body .= static::$LE;
            $body .= $this->endBoundary($this->boundary[1]);
            break;
        case 'alt_attach':
            $body .= $mimepre;
            $body .= $this->textLine('--' . $this->boundary[1]);
            $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
            $body .= $this->textLine(' boundary="' . $this->boundary[2] . '"');
            $body .= static::$LE;
            $body .= $this->getBoundary(
                $this->boundary[2],
                $altBodyCharSet,
                static::CONTENT_TYPE_PLAINTEXT,
                $altBodyEncoding
            );
            $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
            $body .= static::$LE;
            $body .= $this->getBoundary(
                $this->boundary[2],
                $bodyCharSet,
                static::CONTENT_TYPE_TEXT_HTML,
                $bodyEncoding
            );
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            if (!empty($this->Ical)) {
                $method = static::ICAL_METHOD_REQUEST;
                foreach (static::$IcalMethods as $imethod) {
                    if (stripos($this->Ical, 'METHOD:' . $imethod) !== false) {
                        $method = $imethod;
                        break;
                    }
                }
                $body .= $this->getBoundary(
                    $this->boundary[2],
                    '',
                    static::CONTENT_TYPE_TEXT_CALENDAR . '; method=' . $method,
                    ''
                );
                $body .= $this->encodeString($this->Ical, $this->Encoding);
            }
            $body .= $this->endBoundary($this->boundary[2]);
            $body .= static::$LE;
            $body .= $this->attachAll('attachment', $this->boundary[1]);
            break;
        case 'alt_inline_attach':
            $body .= $mimepre;
            $body .= $this->textLine('--' . $this->boundary[1]);
            $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';');
            $body .= $this->textLine(' boundary="' . $this->boundary[2] . '"');
            $body .= static::$LE;
            $body .= $this->getBoundary(
                $this->boundary[2],
                $altBodyCharSet,
                static::CONTENT_TYPE_PLAINTEXT,
                $altBodyEncoding
            );
            $body .= $this->encodeString($this->AltBody, $altBodyEncoding);
            $body .= static::$LE;
            $body .= $this->textLine('--' . $this->boundary[2]);
            $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';');
            $body .= $this->textLine(' boundary="' . $this->boundary[3] . '";');
            $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"');
            $body .= static::$LE;
            $body .= $this->getBoundary(
                $this->boundary[3],
                $bodyCharSet,
                static::CONTENT_TYPE_TEXT_HTML,
                $bodyEncoding
            );
            $body .= $this->encodeString($this->Body, $bodyEncoding);
            $body .= static::$LE;
            $body .= $this->attachAll('inline', $this->boundary[3]);
            $body .= static::$LE;
            $body .= $this->endBoundary($this->boundary[2]);
            $body .= static::$LE;
            $body .= $this->attachAll('attachment', $this->boundary[1]);
            break;
        default:
            //Catch case 'plain' and case '', applies to simple `text/plain` and `text/html` body content types
            //Reset the `Encoding` property in case we changed it for line length reasons
            $this->Encoding = $bodyEncoding;
            $body .= $this->encodeString($this->Body, $this->Encoding);
            break;
    }

    if ($this->isError()) {
        $body = '';
        if ($this->exceptions) {
            throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL);
        }
    } elseif ($this->sign_key_file) {
        try {
            if (!defined('PKCS7_TEXT')) {
                throw new Exception($this->lang('extension_missing') . 'openssl');
            }

            $file = tempnam(sys_get_temp_dir(), 'srcsign');
            $signed = tempnam(sys_get_temp_dir(), 'mailsign');
            file_put_contents($file, $body);

            //Workaround for PHP bug https://bugs.php.net/bug.php?id=69197
            if (empty($this->sign_extracerts_file)) {
                $sign = @openssl_pkcs7_sign(
                    $file,
                    $signed,
                    'file://' . realpath($this->sign_cert_file),
                    ['file://' . realpath($this->sign_key_file), $this->sign_key_pass],
                    []
                );
            } else {
                $sign = @openssl_pkcs7_sign(
                    $file,
                    $signed,
                    'file://' . realpath($this->sign_cert_file),
                    ['file://' . realpath($this->sign_key_file), $this->sign_key_pass],
                    [],
                    PKCS7_DETACHED,
                    $this->sign_extracerts_file
                );
            }

            @unlink($file);
            if ($sign) {
                $body = file_get_contents($signed);
                @unlink($signed);
                //The message returned by openssl contains both headers and body, so need to split them up
                $parts = explode("\n\n", $body, 2);
                $this->MIMEHeader .= $parts[0] . static::$LE . static::$LE;
                $body = $parts[1];
            } else {
                @unlink($signed);
                throw new Exception($this->lang('signing') . openssl_error_string());
            }
        } catch (Exception $exc) {
            $body = '';
            if ($this->exceptions) {
                throw $exc;
            }
        }
    }

    return $body;
}