addPart example


        return $this->htmlCharset;
    }

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

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

    /** * @param resource|string $body * * @return $this */
$e->html('html content', 'iso-8859-1');
        $e->text('text content', 'iso-8859-1');
        $this->assertEquals('iso-8859-1', $e->getTextCharset());
        $this->assertEquals('iso-8859-1', $e->getHtmlCharset());
        $this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1')new TextPart('html content', 'iso-8859-1', 'html'))$e->getBody());
    }

    public function testGenerateBodyWithTextContentAndAttachedFile()
    {
        [$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->text('text content');
        $this->assertEquals(new MixedPart($text$filePart)$e->getBody());
    }

    public function testGenerateBodyWithHtmlContentAndAttachedFile()
    {
        [$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());
    }
return $this;
    }

    /** * @return $this */
    public function exception(\Throwable|FlattenException $exception)static
    {
        $exceptionAsString = $this->getExceptionAsString($exception);

        $this->context['exception'] = true;
        $this->addPart(new DataPart($exceptionAsString, 'exception.txt', 'text/plain'));
        $this->importance(self::IMPORTANCE_URGENT);

        if (!$this->getSubject()) {
            $this->subject($exception->getMessage());
        }

        return $this;
    }

    /** * @return $this */
$transport->setPort(8984);

        $mail = new Email();
        $mail->subject('Hello!')
            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
            ->from(new Address('fabpot@symfony.com', 'Fabien'))
            ->text('Hello here!')
            ->html('Hello there!')
            ->addCc('foo@bar.fr')
            ->addBcc('foo@bar.fr')
            ->addReplyTo('foo@bar.fr')
            ->addPart(new DataPart('body'))
        ;

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

        $this->assertSame('foobar', $message->getMessageId());
    }
}
return self::addParts($email, \array_slice($parts, 1));
    }

    private static function addParts(Email $email, array $parts): Email
    {
        foreach ($parts as $part) {
            if (!$part instanceof DataPart) {
                throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($email)));
            }

            $email->addPart($part);
        }

        return $email;
    }
}

        return $this->htmlCharset;
    }

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

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

    /** * @param resource|string $body * * @return $this */
$transport->setPort(8984);

        $mail = new Email();
        $mail->subject('Hello!')
            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
            ->from(new Address('fabpot@symfony.com', 'Fabien'))
            ->text('Hello here!')
            ->html('Hello there!')
            ->addCc('foo@bar.fr')
            ->addBcc('foo@bar.fr')
            ->addReplyTo('foo@bar.fr')
            ->addPart(new DataPart('body'));

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

        $this->assertSame('foobar', $message->getMessageId());
    }
}
break;
                    case 'content-location':
                        $newPart->location = $value;
                        break;
                    case 'content-language':
                        $newPart->language = $value;
                        break;
                    default:
                        throw new Zend_Exception('Unknown header ignored for MimePart:' . $key);
                }
            }
            $res->addPart($newPart);
        }
        return $res;
    }
}

                $parts = $this->parseSelectorParts();
                if (!count($parts)) {
                    throw new Exception("Missing selector after combinator");
                }
                $first = false;
                $selCombinator = new Node\Combinator;
                $selCombinator->setOperator(
                    $combinator ?: ($filter ? null : " ")
                );
                foreach ($parts as $part) {
                    $selCombinator->addPart($part);
                }
                $group->addCombinator($selCombinator);
            }
            $selector->addGroup($group);
            $this->consumeWhitespaces();
        } while ($this->consume(","));
        return $selector;
    }

    /** * Parses a set of selector pats * * @return array * * @throws Exception */
use Symfony\Component\Mime\Part\DataPart;

class EmailController
{
    public function indexAction(MailerInterface $mailer)
    {
        $mailer->send((new Email())->to('fabien@symfony.com')->from('fabien@symfony.com')->subject('Foo')
            ->addReplyTo('me@symfony.com')
            ->addCc('cc@symfony.com')
            ->text('Bar!')
            ->html('<p>Foo</p>')
            ->addPart(new DataPart(file_get_contents(__FILE__), 'foobar.php'))
        );

        $mailer->send((new Email())->to('fabien@symfony.com', 'thomas@symfony.com')->from('fabien@symfony.com')->subject('Foo')
            ->addReplyTo(new Address('me@symfony.com', 'Fabien Potencier'))
            ->addCc('cc@symfony.com')
            ->text('Bar!')
            ->html('<p>Foo</p>')
            ->addPart(new DataPart(file_get_contents(__FILE__), 'foobar.php'))
        );

        return new Response();
    }
$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'])[
                
return self::addParts($email, \array_slice($parts, 1));
    }

    private static function addParts(Email $email, array $parts): Email
    {
        foreach ($parts as $part) {
            if (!$part instanceof DataPart) {
                throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($email)));
            }

            $email->addPart($part);
        }

        return $email;
    }
}
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--
return $this->_bodyHtml;
    }

    /** * Adds an existing attachment to the mail message * * @param Zend_Mime_Part $attachment * @return Zend_Mail Provides fluent interface */
    public function addAttachment(Zend_Mime_Part $attachment)
    {
        $this->addPart($attachment);
        $this->hasAttachments = true;

        return $this;
    }

    /** * Creates a Zend_Mime_Part attachment * * Attachment is automatically added to the mail object after creation. The * attachment object is returned to allow for further manipulation. * * @param string $body * @param string $mimeType * @param string $disposition * @param string $encoding * @param string $filename OPTIONAL A filename for the attachment * @return Zend_Mime_Part Newly created Zend_Mime_Part object (to allow * advanced settings) */
$mailer = new SendgridApiTransport('foo', $httpClient);
        $mailer->send($email);
    }

    public function testLineBreaksInEncodedAttachment()
    {
        $email = new Email();
        $email->from('foo@example.com')
            ->to('bar@example.com')
            // even if content doesn't include new lines, the base64 encoding performed later may add them             ->addPart(new DataPart('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod', 'lorem.txt'));

        $response = $this->createMock(ResponseInterface::class);

        $response
            ->expects($this->once())
            ->method('getStatusCode')
            ->willReturn(202);
        $response
            ->expects($this->once())
            ->method('getHeaders')
            ->willReturn(['x-message-id' => '1']);

        
Home | Imprint | This part of the site doesn't use cookies.