Skip to content

Instantly share code, notes, and snippets.

@fedeisas
Last active December 18, 2015 23:29
Show Gist options
  • Save fedeisas/5862227 to your computer and use it in GitHub Desktop.
Save fedeisas/5862227 to your computer and use it in GitHub Desktop.
Inline CSS Styles on Laravel 4
<?php
/* 1. Add the dependency to your composer.json.
// I'm using https://packagist.org/packages/tijsverkoyen/css-to-inline-styles
*/
/* 2. Extend \Illuminate\Mail\Mailer to override the getView() method
// app/libraries/MyApp/Mailer.php
*/
namespace MyApp\Mail;
class Mailer extends \Illuminate\Mail\Mailer {
/**
* Render the given view.
*
* @param string $view
* @param array $data
* @return \Illuminate\View\View
*/
protected function getView($view, $data)
{
$cssInline = new \TijsVerkoyen\CssToInlineStyles\CssToInlineStyles();
$view = $this->views->make($view, $data)->render();
$cssInline->setUseInlineStylesBlock();
$cssInline->setStripOriginalStyleTags();
$cssInline->setCleanup();
$cssInline->setHTML($view);
return $cssInline->convert();
}
}
/* 3. Extend the \Illuminate\Mail\MailServiceProvider to override the register() method
// app/libraries/MyApp/MailServiceProvider.php
*/
namespace MyApp\Mail;
class MailServiceProvider extends \Illuminate\Mail\MailServiceProvider {
public function register()
{
$this->registerSwiftMailer();
$this->app['mailer'] = $this->app->share(function($app)
{
// Once we have create the mailer instance, we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new \MyApp\Mail\Mailer($app['view'], $app['swift.mailer']);
$mailer->setLogger($app['log'])->setQueue($app['queue']);
$mailer->setContainer($app);
// If a "from" address is set, we will set it on the mailer so that all mail
// messages sent by the applications will utilize the same "from" address
// on each one, which makes the developer's life a lot more convenient.
$from = $app['config']['mail.from'];
if (is_array($from) and isset($from['address']))
{
$mailer->alwaysFrom($from['address'], $from['name']);
}
// Here we will determine if the mailer should be in "pretend" mode for this
// environment, which will simply write out e-mail to the logs instead of
// sending it over the web, which is useful for local dev enviornments.
$pretend = $app['config']->get('mail.pretend', false);
$mailer->pretend($pretend);
return $mailer;
});
}
}
/* 4. Comment the default service in app/config/app.php and add the new one you have just created
// app/config/app.php
*/
// ommited some parts for brevity
return {
'providers' => array(
//'Illuminate\Mail\MailServiceProvider',
// Mail Hack
'MyApp\Mail\MailServiceProvider',
)
}
// 5. I'm storing this classes in app/libraries and Laravel should know of this path to works its autoloading magic
// app/start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/libraries', // ADD THIS LINE
app_path().'/mailers',
app_path().'/models',
app_path().'/database/seeds',
));
@RobinMalfait
Copy link

Hello man :)

Nice but i get an error Class 'MyApp\Mail\MailServiceProvider' not found and I did exactly what you are saying...

@Zolli
Copy link

Zolli commented Dec 12, 2013

You need to dump the autoloader, try the php artisan optimize command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment