Skip to content
4 changes: 4 additions & 0 deletions config/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
'transport' => 'resend',
],

'mailtrap' => [
'transport' => 'mailtrap',
],

'sendmail' => [
'transport' => 'sendmail',
'path' => env('MAIL_SENDMAIL_PATH', '/usr/sbin/sendmail -bs -i'),
Expand Down
6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
],
],

'mailtrap' => [
'key' => env('MAILTRAP_API_KEY'),
'is_sandbox' => env('MAILTRAP_SANDBOX_ENABLED', false),
'inbox_id' => env('MAILTRAP_INBOX_ID'),
],

];
2 changes: 2 additions & 0 deletions phpstan.src.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ parameters:
- "#has invalid type#"
- "#Instantiated class [a-zA-Z0-9\\\\_]+ not found.#"
- "#Unsafe usage of new static#"
- '#^Call to static method initSendingEmails\(\) on an unknown class Mailtrap\\MailtrapClient\.$#'
- '#^Property Illuminate\\Mail\\Transport\\MailtrapTransport\:\:\$mailtrap has unknown class Mailtrap\\Api\\EmailsSendApiInterface as its type\.$#'
20 changes: 20 additions & 0 deletions src/Illuminate/Mail/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Illuminate\Log\LogManager;
use Illuminate\Mail\Transport\ArrayTransport;
use Illuminate\Mail\Transport\LogTransport;
use Illuminate\Mail\Transport\MailtrapTransport;
use Illuminate\Mail\Transport\ResendTransport;
use Illuminate\Mail\Transport\SesTransport;
use Illuminate\Mail\Transport\SesV2Transport;
use Illuminate\Support\Arr;
use Illuminate\Support\ConfigurationUrlParser;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Mailtrap\MailtrapClient;
use Psr\Log\LoggerInterface;
use Resend;
use Symfony\Component\HttpClient\HttpClient;
Expand Down Expand Up @@ -321,6 +323,24 @@ protected function createResendTransport(array $config)
);
}

/**
* Create an instance of the Mailtrap Transport driver.
*
* @param array $config
* @return \Illuminate\Mail\Transport\MailtrapTransport
*/
protected function createMailtrapTransport(array $config)
{
return new MailtrapTransport(
MailtrapClient::initSendingEmails(
$config['key'] ?? $this->app['config']->get('services.mailtrap.key'),
(bool) ($config['is_bulk'] ?? $this->app['config']->get('services.mailtrap.is_bulk')),
(bool) ($config['is_sandbox'] ?? $this->app['config']->get('services.mailtrap.is_sandbox')),
$config['inbox_id'] ?? $this->app['config']->get('services.mailtrap.inbox_id'),
),
);
}

/**
* Create an instance of the Symfony Mail Transport driver.
*
Expand Down
47 changes: 47 additions & 0 deletions src/Illuminate/Mail/Transport/MailtrapTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Mail\Transport;

use Exception;
use Mailtrap\Api\EmailsSendApiInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\MessageConverter;

class MailtrapTransport extends AbstractTransport
{
/**
* Create a new Mailtrap transport instance.
*/
public function __construct(protected EmailsSendApiInterface $mailtrap)
{
parent::__construct();
}

/**
* {@inheritDoc}
*/
protected function doSend(SentMessage $message): void
{
$email = MessageConverter::toEmail($message->getOriginalMessage());

try {
$this->mailtrap->send($email);
} catch (Exception $exception) {
throw new TransportException(
sprintf('Request to Mailtrap API failed. Reason: %s.', $exception->getMessage()),
is_int($exception->getCode()) ? $exception->getCode() : 0,
$exception
);
}
}

/**
* Get the string representation of the transport.
*/
public function __toString(): string
{
return 'mailtrap';
}
}