Repository: symfony/symfony
File: src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridSmtpTransport.php (PHP)

Code before:
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Mailer\Bridge\Sendgrid\Transport;

use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Bridge\Sendgrid\Header\SuppressionGroupHeader;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;

/**
 * @author Kevin Verschaeve
 */
class SendgridSmtpTransport extends EsmtpTransport
{
    public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, private ?string $region = null)
    {
        $host = 'smtp.sendgrid.net';

        if (null !== $region && 'global' !== strtolower($region)) {
            $host = \sprintf('smtp.%s.sendgrid.net', $region);
        }

        parent::__construct($host, 465, true, $dispatcher, $logger);

        $this->setUsername('apikey');
        $this->setPassword($key);
    }

    public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
    {
        if ($message instanceof Message) {
            $this->addSendgridHeaders($message);
        }

        return parent::send($message, $envelope);
    }

    private function addSendgridHeaders(Message $message): void
    {
        $headers = $message->getHeaders();

        if ($headers->has('X-SMTPAPI')) {
            return;
        }

Review comment (security):
```suggestion
        if (null !== $region && 'global' !== $region) {
```

I don't think we need to make it case insensitive.


Code after:
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Mailer\Bridge\Sendgrid\Transport;

use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Bridge\Sendgrid\Header\SuppressionGroupHeader;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;

/**
 * @author Kevin Verschaeve
 */
class SendgridSmtpTransport extends EsmtpTransport
{
    public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, private ?string $region = null)
    {
        $host = 'smtp.sendgrid.net';

        if (null !== $region && 'global' !== $region) {
            $host = \sprintf('smtp.%s.sendgrid.net', $region);
        }

        parent::__construct($host, 465, true, $dispatcher, $logger);

        $this->setUsername('apikey');
        $this->setPassword($key);
    }

    public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
    {
        if ($message instanceof Message) {
            $this->addSendgridHeaders($message);
        }

        return parent::send($message, $envelope);
    }

    private function addSendgridHeaders(Message $message): void
    {
        $headers = $message->getHeaders();

        if ($headers->has('X-SMTPAPI')) {
            return;
        }