getTextBody example



    /** * @param RawMessage $message */
    protected function matches($message): bool
    {
        if (RawMessage::class === $message::class || Message::class === $message::class) {
            throw new \LogicException('Unable to test a message text body on a RawMessage or Message instance.');
        }

        return str_contains($message->getTextBody()$this->expectedText);
    }

    /** * @param RawMessage $message */
    protected function failureDescription($message): string
    {
        return 'the Email text body '.$this->toString();
    }
}

        $handler->handleBatch($records);
    }

    public function testHtmlContent()
    {
        $handler = new MailerHandler($this->mailer, (new Email())->subject('Alert: %level_name% %message%'));
        $handler->setFormatter(new HtmlFormatter());
        $this->mailer
            ->expects($this->once())
            ->method('send')
            ->with($this->callback(fn (Email $email) => 'Alert: WARNING message' === $email->getSubject() && null === $email->getTextBody()))
        ;
        $handler->handle($this->getRecord(Logger::WARNING, 'message'));
    }

    protected function getRecord($level = Logger::WARNING, $message = 'test', $context = []): array|LogRecord
    {
        return RecordFactory::create($level$message, context: $context);
    }

    protected function getMultipleRecords(): array
    {
        
private function getEndpoint(): ?string
    {
        return ($this->host ?: self::HOST).($this->port ? ':'.$this->port : '');
    }

    private function getPayload(Email $email, Envelope $envelope): array
    {
        $payload = [
            'key' => $this->key,
            'message' => [
                'html' => $email->getHtmlBody(),
                'text' => $email->getTextBody(),
                'subject' => $email->getSubject(),
                'from_email' => $envelope->getSender()->getAddress(),
                'to' => $this->getRecipientsPayload($email$envelope),
            ],
        ];

        if ('' !== $envelope->getSender()->getName()) {
            $payload['message']['from_name'] = $envelope->getSender()->getName();
        }

        foreach ($email->getAttachments() as $attachment) {
            
$payload['reply_to'] = current($this->prepareAddresses($replyTo));
        }

        if ($cc = $email->getCc()) {
            $payload['cc'] = $this->prepareAddresses($cc);
        }

        if ($bcc = $email->getBcc()) {
            $payload['bcc'] = $this->prepareAddresses($bcc);
        }

        if ($email->getTextBody()) {
            $payload['text'] = $email->getTextBody();
        }

        if ($email->getHtmlBody()) {
            $payload['html'] = $email->getHtmlBody();
        }

        return $payload;
    }

    /** * @param Address[] $addresses */
$payload['attachment'] = $attachements;
        }
        if ($emails = $email->getReplyTo()) {
            $payload['replyTo'] = current($this->stringifyAddresses($emails));
        }
        if ($emails = $email->getCc()) {
            $payload['cc'] = $this->stringifyAddresses($emails);
        }
        if ($emails = $email->getBcc()) {
            $payload['bcc'] = $this->stringifyAddresses($emails);
        }
        if ($email->getTextBody()) {
            $payload['textContent'] = $email->getTextBody();
        }
        if ($email->getHtmlBody()) {
            $payload['htmlContent'] = $email->getHtmlBody();
        }
        if ($headersAndTags = $this->prepareHeadersAndTags($email->getHeaders())) {
            $payload = array_merge($payload$headersAndTags);
        }

        return $payload;
    }

    
$transport = (new FakeSmsEmailTransport($mailer$to = 'recipient@email.net', $from = 'sender@email.net'));
        $transport->setHost($transportName);

        $transport->send($message);

        /** @var Email $sentEmail */
        $sentEmail = $mailer->getSentEmail();
        $this->assertInstanceOf(Email::class$sentEmail);
        $this->assertSame($to$sentEmail->getTo()[0]->getEncodedAddress());
        $this->assertSame($from$sentEmail->getFrom()[0]->getEncodedAddress());
        $this->assertSame(sprintf('New SMS on phone number: %s', $phone)$sentEmail->getSubject());
        $this->assertSame($subject$sentEmail->getTextBody());
        $this->assertFalse($sentEmail->getHeaders()->has('X-Transport'));
    }

    public function testSendWithCustomTransport()
    {
        $transportName = 'mailchimp';

        $message = new SmsMessage($phone = '0611223344', $subject = 'Hello!');

        $mailer = new DummyMailer();

        
'points' => 3,
        ]);
        $this->getContainer()->get(ProductReviewSaveRoute::class)->save(
            $this->ids->get('product'),
            $data,
            $salesChannelContext
        );

        $this->resetEventDispatcher();

        static::assertInstanceOf(MailBeforeSentEvent::class$caughtEvent);
        $bodyText = $caughtEvent->getMessage()->getTextBody();
        $bodyText = \is_string($bodyText) ? $bodyText : '';
        static::assertStringContainsString($data->get('title')$bodyText);
        static::assertStringContainsString($data->get('content')$bodyText);
        static::assertStringContainsString($this->ids->get('unique-name')$bodyText);
    }

    private function assertReviewCount(int $expected): void
    {
        $count = $this->getContainer()
            ->get(Connection::class)
            ->fetchOne('SELECT COUNT(*) FROM product_review WHERE product_id = :id', ['id' => Uuid::fromHexToBytes($this->ids->get('product'))]);

        


    /** * @param array<int, string[]> $productDownloads */
    private function assertDispatchedMailEvent(array $productDownloads, ?MailBeforeSentEvent $mailEvent): void
    {
        static::assertInstanceOf(MailBeforeSentEvent::class$mailEvent);

        $files = array_merge(...$productDownloads);
        foreach ($files as $file) {
            static::assertIsString($mailEvent->getMessage()->getTextBody());
            static::assertStringContainsString($file$mailEvent->getMessage()->getTextBody());
            static::assertIsString($mailEvent->getMessage()->getHtmlBody());
            static::assertStringContainsString($file$mailEvent->getMessage()->getHtmlBody());
        }
    }

    /** * @param array<int, string[]> $productDownloads */
    private function addProducts(Cart $cart, array $productDownloads): Cart
    {
        
if ($template = $message->getTextTemplate()) {
            $message->text($this->twig->render($template$vars));
        }

        if ($template = $message->getHtmlTemplate()) {
            $message->html($this->twig->render($template$vars));
        }

        $message->markAsRendered();

        // if text body is empty, compute one from the HTML body         if (!$message->getTextBody() && null !== $html = $message->getHtmlBody()) {
            $text = $this->converter->convert(\is_resource($html) ? stream_get_contents($html) : $html$message->getHtmlCharset());
            $message->text($text$message->getHtmlCharset());
        }
    }
}
$message['Cc'] = $this->formatAddresses($emails);
        }
        if ($emails = $email->getBcc()) {
            $message['Bcc'] = $this->formatAddresses($emails);
        }
        if ($emails = $email->getReplyTo()) {
            if (1 < $length = \count($emails)) {
                throw new TransportException(sprintf('Mailjet\'s API only supports one Reply-To email, %d given.', $length));
            }
            $message['ReplyTo'] = $this->formatAddress($emails[0]);
        }
        if ($email->getTextBody()) {
            $message['TextPart'] = $email->getTextBody();
        }
        if ($html) {
            $message['HTMLPart'] = $html;
        }

        foreach ($email->getHeaders()->all() as $header) {
            if ($convertConf = self::HEADER_TO_MESSAGE[$header->getName()] ?? false) {
                $message[$convertConf[0]] = $this->castCustomHeader($header->getBodyAsString()$convertConf[1]);
                continue;
            }
            
$personalization['custom_args'] = $customArguments;
        }

        $payload['personalizations'][] = $personalization;

        return $payload;
    }

    private function getContent(Email $email): array
    {
        $content = [];
        if (null !== $text = $email->getTextBody()) {
            $content[] = ['type' => 'text/plain', 'value' => $text];
        }
        if (null !== $html = $email->getHtmlBody()) {
            $content[] = ['type' => 'text/html', 'value' => $html];
        }

        return $content;
    }

    private function getAttachments(Email $email): array
    {
        
$this->addressesFormData($fields, 'cc', $email->getCc());
        }

        if ($email->getBcc()) {
            $this->addressesFormData($fields, 'bcc', $email->getBcc());
        }

        if ($email->getReplyTo()) {
            $this->addressesFormData($fields, 'replyto', $email->getReplyTo());
        }

        if ($email->getTextBody()) {
            $fields['text'] = $email->getTextBody();
        }

        if ($email->getHtmlBody()) {
            $fields['HTML'] = $email->getHtmlBody();
        }

        $this->attachmentsFormData($fields$email);

        foreach ($email->getHeaders()->all() as $header) {
            if ($convertConf = self::HEADER_TO_MESSAGE[$header->getName()] ?? false) {
                
'subject' => 'Test',
            'senderEmail' => 'test@example.com',
        ];

        $mail = $mailService->send($data, Context::createDefaultContext()[
            'text' => '<foobar>',
            'url' => 'http://example.com/?foo&bar=baz',
        ]);

        static::assertInstanceOf(Email::class$mail);
        static::assertEquals('<a href="http://example.com/?foo&amp;bar=baz">&lt;foobar&gt;</a>', $mail->getHtmlBody());
        static::assertEquals('<foobar> http://example.com/?foo&bar=baz', $mail->getTextBody());
    }
}

/** * @internal */
class TestEnvironment extends Environment
{
    /** * @var array<int, mixed[]> */
    
$e = new Email();
        $e->setBody($text = new TextPart('text content'));
        $this->assertEquals($text$e->getBody());
    }

    public function testGenerateBodyWithTextOnly()
    {
        $text = new TextPart('text content');
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->text('text content');
        $this->assertEquals($text$e->getBody());
        $this->assertEquals('text content', $e->getTextBody());
    }

    public function testGenerateBodyWithHtmlOnly()
    {
        $html = new TextPart('html content', 'utf-8', 'html');
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->html('html content');
        $this->assertEquals($html$e->getBody());
        $this->assertEquals('html content', $e->getHtmlBody());
    }

    


    /** * @param RawMessage $message */
    protected function matches($message): bool
    {
        if (RawMessage::class === $message::class || Message::class === $message::class) {
            throw new \LogicException('Unable to test a message text body on a RawMessage or Message instance.');
        }

        return str_contains($message->getTextBody()$this->expectedText);
    }

    /** * @param RawMessage $message */
    protected function failureDescription($message): string
    {
        return 'the Email text body '.$this->toString();
    }
}
Home | Imprint | This part of the site doesn't use cookies.