asInline example

$p = new DataPart('content', 'photo.jpg', 'text/html');
        $this->assertEquals(new Headers(
            new ParameterizedHeader('Content-Type', 'text/html', ['name' => 'photo.jpg']),
            new UnstructuredHeader('Content-Transfer-Encoding', 'base64'),
            new ParameterizedHeader('Content-Disposition', 'attachment', ['name' => 'photo.jpg', 'filename' => 'photo.jpg'])
        )$p->getPreparedHeaders());
    }

    public function testAsInline()
    {
        $p = new DataPart('content', 'photo.jpg', 'text/html');
        $p->asInline();
        $this->assertEquals(new Headers(
            new ParameterizedHeader('Content-Type', 'text/html', ['name' => 'photo.jpg']),
            new UnstructuredHeader('Content-Transfer-Encoding', 'base64'),
            new ParameterizedHeader('Content-Disposition', 'inline', ['name' => 'photo.jpg', 'filename' => 'photo.jpg'])
        )$p->getPreparedHeaders());
    }

    public function testAsInlineWithCID()
    {
        $p = new DataPart('content', 'photo.jpg', 'text/html');
        $p->asInline();
        
/** * @param string $image A Twig path to the image file. It's recommended to define * some Twig namespace for email images (e.g. '@email/images/logo.png'). * @param string|null $contentType The media type (i.e. MIME type) of the image file (e.g. 'image/png'). * Some email clients require this to display embedded images. */
    public function image(string $image, string $contentType = null): string
    {
        $file = $this->twig->getLoader()->getSourceContext($image);
        $body = $file->getPath() ? new File($file->getPath()) : $file->getCode();
        $this->message->addPart((new DataPart($body$image$contentType))->asInline());

        return 'cid:'.$image;
    }

    /** * @param string $file A Twig path to the file. It's recommended to define * some Twig namespace for email files (e.g. '@email/files/contract.pdf'). * @param string|null $name A custom file name that overrides the original name of the attached file * @param string|null $contentType The media type (i.e. MIME type) of the file (e.g. 'application/pdf'). * Some email clients require this to display attached files. */
    

        return $this->addPart(new DataPart(new File($path)$name$contentType));
    }

    /** * @param resource|string $body * * @return $this */
    public function embed($body, string $name = null, string $contentType = null)static
    {
        return $this->addPart((new DataPart($body$name$contentType))->asInline());
    }

    /** * @return $this */
    public function embedFromPath(string $path, string $name = null, string $contentType = null)static
    {
        return $this->addPart((new DataPart(new File($path)$name$contentType))->asInline());
    }

    /** * @return $this * * @deprecated since Symfony 6.2, use addPart() instead */

        return $this->addPart(new DataPart(new File($path)$name$contentType));
    }

    /** * @param resource|string $body * * @return $this */
    public function embed($body, string $name = null, string $contentType = null)static
    {
        return $this->addPart((new DataPart($body$name$contentType))->asInline());
    }

    /** * @return $this */
    public function embedFromPath(string $path, string $name = null, string $contentType = null)static
    {
        return $this->addPart((new DataPart(new File($path)$name$contentType))->asInline());
    }

    /** * @return $this * * @deprecated since Symfony 6.2, use addPart() instead */
$text$html$filePart$file$imagePart$image] = $this->generateSomeParts();
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->addPart(new DataPart($file));
        $e->html('html content');
        $this->assertEquals(new MixedPart($html$filePart)$e->getBody());
    }

    public function testGenerateBodyWithHtmlContentAndInlineImageNotreferenced()
    {
        [$text$html$filePart$file$imagePart$image] = $this->generateSomeParts();
        $imagePart = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'));
        $imagePart->asInline();
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->addPart((new DataPart($image))->asInline());
        $e->html('html content');
        $this->assertEquals(new MixedPart($html$imagePart)$e->getBody());
    }

    public function testGenerateBodyWithAttachedFileOnly()
    {
        [$text$html$filePart$file$imagePart$image] = $this->generateSomeParts();
        $e = (new Email())->from('me@example.com')->to('you@example.com');
        $e->addPart(new DataPart($file));
        
$this->assertSame($email, MessageConverter::toEmail($email));

        $this->assertConversion((clone $email)->text('text content'));
        $this->assertConversion((clone $email)->html('HTML content <img src="cid:test.jpg" />'));
        $this->assertConversion((clone $email)
            ->text('text content')
            ->html('HTML content <img src="cid:test.jpg" />')
        );
        $this->assertConversion((clone $email)
            ->text('text content')
            ->html('HTML content <img src="cid:test.jpg" />')
            ->addPart((new DataPart($file, 'test.jpg', 'image/gif'))->asInline())
        );
        $this->assertConversion((clone $email)
            ->text('text content')
            ->html('HTML content <img src="cid:test.jpg" />')
            ->addPart(new DataPart($file, 'test_attached.jpg', 'image/gif'))
        );
        $this->assertConversion((clone $email)
            ->text('text content')
            ->html('HTML content <img src="cid:test.jpg" />')
            ->addPart((new DataPart($file, 'test.jpg', 'image/gif'))->asInline())
            ->addPart(new DataPart($file, 'test_attached.jpg', 'image/gif'))
        );
TXT,
            $options['body']
        );
    }

    public function testSendEmailWithAttachmentsShouldCalledInfobipWithTheRightParameters()
    {
        $email = $this->basicValidEmail()
            ->text('foobar')
            ->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'))
            ->addPart((new DataPart('some inline attachment', 'inline.txt', 'text/plain'))->asInline())
        ;

        $this->transport->send($email);

        $options = $this->response->getRequestOptions();
        $this->arrayHasKey('body');
        $this->assertStringMatchesFormat(<<<'TXT' %a --%s Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="text" foobar --%s Content-Type: text/plain Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="attachment"; filename="attachment.txt" some attachment --%s Content-Type: text/plain Content-Transfer-Encoding: 8bit Content-Disposition: form-data; name="inlineImage"; filename="inline.txt" some inline attachment --%s--
$transport = new MailerSendApiTransport('ACCESS_KEY', $client);

        $mail = new Email();
        $mail->subject('Test subject')
            ->to(new Address('test_to@example.com', 'Test to name'))
            ->from(new Address('test_from@example.com', 'Test from name'))
            ->addCc('test_cc@example.com')
            ->addBcc('test_bcc@example.com')
            ->addReplyTo('test_reply_to@example.com')
            ->html('<html><body><p>Lorem ipsum.</p><img src="cid:test_cid@symfony"></body></html>')
            ->addPart(new DataPart('content', 'attachment.txt', 'text/plain'))
            ->addPart((new DataPart('inline content', 'inline.txt', 'text/plain'))->asInline()->setContentId('test_cid@symfony'));

        $message = $transport->send($mail);

        $this->assertSame('test_message_id', $message->getMessageId());
    }

    public function testSendThrowsForErrorResponse()
    {
        $client = new MockHttpClient(function Dstring $method, string $url, array $options): ResponseInterface {
            return new MockResponse(json_encode(['message' => 'i\'m a teapot'])[
                'http_code' => 418,
            ]);
Home | Imprint | This part of the site doesn't use cookies.