diff --git a/components/mail/compiled/.gitignore b/components/mail/compiled/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/components/mail/compiled/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/components/mail/composer.json b/components/mail/composer.json new file mode 100644 index 0000000..686e824 --- /dev/null +++ b/components/mail/composer.json @@ -0,0 +1,10 @@ +{ + "require": { + "filp/whoops": "2.7.*", + "illuminate/mail": "^8.0", + "illuminate/view": "^8.0", + "slim/psr7": "1.2.*", + "slim/slim": "4.*", + "zeuxisoo/slim-whoops": "0.7.*" + } +} diff --git a/components/mail/index.php b/components/mail/index.php new file mode 100644 index 0000000..67c142d --- /dev/null +++ b/components/mail/index.php @@ -0,0 +1,141 @@ +add(new WhoopsMiddleware(['enable' => true])); + +$app->get('/', function (Request $request, Response $response) { + $container = new Container(); + + $container['config'] = [ + 'mail.driver' => null, + 'mail.default' => 'array', // used for testing + 'mail.mailers.array' => [ + 'transport' => 'array' + ], + 'mail.mailers.sendmail' => [ + 'transport' => 'sendmail', + 'path' => '/usr/sbin/sendmail -bs' + ], + 'mail.from' => 'person@example.com', + 'mail.reply_to' => 'person@example.com', + 'mail.to' => 'person@example.com', + 'mail.return_path' => 'person@example.com', + ]; + + $container->bind('exception.handler', ExceptionHandler::class); + + $container->singleton('mail.manager', function ($app) { + return new MailManager($app); + }); + + $container->bind('mailer', function ($app) { + return $app->make('mail.manager')->mailer(); + }); + + $container->singleton('events', function ($app) { + return new Dispatcher($app); + }); + + $container->alias('mail.manager', Factory::class); + + $container->singleton('view', function ($app) { + // the following code is taken from the view example component + + // Note that you can set several directories where your templates are located + $pathsToTemplates = [__DIR__ . '/templates']; + $pathToCompiledTemplates = __DIR__ . '/compiled'; + + // Dependencies + $filesystem = new Filesystem; + $eventDispatcher = $app['events']; + + // Create View Factory capable of rendering PHP and Blade templates + $viewResolver = new EngineResolver; + $bladeCompiler = new BladeCompiler($filesystem, $pathToCompiledTemplates); + + $viewResolver->register('blade', function () use ($bladeCompiler) { + return new CompilerEngine($bladeCompiler); + }); + + $viewFinder = new FileViewFinder($filesystem, $pathsToTemplates); + $viewFactory = new \Illuminate\View\Factory($viewResolver, $viewFinder, $eventDispatcher); + $viewFactory->setContainer($app); + Facade::setFacadeApplication($app); + $app->instance(\Illuminate\Contracts\View\Factory::class, $viewFactory); + $app->alias( + \Illuminate\Contracts\View\Factory::class, + (new class extends View { + public static function getFacadeAccessor() { return parent::getFacadeAccessor(); } + })::getFacadeAccessor() + ); + $app->instance(BladeCompiler::class, $bladeCompiler); + $app->alias( + BladeCompiler::class, + (new class extends Blade { + public static function getFacadeAccessor() { return parent::getFacadeAccessor(); } + })::getFacadeAccessor() + ); + + return $viewFactory; + }); + + /** @var \Illuminate\Mail\MailManager $mailManager */ + $mailManager = $container->make('mail.manager'); + + // send an email message + $mailManager->to('person@example.com')->send( + (new ExampleMailable) + ->from('person@example.com') + ->subject('Hello world') + ); + + /** @var \Swift_Message $message */ + $message = $mailManager->getSwiftMailer()->getTransport()->messages()[0]; + + $response->getBody()->write('
');
+    $response->getBody()->write($message->toString());
+    $response->getBody()->write('
'); + + return $response; +}); + +class ExampleMailable extends Mailable +{ + public function build() + { + return $this->view('example'); + } +} + +$app->run(); diff --git a/components/mail/readme.md b/components/mail/readme.md new file mode 100644 index 0000000..8714121 --- /dev/null +++ b/components/mail/readme.md @@ -0,0 +1,28 @@ +# Torch LogoTorch - Using Laravel's Illuminate Components Independently + +[<< Back to Torch](../../readme.md) + +# Mail + +This component shows how to use Laravel's [Mail](https://laravel.com/docs/8.x/mail) features in non-Laravel applications. + +### To Do + +An example that allows you to use the queue for email delivery. + +## Usage + +From this directory, run the following to serve a web site locally showing the output of the `index.php` file. + +```bash +$ composer install +$ php -S localhost:8000 +``` + +Now you can visit [http://localhost:8000/](http://localhost:8000/) in your browser to view the output of this example. + +## Options and information + +To learn more about this component's dependencies and options, view the source of [index.php](index.php). + +This example uses the Laravel Array transport for email to avoid sending an email to a recipient. diff --git a/components/mail/templates/example.blade.php b/components/mail/templates/example.blade.php new file mode 100644 index 0000000..190f584 --- /dev/null +++ b/components/mail/templates/example.blade.php @@ -0,0 +1 @@ +Hello world... \ No newline at end of file diff --git a/readme.md b/readme.md index 15771b6..d751e27 100644 --- a/readme.md +++ b/readme.md @@ -31,6 +31,7 @@ Now you can visit [http://localhost:8000/](http://localhost:8000/) in your brows * [Filesystem](https://github.com/mattstauffer/Torch/tree/master/components/filesystem) * [HTTP Client](https://github.com/mattstauffer/Torch/tree/master/components/http) * [Log](https://github.com/mattstauffer/Torch/tree/master/components/log) +* [Mail](https://github.com/mattstauffer/Torch/tree/master/components/mail) * [Middleware](https://github.com/mattstauffer/Torch/tree/master/components/middleware) * [Pagination](https://github.com/mattstauffer/Torch/tree/master/components/pagination) * [Queue](https://github.com/mattstauffer/Torch/tree/master/components/queue) @@ -43,10 +44,6 @@ Now you can visit [http://localhost:8000/](http://localhost:8000/) in your brows * [Validation](https://github.com/mattstauffer/Torch/tree/master/components/validation) -### Need to be moved over from 4.2 - -* [Mail](https://github.com/mattstauffer/Torch/tree/4.2/public/mail) - Never finished porting from 4.2-5.1 and then it never got the simpler upgrades from 5.1 until today - ## Other Packages ### Done