Skip to content

Commit 26e57da

Browse files
committed
added the Mailer component
0 parents  commit 26e57da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3182
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
4.3.0
5+
-----
6+
7+
* Added the component

Event/MessageEvent.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Event;
13+
14+
use Symfony\Component\EventDispatcher\Event;
15+
use Symfony\Component\Mailer\SmtpEnvelope;
16+
use Symfony\Component\Mime\RawMessage;
17+
18+
/**
19+
* Allows the transformation of a Message.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @experimental in 4.3
24+
*/
25+
class MessageEvent extends Event
26+
{
27+
private $message;
28+
private $envelope;
29+
30+
public function __construct(RawMessage $message, SmtpEnvelope $envelope)
31+
{
32+
$this->message = $message;
33+
$this->envelope = $envelope;
34+
}
35+
36+
public function getMessage(): RawMessage
37+
{
38+
return $this->message;
39+
}
40+
41+
public function setMessage(RawMessage $message): void
42+
{
43+
$this->message = $message;
44+
}
45+
46+
public function getEnvelope(): SmtpEnvelope
47+
{
48+
return $this->envelope;
49+
}
50+
51+
public function setEnvelope(SmtpEnvelope $envelope): void
52+
{
53+
$this->envelope = $envelope;
54+
}
55+
}

EventListener/EnvelopeListener.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\Mailer\Event\MessageEvent;
16+
use Symfony\Component\Mime\Address;
17+
18+
/**
19+
* Manipulates the Envelope of a Message.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*
23+
* @experimental in 4.3
24+
*/
25+
class EnvelopeListener implements EventSubscriberInterface
26+
{
27+
private $sender;
28+
private $recipients;
29+
30+
/**
31+
* @param Address|string $sender
32+
* @param (Address|string)[] $recipients
33+
*/
34+
public function __construct($sender = null, array $recipients = null)
35+
{
36+
if (null !== $sender) {
37+
$this->sender = Address::create($sender);
38+
}
39+
if (null !== $recipients) {
40+
$this->recipients = Address::createArray($recipients);
41+
}
42+
}
43+
44+
public function onMessage(MessageEvent $event): void
45+
{
46+
if ($this->sender) {
47+
$event->getEnvelope()->setSender($this->sender);
48+
}
49+
50+
if ($this->recipients) {
51+
$event->getEnvelope()->setRecipients($this->recipients);
52+
}
53+
}
54+
55+
public static function getSubscribedEvents()
56+
{
57+
return [
58+
// should be the last one to allow header changes by other listeners first
59+
MessageEvent::class => ['onMessage', -255],
60+
];
61+
}
62+
}

EventListener/MessageListener.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\Mailer\Event\MessageEvent;
16+
use Symfony\Component\Mime\BodyRendererInterface;
17+
use Symfony\Component\Mime\Header\Headers;
18+
use Symfony\Component\Mime\Message;
19+
20+
/**
21+
* Manipulates the headers and the body of a Message.
22+
*
23+
* @author Fabien Potencier <[email protected]>
24+
*
25+
* @experimental in 4.3
26+
*/
27+
class MessageListener implements EventSubscriberInterface
28+
{
29+
private $headers;
30+
private $renderer;
31+
32+
public function __construct(Headers $headers = null, BodyRendererInterface $renderer = null)
33+
{
34+
$this->headers = $headers;
35+
$this->renderer = $renderer;
36+
}
37+
38+
public function onMessage(MessageEvent $event): void
39+
{
40+
$message = $event->getMessage();
41+
if (!$message instanceof Message) {
42+
return;
43+
}
44+
45+
$this->setHeaders($message);
46+
$this->renderMessage($message);
47+
}
48+
49+
private function setHeaders(Message $message): void
50+
{
51+
if (!$this->headers) {
52+
return;
53+
}
54+
55+
$headers = $message->getHeaders();
56+
foreach ($this->headers->getAll() as $name => $header) {
57+
if (!$headers->has($name)) {
58+
$headers->add($header);
59+
} else {
60+
if (Headers::isUniqueHeader($name)) {
61+
continue;
62+
}
63+
$headers->add($header);
64+
}
65+
}
66+
$message->setHeaders($headers);
67+
}
68+
69+
private function renderMessage(Message $message): void
70+
{
71+
if (!$this->renderer) {
72+
return;
73+
}
74+
75+
$this->renderer->render($message);
76+
}
77+
78+
public static function getSubscribedEvents()
79+
{
80+
return [
81+
MessageEvent::class => 'onMessage',
82+
];
83+
}
84+
}

Exception/ExceptionInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
/**
15+
* Exception interface for all exceptions thrown by the component.
16+
*
17+
* @author Fabien Potencier <[email protected]>
18+
*
19+
* @experimental in 4.3
20+
*/
21+
interface ExceptionInterface extends \Throwable
22+
{
23+
}

Exception/HttpTransportException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*
17+
* @experimental in 4.3
18+
*/
19+
class HttpTransportException extends TransportException
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*
17+
* @experimental in 4.3
18+
*/
19+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
20+
{
21+
}

Exception/LogicException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*
17+
* @experimental in 4.3
18+
*/
19+
class LogicException extends \LogicException implements ExceptionInterface
20+
{
21+
}

Exception/RuntimeException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*
17+
* @experimental in 4.3
18+
*/
19+
class RuntimeException extends \RuntimeException implements ExceptionInterface
20+
{
21+
}

Exception/TransportException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Exception;
13+
14+
/**
15+
* @author Fabien Potencier <[email protected]>
16+
*
17+
* @experimental in 4.3
18+
*/
19+
class TransportException extends RuntimeException implements TransportExceptionInterface
20+
{
21+
}

0 commit comments

Comments
 (0)