Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a component example for illuminate/mail #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/mail/compiled/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
10 changes: 10 additions & 0 deletions components/mail/composer.json
Original file line number Diff line number Diff line change
@@ -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.*"
}
}
141 changes: 141 additions & 0 deletions components/mail/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Slim\Factory\AppFactory;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\MailManager;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Illuminate\View\FileViewFinder;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Blade;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Contracts\Mail\Factory;
use Illuminate\Support\Facades\Facade;
use Zeuxisoo\Whoops\Slim\WhoopsMiddleware;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Compilers\BladeCompiler;

require_once 'vendor/autoload.php';
require_once '../../src/App.php';
require_once '../../src/ExceptionHandler.php';

/**
* Illuminate/mail
*
* @source https://github.com/illuminate/mail
*/

// Instantiate App
$app = AppFactory::create();

// Middleware
$app->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' => '[email protected]',
'mail.reply_to' => '[email protected]',
'mail.to' => '[email protected]',
'mail.return_path' => '[email protected]',
];

$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('[email protected]')->send(
(new ExampleMailable)
->from('[email protected]')
->subject('Hello world')
);

/** @var \Swift_Message $message */
$message = $mailManager->getSwiftMailer()->getTransport()->messages()[0];

$response->getBody()->write('<pre>');
$response->getBody()->write($message->toString());
$response->getBody()->write('</pre>');

return $response;
});

class ExampleMailable extends Mailable
{
public function build()
{
return $this->view('example');
}
}

$app->run();
28 changes: 28 additions & 0 deletions components/mail/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# <img src="../../torch-logo.png" alt="Torch Logo" align="right">Torch - Using Laravel's Illuminate Components Independently

[&lt;&lt; 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.
1 change: 1 addition & 0 deletions components/mail/templates/example.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world...
5 changes: 1 addition & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down