diff --git a/config/mail.php b/config/mail.php index 22c03b032d76..489c1a1a2480 100644 --- a/config/mail.php +++ b/config/mail.php @@ -65,6 +65,10 @@ 'transport' => 'resend', ], + 'mailtrap' => [ + 'transport' => 'mailtrap', + ], + 'sendmail' => [ 'transport' => 'sendmail', 'path' => env('MAIL_SENDMAIL_PATH', '/usr/sbin/sendmail -bs -i'), diff --git a/config/services.php b/config/services.php index 6182e4b90c94..09291c7e013b 100644 --- a/config/services.php +++ b/config/services.php @@ -35,4 +35,10 @@ ], ], + 'mailtrap' => [ + 'key' => env('MAILTRAP_API_KEY'), + 'is_sandbox' => env('MAILTRAP_SANDBOX_ENABLED', false), + 'inbox_id' => env('MAILTRAP_INBOX_ID'), + ], + ]; diff --git a/phpstan.src.neon.dist b/phpstan.src.neon.dist index 59996713063e..ca01b0886c6f 100644 --- a/phpstan.src.neon.dist +++ b/phpstan.src.neon.dist @@ -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\.$#' diff --git a/src/Illuminate/Mail/MailManager.php b/src/Illuminate/Mail/MailManager.php index c1ca0f4cb8a9..fe35f7b2d5c9 100644 --- a/src/Illuminate/Mail/MailManager.php +++ b/src/Illuminate/Mail/MailManager.php @@ -9,6 +9,7 @@ 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; @@ -16,6 +17,7 @@ use Illuminate\Support\ConfigurationUrlParser; use Illuminate\Support\Str; use InvalidArgumentException; +use Mailtrap\MailtrapClient; use Psr\Log\LoggerInterface; use Resend; use Symfony\Component\HttpClient\HttpClient; @@ -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. * diff --git a/src/Illuminate/Mail/Transport/MailtrapTransport.php b/src/Illuminate/Mail/Transport/MailtrapTransport.php new file mode 100644 index 000000000000..edbd1c6625b3 --- /dev/null +++ b/src/Illuminate/Mail/Transport/MailtrapTransport.php @@ -0,0 +1,47 @@ +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'; + } +}