Email example

$this->assertSame([['Name' => 'tagName1', 'Value' => 'tag Value1']['Name' => 'tagName2', 'Value' => 'tag Value2']]$body['EmailTags']);

            $json = '{"MessageId": "foobar"}';

            return new MockResponse($json[
                'http_code' => 200,
            ]);
        });

        $transport = new SesHttpAsyncAwsTransport(new SesClient(Configuration::create([])new NullProvider()$client));

        $mail = new Email();
        $mail->subject('Hello!')
            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
            ->from(new Address('fabpot@symfony.com', 'Fabien'))
            ->text('Hello There!');

        $mail->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', 'aws-configuration-set-name');
        $mail->getHeaders()->addTextHeader('X-SES-SOURCE-ARN', 'aws-source-arn');
        $mail->getHeaders()->add(new MetadataHeader('tagName1', 'tag Value1'));
        $mail->getHeaders()->add(new MetadataHeader('tagName2', 'tag Value2'));

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

        
'postmark+api://example.com',
            ],
            [
                (new PostmarkApiTransport('KEY'))->setHost('example.com')->setPort(99),
                'postmark+api://example.com:99',
            ],
        ];
    }

    public function testCustomHeader()
    {
        $email = new Email();
        $email->getHeaders()->addTextHeader('foo', 'bar');
        $envelope = new Envelope(new Address('alice@system.com')[new Address('bob@system.com')]);

        $transport = new PostmarkApiTransport('ACCESS_KEY');
        $method = new \ReflectionMethod(PostmarkApiTransport::class, 'getPayload');
        $payload = $method->invoke($transport$email$envelope);

        $this->assertArrayHasKey('Headers', $payload);
        $this->assertCount(1, $payload['Headers']);

        $this->assertEquals(['Name' => 'foo', 'Value' => 'bar']$payload['Headers'][0]);
    }
yield [
            (new BrevoApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99),
            'brevo+api://example.com:99',
        ];
    }

    public function testCustomHeader()
    {
        $params = ['param1' => 'foo', 'param2' => 'bar'];
        $json = json_encode(['"custom_header_1' => 'custom_value_1']);

        $email = new Email();
        $email->getHeaders()
            ->add(new MetadataHeader('custom', $json))
            ->add(new TagHeader('TagInHeaders'))
            ->addTextHeader('templateId', 1)
            ->addParameterizedHeader('params', 'params', $params)
            ->addTextHeader('foo', 'bar');
        $envelope = new Envelope(new Address('alice@system.com', 'Alice')[new Address('bob@system.com', 'Bob')]);

        $transport = new BrevoApiTransport('ACCESS_KEY');
        $method = new \ReflectionMethod(BrevoApiTransport::class, 'getPayload');
        $payload = $method->invoke($transport$email$envelope);

        
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\MessageConverter;
use Symfony\Component\Mime\Part\DataPart;

class MessageConverterTest extends TestCase
{
    public function testToEmail()
    {
        $file = file_get_contents(__DIR__.'/Fixtures/mimetypes/test.gif');
        $email = (new Email())->from('fabien@symfony.com')->to('you@example.com');
        $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" />')
            
namespace Symfony\Component\Mailer\Bridge\MailPace\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Bridge\MailPace\Transport\MailPaceSmtpTransport;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Email;

final class MailPaceSmtpTransportTest extends TestCase
{
    public function testCustomHeader()
    {
        $email = new Email();
        $email->getHeaders()->addTextHeader('foo', 'bar');

        $transport = new MailPaceSmtpTransport('ACCESS_KEY');
        $method = new \ReflectionMethod(MailPaceSmtpTransport::class, 'addMailPaceHeaders');
        $method->invoke($transport$email);

        $this->assertCount(1, $email->getHeaders()->toArray());
        $this->assertSame('foo: bar', $email->getHeaders()->get('FOO')->toString());
    }

    public function testTagAndMetadataHeaders()
    {

    public static function email($config = null, bool $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('email', $config);
        }

        if (empty($config) || ! (is_array($config) || $config instanceof EmailConfig)) {
            $config = config(EmailConfig::class);
        }

        return new Email($config);
    }

    /** * The Encryption class provides two-way encryption. * * @param bool $getShared * * @return EncrypterInterface Encryption handler */
    public static function encrypter(?EncryptionConfig $config = null, $getShared = false)
    {
        
EOF
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $message = (new Email())
            ->to($input->getArgument('to'))
            ->from($input->getOption('from'))
            ->subject($input->getOption('subject'))
            ->text($input->getOption('body'))
        ;
        if ($transport = $input->getOption('transport')) {
            $message->getHeaders()->addTextHeader('X-Transport', $transport);
        }

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

        
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;

class EmailTest extends TestCase
{
    public function testSubject()
    {
        $e = new Email();
        $e->subject('Subject');
        $this->assertEquals('Subject', $e->getSubject());
    }

    public function testDate()
    {
        $e = new Email();
        $e->date($d = new \DateTimeImmutable());
        $this->assertEquals($d$e->getDate());
    }

    
use Symfony\Component\Mailer\Header\MetadataHeader;
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Email;

/** * @author Kevin Bond <kevinbond@gmail.com> */
class MailgunSmtpTransportTest extends TestCase
{
    public function testTagAndMetadataHeaders()
    {
        $email = new Email();
        $email->getHeaders()->addTextHeader('foo', 'bar');
        $email->getHeaders()->add(new TagHeader('password-reset'));
        $email->getHeaders()->add(new MetadataHeader('Color', 'blue'));
        $email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));

        $transport = new MailgunSmtpTransport('user', 'password');
        $method = new \ReflectionMethod(MailgunSmtpTransport::class, 'addMailgunHeaders');
        $method->invoke($transport$email);

        $this->assertCount(3, $email->getHeaders()->toArray());
        $this->assertSame('foo: bar', $email->getHeaders()->get('foo')->toString());
        


            return self::addParts($email, \array_slice($parts, 1));
        }

        throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
    }

    private static function createEmailFromTextPart(Message $message, TextPart $part): Email
    {
        if ('text' === $part->getMediaType() && 'plain' === $part->getMediaSubtype()) {
            return (new Email(clone $message->getHeaders()))->text($part->getBody()$part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8');
        }
        if ('text' === $part->getMediaType() && 'html' === $part->getMediaSubtype()) {
            return (new Email(clone $message->getHeaders()))->html($part->getBody()$part->getPreparedHeaders()->getHeaderParameter('Content-Type', 'charset') ?: 'utf-8');
        }

        throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
    }

    private static function createEmailFromAlternativePart(Message $message, AlternativePart $part): Email
    {
        $parts = $part->getParts();
        
'sendgrid+api://example.com',
            ],
            [
                (new SendgridApiTransport('KEY'))->setHost('example.com')->setPort(99),
                'sendgrid+api://example.com:99',
            ],
        ];
    }

    public function testSend()
    {
        $email = new Email();
        $email->from(new Address('foo@example.com', 'Ms. Foo Bar'))
            ->to(new Address('bar@example.com', 'Mr. Recipient'))
            ->bcc('baz@example.com')
            ->text('content');

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

        $response
            ->expects($this->once())
            ->method('getStatusCode')
            ->willReturn(202);
        
'ohmysmtp+api://example.com',
            ],
            [
                (new OhMySmtpApiTransport('KEY'))->setHost('example.com')->setPort(99),
                'ohmysmtp+api://example.com:99',
            ],
        ];
    }

    public function testCustomHeader()
    {
        $email = new Email();
        $email->getHeaders()->addTextHeader('foo', 'bar');
        $envelope = new Envelope(new Address('alice@system.com')[new Address('bob@system.com')]);

        $transport = new OhMySmtpApiTransport('ACCESS_KEY');
        $method = new \ReflectionMethod(OhMySmtpApiTransport::class, 'getPayload');
        $payload = $method->invoke($transport$email$envelope);

        $this->assertArrayHasKey('Headers', $payload);
        $this->assertCount(1, $payload['Headers']);

        $this->assertEquals(['Name' => 'foo', 'Value' => 'bar']$payload['Headers'][0]);
    }
$this->assertSame(['email' => 'saif.gmati@symfony.com', 'name' => 'Saif Eddin']$body['to'][0]);
            $this->assertSame('Hello!', $body['subject']);
            $this->assertSame('Hello There!', $body['text']);

            return new MockResponse(json_encode(['emails' => [['message_id' => 'foobar']]])[
                'http_code' => 200,
            ]);
        });
        $transport = new ScalewayApiTransport('PROJECT_ID', 'TOKEN', 'fr-par', $client);
        $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 There!');

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

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

    public function testSendThrowsForErrorResponse()
    {
$options = $this->response->getRequestOptions();
        $this->arrayHasKey('headers');
        $this->assertCount(4, $options['headers']);
        $this->assertStringMatchesFormat('Content-Type: multipart/form-data; boundary=%s', $options['headers'][0]);
        $this->assertSame('Authorization: App k3y', $options['headers'][1]);
        $this->assertSame('Accept: application/json', $options['headers'][2]);
        $this->assertStringMatchesFormat('Content-Length: %d', $options['headers'][3]);
    }

    public function testSendMinimalEmailShouldCalledInfobipWithTheRightParameters()
    {
        $email = (new Email())
            ->subject('Subject of the email')
            ->from('from@example.com')
            ->to('to@example.com')
            ->text('Some text')
        ;

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

        $options = $this->response->getRequestOptions();
        $this->arrayHasKey('body');
        $this->assertStringMatchesFormat(<<<'TXT'
$this->assertStringContainsString('To: Saif Eddin <saif.gmati@symfony.com>', $message);
            $this->assertStringContainsString('From: Fabien <fabpot@symfony.com>', $message);
            $this->assertStringContainsString('Hello There!', $message);

            return new MockResponse(json_encode([['_id' => 'foobar']])[
                'http_code' => 200,
            ]);
        });

        $transport = new MandrillHttpTransport('KEY', $client);

        $mail = new Email();
        $mail->subject('Hello!')
            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
            ->from(new Address('fabpot@symfony.com', 'Fabien'))
            ->text('Hello There!');

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

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

    public function testSendThrowsForErrorResponse()
    {
Home | Imprint | This part of the site doesn't use cookies.