diff --git a/composer.json b/composer.json index b325254..2c44ef5 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,11 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "dreamfactory/df-core": "~0.25", - "guzzlehttp/guzzle": "~6.0" + "dreamfactory/df-core": "~1.0", + "guzzlehttp/guzzle": "~7.2", + "symfony/mailer": "~6.1.7", + "symfony/mailgun-mailer": "~6.1.0", + "symfony/http-client": "~6.1.7" }, "require-dev": { "phpunit/phpunit": "@stable" diff --git a/src/Components/EmailUtilities.php b/src/Components/EmailUtilities.php index c122075..012b1be 100644 --- a/src/Components/EmailUtilities.php +++ b/src/Components/EmailUtilities.php @@ -3,6 +3,7 @@ namespace DreamFactory\Core\Email\Components; use DreamFactory\Core\Exceptions\BadRequestException; +use \Illuminate\Support\Arr; trait EmailUtilities { @@ -14,7 +15,7 @@ public static function sanitizeAndValidateEmails($emails, $return_format = '') $out = array(); foreach ($emails as $info) { if (is_array($info)) { - $email = array_get($info, 'email'); + $email = Arr::get($info, 'email'); $email = filter_var($email, FILTER_SANITIZE_EMAIL); if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new BadRequestException("Invalid email - '$email'."); @@ -22,7 +23,7 @@ public static function sanitizeAndValidateEmails($emails, $return_format = '') if (empty($email)) { throw new BadRequestException('Email can not be empty.'); } - $name = array_get($info, 'name'); + $name = Arr::get($info, 'name'); if (empty($name)) { $out[] = $email; } else { @@ -48,7 +49,7 @@ public static function sanitizeAndValidateEmails($emails, $return_format = '') } } else // single pair { - $email = array_get($emails, 'email'); + $email = Arr::get($emails, 'email'); $email = filter_var($email, FILTER_SANITIZE_EMAIL); if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new BadRequestException("Invalid email - '$email'."); @@ -56,7 +57,7 @@ public static function sanitizeAndValidateEmails($emails, $return_format = '') if (empty($email)) { throw new BadRequestException('Email can not be empty.'); } - $name = array_get($emails, 'name'); + $name = Arr::get($emails, 'name'); if (empty($name)) { $out = $email; } else { diff --git a/src/Models/MailGunConfig.php b/src/Models/MailGunConfig.php index e24fc8f..b4af6e5 100644 --- a/src/Models/MailGunConfig.php +++ b/src/Models/MailGunConfig.php @@ -29,10 +29,10 @@ protected static function prepareConfigSchemaField(array &$schema) case 'region_endpoint': $schema['type'] = 'picklist'; $schema['values'] = [ - ['label' => 'United States', 'name' => 'api.mailgun.net'], - ['label' => 'Europe', 'name' => 'api.eu.mailgun.net'], + ['label' => 'United States', 'name' => ''], + ['label' => 'Europe', 'name' => 'eu'], ]; - $schema['default'] = 'api.mailgun.net'; + $schema['default'] = ''; $schema['description'] = 'Select Mailgun service REST API Region Endpoint. According to Mailgun documentation.'; break; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6632e45..1c5f9c5 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -57,30 +57,6 @@ public function register() return new MailGun($config); }, ])); - $df->addType( - new ServiceType( - [ - 'name' => 'mandrill_email', - 'label' => 'Mandrill', - 'description' => 'Mandrill email service', - 'group' => ServiceTypeGroups::EMAIL, - 'config_handler' => MandrillConfig::class, - 'factory' => function ($config) { - return new Mandrill($config); - }, - ])); - $df->addType( - new ServiceType( - [ - 'name' => 'sparkpost_email', - 'label' => 'SparkPost', - 'description' => 'SparkPost email service', - 'group' => ServiceTypeGroups::EMAIL, - 'config_handler' => SparkpostConfig::class, - 'factory' => function ($config) { - return new SparkPost($config); - }, - ])); }); } diff --git a/src/Services/BaseService.php b/src/Services/BaseService.php index 747b39c..3463de6 100644 --- a/src/Services/BaseService.php +++ b/src/Services/BaseService.php @@ -5,6 +5,7 @@ use App; use DreamFactory\Core\Contracts\EmailServiceInterface; use DreamFactory\Core\Contracts\ServiceRequestInterface; +use DreamFactory\Core\Contracts\ServiceResponseInterface; use DreamFactory\Core\Email\Components\Attachment; use DreamFactory\Core\Email\Components\EmailUtilities; use DreamFactory\Core\Email\Components\Mailer as DfMailer; @@ -17,92 +18,68 @@ use DreamFactory\Core\Services\BaseRestService; use DreamFactory\Core\Utility\FileUtilities; use DreamFactory\Core\Utility\Session; +use Illuminate\Mail\Mailer; use Illuminate\Mail\Message; -use Swift_Transport as SwiftTransport; -use Swift_Mailer as SwiftMailer; +use Symfony\Component\Mailer\Transport\TransportInterface as SymfonyTransport; +use Symfony\Component\Mailer\Mailer as SymfonyMailer; use ServiceManager; +use \Illuminate\Support\Arr; abstract class BaseService extends BaseRestService implements EmailServiceInterface { use EmailUtilities; - /** - * @var SwiftTransport - */ - protected $transport; + protected SymfonyTransport $transport; - /** - * @var \Illuminate\Mail\Mailer; - */ - protected $mailer; + protected Mailer $mailer; - /** - * @var array; - */ - protected $parameters; + protected array $parameters; /** - * @param array $settings * @throws InternalServerErrorException */ - public function __construct($settings) + public function __construct(array $settings) { parent::__construct($settings); - $config = (array_get($settings, 'config', [])) ?: []; + $config = (Arr::get($settings, 'config', [])) ?: []; $this->setParameters($config); $this->setTransport($config); $this->setMailer(); } - /** - * Sets the email transport layer based on configuration. - * - * @param array $config - */ - abstract protected function setTransport($config); + abstract protected function setTransport(array $config); /** - * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException + * @throws InternalServerErrorException */ protected function setMailer() { - if (!$this->transport instanceof SwiftTransport) { + if (!$this->transport instanceof SymfonyTransport) { throw new InternalServerErrorException('Invalid Email Transport.'); } - $swiftMailer = new SwiftMailer($this->transport); - $this->mailer = new DfMailer(App::make('view'), $swiftMailer, App::make('events')); + $this->mailer = new DfMailer($this->name, App::make('view'), $this->transport, App::make('events')); } - /** - * @param $config - */ protected function setParameters($config) { - $this->parameters = (array)array_get($config, 'parameters', []); + $this->parameters = (array)Arr::get($config, 'parameters', []); foreach ($this->parameters as $params) { - $this->parameters[$params['name']] = array_get($params, 'value'); + $this->parameters[$params['name']] = Arr::get($params, 'value'); } } /** * {@inheritdoc} */ - protected function handleGET() + protected function handleGET(): bool { return false; } - /** - * Gets uploaded file(s) for attachment. - * - * @param null|string|array $path - * - * @return array - */ - protected function getUploadedAttachment($path = null) + protected function getUploadedAttachment(array|string $path = null): array { $attachment = []; $file = $path; @@ -112,11 +89,11 @@ protected function getUploadedAttachment($path = null) if (is_array($file)) { if (isset($file['tmp_name'], $file['name'])) { - $attachment[] = new Attachment($file['tmp_name'], array_get($file, 'name')); + $attachment[] = new Attachment($file['tmp_name'], Arr::get($file, 'name')); } else { foreach ($file as $f) { if (isset($f['tmp_name'], $f['name'])) { - $attachment[] = new Attachment(array_get($f, 'tmp_name'), array_get($f, 'name')); + $attachment[] = new Attachment(Arr::get($f, 'tmp_name'), Arr::get($f, 'name')); } } } @@ -126,14 +103,9 @@ protected function getUploadedAttachment($path = null) } /** - * Gets URL imported file(s) for attachment. - * - * @param null|string $path - * - * @return array - * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException + * @throws InternalServerErrorException */ - protected function getUrlAttachment($path = null) + protected function getUrlAttachment( $path = null): array { $attachment = []; $file = $path; @@ -167,13 +139,9 @@ protected function getUrlAttachment($path = null) /** * Gets file(s) stored in storage service(s) for attachment. - * - * @param null|string|array $path - * - * @return array - * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException + * @throws InternalServerErrorException */ - protected function getServiceAttachment($path = null) + protected function getServiceAttachment(array|string $path = null): array { $attachment = []; $file = $path; @@ -191,8 +159,8 @@ protected function getServiceAttachment($path = null) try { foreach ($files as $f) { if (is_array($f)) { - $service = array_get($f, 'service'); - $path = array_get($f, 'path', array_get($f, 'file_path')); + $service = Arr::get($f, 'service'); + $path = Arr::get($f, 'path', Arr::get($f, 'file_path')); Session::replaceLookups($service); Session::replaceLookups($path); @@ -202,7 +170,7 @@ protected function getServiceAttachment($path = null) if (Session::checkServicePermission(Verbs::GET, $service, $path, Session::getRequestor(), false)) { - /** @var \DreamFactory\Core\Contracts\ServiceResponseInterface $result */ + /** @var ServiceResponseInterface $result */ $result = ServiceManager::handleRequest( $service, Verbs::GET, @@ -219,7 +187,7 @@ protected function getServiceAttachment($path = null) } $content = $result->getContent(); - $content = base64_decode(array_get($content, 'content', '')); + $content = base64_decode(Arr::get($content, 'content', '')); $fileName = basename($path); $filePath = sys_get_temp_dir() . '/' . $fileName; file_put_contents($filePath, $content); @@ -243,12 +211,9 @@ protected function getServiceAttachment($path = null) } /** - * @param null|string $path - * - * @return array|mixed|string * @throws InternalServerErrorException */ - public function getAttachments($path = null) + public function getAttachments($path = null): array { return array_merge( $this->getUploadedAttachment($path), @@ -258,11 +223,10 @@ public function getAttachments($path = null) } /** - * @return array * @throws BadRequestException * @throws NotFoundException */ - protected function handlePOST() + protected function handlePOST(): array { $data = $this->getPayloadData(); if (empty($data)) { @@ -294,11 +258,11 @@ protected function handlePOST() throw new BadRequestException('No valid data in request.'); } - $data = array_merge((array)array_get($templateData, 'defaults', []), $data); + $data = array_merge((array)Arr::get($templateData, 'defaults', []), $data); $data = array_merge($this->parameters, $templateData, $data); - $text = array_get($data, 'body_text'); - $html = array_get($data, 'body_html'); + $text = Arr::get($data, 'body_text'); + $html = Arr::get($data, 'body_html'); $count = $this->sendEmail($data, $text, $html); @@ -312,12 +276,6 @@ protected function handlePOST() /** * Sends out emails. - * - * @param array $data - * @param null $textView - * @param null $htmlView - * - * @return mixed */ public function sendEmail($data, $textView = null, $htmlView = null) { @@ -329,19 +287,18 @@ public function sendEmail($data, $textView = null, $htmlView = null) 'text' => $textView ]; - /** @noinspection PhpVoidFunctionResultUsedInspection */ - $count = $this->mailer->send( + return $this->mailer->send( $view, $data, function (Message $m) use ($data) { - $to = array_get($data, 'to'); - $cc = array_get($data, 'cc'); - $bcc = array_get($data, 'bcc'); - $subject = array_get($data, 'subject'); - $fromName = array_get($data, 'from_name'); - $fromEmail = array_get($data, 'from_email'); - $replyName = array_get($data, 'reply_to_name'); - $replyEmail = array_get($data, 'reply_to_email'); + $to = Arr::get($data, 'to'); + $cc = Arr::get($data, 'cc'); + $bcc = Arr::get($data, 'bcc'); + $subject = Arr::get($data, 'subject'); + $fromName = Arr::get($data, 'from_name'); + $fromEmail = Arr::get($data, 'from_email'); + $replyName = Arr::get($data, 'reply_to_name'); + $replyEmail = Arr::get($data, 'reply_to_email'); // Look for any attachment in request data. $attachment = $this->getAttachments(); // No attachment in request data. Attachment found in email template. @@ -406,18 +363,12 @@ function (Message $m) use ($data) { } } ); - - return $count; } /** - * @param $name - * * @throws NotFoundException - * - * @return array */ - public static function getTemplateDataByName($name) + public static function getTemplateDataByName($name): array { // find template in system db $template = EmailTemplate::whereName($name)->first(); @@ -429,13 +380,9 @@ public static function getTemplateDataByName($name) } /** - * @param $id - * * @throws NotFoundException - * - * @return array */ - public static function getTemplateDataById($id) + public static function getTemplateDataById($id): array { // find template in system db $template = EmailTemplate::whereId($id)->first(); @@ -446,7 +393,7 @@ public static function getTemplateDataById($id) return $template->toArray(); } - protected function getApiDocPaths() + protected function getApiDocPaths(): array { $capitalized = camelize($this->name); @@ -489,7 +436,7 @@ protected function getApiDocPaths() ]; } - protected function getApiDocRequests() + protected function getApiDocRequests(): array { return [ 'EmailRequest' => [ @@ -506,7 +453,7 @@ protected function getApiDocRequests() ]; } - protected function getApiDocResponses() + protected function getApiDocResponses(): array { return [ 'EmailResponse' => [ @@ -523,7 +470,7 @@ protected function getApiDocResponses() ]; } - protected function getApiDocSchemas() + protected function getApiDocSchemas(): array { return [ 'EmailResponse' => [ diff --git a/src/Services/Local.php b/src/Services/Local.php index 7d713a4..d675d9a 100644 --- a/src/Services/Local.php +++ b/src/Services/Local.php @@ -2,17 +2,18 @@ namespace DreamFactory\Core\Email\Services; -use Swift_SendmailTransport as SendmailTransport; +use Symfony\Component\Mailer\Transport\SendmailTransport as SendmailTransport; use Config; +use \Illuminate\Support\Arr; class Local extends BaseService { /** * {@inheritdoc} */ - protected function setTransport($config) + protected function setTransport(array $config) { - $command = array_get($config, 'command'); + $command = Arr::get($config, 'command'); // old usage of mail config and env may be set to smtp if (empty($command) && ('smtp' == Config::get('mail.driver'))) { $host = Config::get('mail.host'); diff --git a/src/Services/MailGun.php b/src/Services/MailGun.php index 6e118e5..4ba58b6 100644 --- a/src/Services/MailGun.php +++ b/src/Services/MailGun.php @@ -3,16 +3,16 @@ namespace DreamFactory\Core\Email\Services; use DreamFactory\Core\Exceptions\InternalServerErrorException; -use GuzzleHttp\Client; -use Illuminate\Mail\Transport\MailgunTransport; +use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunApiTransport as MailgunTransport; +use \Illuminate\Support\Arr; class MailGun extends BaseService { - protected function setTransport($config) + protected function setTransport(array $config) { - $domain = array_get($config, 'domain'); - $key = array_get($config, 'key'); - $regionEndpoint = array_get($config, 'region_endpoint'); + $domain = Arr::get($config, 'domain'); + $key = Arr::get($config, 'key'); + $regionEndpoint = Arr::get($config, 'region_endpoint'); $this->transport = static::getTransport($domain, $key, $regionEndpoint); } @@ -21,7 +21,7 @@ protected function setTransport($config) * @param $domain * @param $key * @param $regionEndpoint - * @return \Illuminate\Mail\Transport\MailgunTransport + * @return MailgunTransport * @throws InternalServerErrorException */ public static function getTransport($domain, $key, $regionEndpoint) @@ -30,6 +30,6 @@ public static function getTransport($domain, $key, $regionEndpoint) throw new InternalServerErrorException('Missing one or more configuration for MailGun service.'); } - return new MailgunTransport(new Client(), $key, $domain, $regionEndpoint); + return new MailgunTransport($key, $domain, $regionEndpoint); } } \ No newline at end of file diff --git a/src/Services/Mandrill.php b/src/Services/Mandrill.php index bb0692a..c8a7a21 100644 --- a/src/Services/Mandrill.php +++ b/src/Services/Mandrill.php @@ -5,12 +5,13 @@ use DreamFactory\Core\Exceptions\InternalServerErrorException; use GuzzleHttp\Client; use Illuminate\Mail\Transport\MandrillTransport; +use \Illuminate\Support\Arr; class Mandrill extends BaseService { - protected function setTransport($config) + protected function setTransport(array $config) { - $key = array_get($config, 'key'); + $key = Arr::get($config, 'key'); $this->transport = static::getTransport($key); } diff --git a/src/Services/Smtp.php b/src/Services/Smtp.php index 1195de0..46bc863 100644 --- a/src/Services/Smtp.php +++ b/src/Services/Smtp.php @@ -3,17 +3,18 @@ namespace DreamFactory\Core\Email\Services; use DreamFactory\Core\Exceptions\InternalServerErrorException; -use Swift_SmtpTransport as SmtpTransport; +use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport as SmtpTransport; +use \Illuminate\Support\Arr; class Smtp extends BaseService { - protected function setTransport($config) + protected function setTransport(array $config) { - $host = array_get($config, 'host'); - $port = array_get($config, 'port'); - $encryption = array_get($config, 'encryption'); - $username = array_get($config, 'username'); - $password = array_get($config, 'password'); + $host = Arr::get($config, 'host'); + $port = Arr::get($config, 'port'); + $encryption = Arr::get($config, 'encryption'); + $username = Arr::get($config, 'username'); + $password = Arr::get($config, 'password'); $this->transport = static::getTransport($host, $port, $encryption, $username, $password); } @@ -25,10 +26,9 @@ protected function setTransport($config) * @param $username * @param $password * - * @return \Swift_SmtpTransport - * @throws \DreamFactory\Core\Exceptions\InternalServerErrorException + * @throws InternalServerErrorException */ - public static function getTransport($host, $port, $encryption, $username, $password) + public static function getTransport($host, $port, $encryption, $username, $password): SmtpTransport { if (empty($host)) { throw new InternalServerErrorException("Missing SMTP host. Check service configuration."); @@ -36,11 +36,7 @@ public static function getTransport($host, $port, $encryption, $username, $passw if (empty($port)) { throw new InternalServerErrorException("Missing SMTP port. Check service configuration."); } - $transport = new SmtpTransport($host, $port); - - if (!empty($encryption)) { - $transport->setEncryption($encryption); - } + $transport = new SmtpTransport($host, $port, boolval($encryption)); if (!empty($username) && !empty($password)) { $transport->setUsername($username); diff --git a/src/Services/SparkPost.php b/src/Services/SparkPost.php index 7e548aa..deb3cc5 100644 --- a/src/Services/SparkPost.php +++ b/src/Services/SparkPost.php @@ -5,13 +5,14 @@ use DreamFactory\Core\Exceptions\InternalServerErrorException; use GuzzleHttp\Client; use Illuminate\Mail\Transport\SparkPostTransport; +use \Illuminate\Support\Arr; class SparkPost extends BaseService { - protected function setTransport($config) + protected function setTransport(array $config) { - $key = array_get($config, 'key'); - $options = (array)array_get($config, 'options'); + $key = Arr::get($config, 'key'); + $options = (array)Arr::get($config, 'options'); $this->transport = static::getTransport($key, $options); }