Skip to content

Added support for whitelisting Recipients #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions EventListener/EnvelopeWhitelistListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Address;

/**
* Manipulates the Envelope of a Message.
*
* @author Fabien Potencier <[email protected]>
*/
class EnvelopeWhitelistListener implements EventSubscriberInterface
{
private $sender;
private $recipients;
private $whitelist;

/**
* @param Address|string $sender
* @param (Address|string)[] $recipients
* @param (Address|string)[] $whitelist
*/
public function __construct($sender = null, array $recipients = null, array $whitelist = null)
{
if (null !== $sender) {
$this->sender = Address::create($sender);
}
if (null !== $recipients) {
$this->recipients = Address::createArray($recipients);
}
if (null !== $whitelist) {
$this->whitelist = Address::createArray($whitelist);
}
}

public function onMessage(MessageEvent $event): void
{
if ($this->sender) {
$event->getEnvelope()->setSender($this->sender);
}

if ($this->whitelist) {
array_push(
$this->recipients,
array_intersect(
$event->getEnvelope()->getRecipients(),
$this->whitelist
)
);
}

if ($this->recipients) {
$event->getEnvelope()->setRecipients($this->recipients);
}
}

public static function getSubscribedEvents()
{
return [
// should be the last one to allow header changes by other listeners first
MessageEvent::class => ['onMessage', -255],
];
}
}