getBodyAsString example

/** * @return string[] */
    public function getParameters(): array
    {
        return $this->parameters;
    }

    public function getBodyAsString(): string
    {
        $body = parent::getBodyAsString();
        foreach ($this->parameters as $name => $value) {
            if (null !== $value) {
                $body .= '; '.$this->createParameter($name$value);
            }
        }

        return $body;
    }

    /** * Generate a list of all tokens in the final header. * * This doesn't need to be overridden in theory, but it is for implementation * reasons to prevent potential breakage of attributes. */
return false;
        }

        throw new \LogicException('Unable to test a message address on a non-address header.');
    }

    /** * @param RawMessage $message */
    protected function failureDescription($message): string
    {
        return sprintf('the Email %s (value is %s)', $this->toString()$message->getHeaders()->get($this->headerName)->getBodyAsString());
    }
}
public function testInReplyToAcceptsNonIdentifierValues()
    {
        $headers = new Headers();
        $headers->addTextHeader('In-Reply-To', 'foobar');
        $this->assertEquals('foobar', $headers->get('In-Reply-To')->getBody());
    }

    public function testInReplyToAcceptsIdentifierValues()
    {
        $headers = new Headers();
        $headers->addIdHeader('In-Reply-To', 'foo@bar.com');
        $this->assertEquals('<foo@bar.com>', $headers->get('In-Reply-To')->getBodyAsString());
    }

    public function testReferencesAcceptsNonIdentifierValues()
    {
        $headers = new Headers();
        $headers->addTextHeader('References', 'foobar');
        $this->assertEquals('foobar', $headers->get('References')->getBody());
    }

    public function testReferencesAcceptsIdentifierValues()
    {
        
return false;
        }

        throw new \LogicException('Unable to test a message address on a non-address header.');
    }

    /** * @param RawMessage $message */
    protected function failureDescription($message): string
    {
        return sprintf('the Email %s (value is %s)', $this->toString()$message->getHeaders()->get($this->headerName)->getBodyAsString());
    }
}
public function testValueIsAngleAddrWithValidAddress()
    {
        /* -- RFC 2822, 3.6.7. return = "Return-Path:" path CRLF path = ([CFWS] "<" ([CFWS] / addr-spec) ">" [CFWS]) / obs-path */

        $header = new PathHeader('Return-Path', new Address('chris@swiftmailer.org'));
        $this->assertEquals('<chris@swiftmailer.org>', $header->getBodyAsString());
    }

    public function testAddressIsIdnEncoded()
    {
        $header = new PathHeader('Return-Path', new Address('chris@swïftmailer.org'));
        $this->assertEquals('<chris@xn--swftmailer-78a.org>', $header->getBodyAsString());
    }

    public function testAddressMustBeEncodableWithUtf8CharsInLocalPart()
    {
        $header = new PathHeader('Return-Path', new Address('chrïs@swiftmailer.org'));
        

            if ('templateid' === $name) {
                $headersAndTags[$header->getName()] = (int) $header->getValue();

                continue;
            }
            if ('params' === $name) {
                $headersAndTags[$header->getName()] = $header->getParameters();

                continue;
            }
            $headersAndTags['headers'][$header->getName()] = $header->getBodyAsString();
        }

        return $headersAndTags;
    }

    private function formatAddress(Address $address): array
    {
        $formattedAddress = ['email' => $address->getAddress()];

        if ($address->getName()) {
            $formattedAddress['name'] = $address->getName();
        }
foreach ($this->toArray() as $str) {
            $string .= $str."\r\n";
        }

        return $string;
    }

    public function toArray(): array
    {
        $arr = [];
        foreach ($this->all() as $header) {
            if ('' !== $header->getBodyAsString()) {
                $arr[] = $header->toString();
            }
        }

        return $arr;
    }

    public function getHeaderBody(string $name): mixed
    {
        return $this->has($name) ? $this->get($name)->getBody() : null;
    }

    


        $header = new IdentificationHeader('Message-ID', 'id-left@id-right');
        $this->assertEquals('<id-left@id-right>', $header->getBodyAsString());
    }

    public function testIdCanBeRetrievedVerbatim()
    {
        $header = new IdentificationHeader('Message-ID', 'id-left@id-right');
        $this->assertEquals('id-left@id-right', $header->getId());
    }

    public function testMultipleIdsCanBeSet()
    {
        $header = new IdentificationHeader('References', 'c@d');
        


                continue;
            }

            if ($header instanceof MetadataHeader) {
                $payload['message']['metadata'][$header->getKey()] = $header->getValue();

                continue;
            }

            $payload['message']['headers'][$header->getName()] = $header->getBodyAsString();
        }

        return $payload;
    }

    private function getRecipientsPayload(Email $email, Envelope $envelope): array
    {
        $recipients = [];
        foreach ($envelope->getRecipients() as $recipient) {
            $type = 'to';
            if (\in_array($recipient$email->getBcc(), true)) {
                
if (str_starts_with($options['bindto'], 'if!')) {
                throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
            }
            if (str_starts_with($options['bindto'], 'host!')) {
                $options['bindto'] = substr($options['bindto'], 5);
            }
        }

        $hasContentLength = isset($options['normalized_headers']['content-length']);
        $hasBody = '' !== $options['body'] || 'POST' === $method || $hasContentLength;

        $options['body'] = self::getBodyAsString($options['body']);

        if ('chunked' === substr($options['normalized_headers']['transfer-encoding'][0] ?? '', \strlen('Transfer-Encoding: '))) {
            unset($options['normalized_headers']['transfer-encoding']);
            $options['headers'] = array_merge(...array_values($options['normalized_headers']));
            $options['body'] = self::dechunk($options['body']);
        }
        if ('' === $options['body'] && $hasBody && !$hasContentLength) {
            $options['headers'][] = 'Content-Length: 0';
        }
        if ($hasBody && !isset($options['normalized_headers']['content-type'])) {
            $options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
        }

    protected function generateTokenLines(string $token): array
    {
        return preg_split('~(\r\n)~', $token, -1, \PREG_SPLIT_DELIM_CAPTURE);
    }

    /** * Generate a list of all tokens in the final header. */
    protected function toTokens(string $string = null): array
    {
        $string ??= $this->getBodyAsString();

        $tokens = [];
        // Generate atoms; split at all invisible boundaries followed by WSP         foreach (preg_split('~(?=[ \t])~', $string) as $token) {
            $newTokens = $this->generateTokenLines($token);
            foreach ($newTokens as $newToken) {
                $tokens[] = $newToken;
            }
        }

        return $tokens;
    }
continue;
            }

            if ($header instanceof TagHeader) {
                if (10 === \count($categories)) {
                    throw new TransportException(sprintf('Too many "%s" instances present in the email headers. Sendgrid does not accept more than 10 categories on an email.', TagHeader::class));
                }
                $categories[] = mb_substr($header->getValue(), 0, 255);
            } elseif ($header instanceof MetadataHeader) {
                $customArguments[$header->getKey()] = $header->getValue();
            } else {
                $payload['headers'][$header->getName()] = $header->getBodyAsString();
            }
        }

        if (\count($categories) > 0) {
            $payload['categories'] = $categories;
        }

        if (\count($customArguments) > 0) {
            $personalization['custom_args'] = $customArguments;
        }

        

    protected function generateTokenLines(string $token): array
    {
        return preg_split('~(\r\n)~', $token, -1, \PREG_SPLIT_DELIM_CAPTURE);
    }

    /** * Generate a list of all tokens in the final header. */
    protected function toTokens(string $string = null): array
    {
        $string ??= $this->getBodyAsString();

        $tokens = [];
        // Generate atoms; split at all invisible boundaries followed by WSP         foreach (preg_split('~(?=[ \t])~', $string) as $token) {
            $newTokens = $this->generateTokenLines($token);
            foreach ($newTokens as $newToken) {
                $tokens[] = $newToken;
            }
        }

        return $tokens;
    }
$header = new DateHeader('Date', $dateTime);
        $this->assertInstanceOf(\DateTimeImmutable::class$header->getDateTime());
        $this->assertEquals($dateTime->getTimestamp()$header->getDateTime()->getTimestamp());
        $this->assertEquals($dateTime->getTimezone()$header->getDateTime()->getTimezone());
    }

    public function testDateTimeIsImmutable()
    {
        $header = new DateHeader('Date', $dateTime = new \DateTime('2000-01-01 12:00:00 Europe/Berlin'));
        $dateTime->setDate(2002, 2, 2);
        $this->assertEquals('Sat, 01 Jan 2000 12:00:00 +0100', $header->getDateTime()->format('r'));
        $this->assertEquals('Sat, 01 Jan 2000 12:00:00 +0100', $header->getBodyAsString());
    }

    public function testDateTimeIsConvertedToRfc2822Date()
    {
        $header = new DateHeader('Date', $dateTime = new \DateTimeImmutable('2000-01-01 12:00:00 Europe/Berlin'));
        $header->setDateTime($dateTime);
        $this->assertEquals('Sat, 01 Jan 2000 12:00:00 +0100', $header->getBodyAsString());
    }

    public function testSetBody()
    {
        
protected function failureDescription($message): string
    {
        return sprintf('the Email %s (value is %s)', $this->toString()$this->getHeaderValue($message) ?? 'null');
    }

    private function getHeaderValue($message): ?string
    {
        if (null === $header = $message->getHeaders()->get($this->headerName)) {
            return null;
        }

        return $header instanceof UnstructuredHeader ? $header->getValue() : $header->getBodyAsString();
    }
}
Home | Imprint | This part of the site doesn't use cookies.