php - Change Mailing Transport in Laravel 4 -
it appears l4 hardcoded use smtp swiftmailer. how can change transport type sendmail?
it's not easy swapping out transport another. said, transport hardcoded in illuminate\mail\mailserviceprovider.
you extend l4 mail implementation own, swapping out in config/app.php providers array:
- replace provider array mail provider:
config/app.php
... 'providers' => array( 'sendmailserviceprovider',
- add sendmail path binary:
config/mail.php
'sendmail_path' => '/usr/bin/sendmail -bs',
- make sure composer.json file autoload "classmap app/libraries:"
composer.json:
.... "autoload": { "classmap": [ "app/libraries", ....
- add sendmail service provider:
app/libraries/sendmailserviceprovider.php
<?php use \swift_mailer; use illuminate\support\serviceprovider; use illuminate\mail\mailer mailer; use swift_sendmailtransport sendmailtransport; class sendmailserviceprovider extends serviceprovider { /** * indicates if loading of provider deferred. * * @var bool */ protected $defer = true; /** * register service provider. * * @return void */ public function register() { $this->registerswiftmailer(); $this->app['mailer'] = $this->app->share(function($app) { // once have create mailer instance, set container instance // on mailer. allows resolve mailer classes via containers // maximum testability on said classes instead of passing closures. $mailer = new mailer($app['view'], $app['swift.mailer']); $mailer->setlogger($app['log']); $mailer->setcontainer($app); $from = $app['config']['mail.from']; // if "from" address set, set on mailer mail // messages sent applications utilize same "from" address // on each one, makes developer's life lot more convenient. if (is_array($from) , isset($from['address'])) { $mailer->alwaysfrom($from['address'], $from['name']); } return $mailer; }); } /** * register swift mailer instance. * * @return void */ protected function registerswiftmailer() { $config = $this->app['config']['mail']; $this->registerswifttransport($config); // once have transporter registered, register actual swift // mailer instance, passing in transport instances, allows // override transporter instances during app start-up if necessary. $this->app['swift.mailer'] = $this->app->share(function($app) { return new swift_mailer($app['swift.transport']); }); } /** * register swift transport instance. * * @param array $config * @return void */ protected function registerswifttransport($config) { $this->app['swift.transport'] = $this->app->share(function($app) use ($config) { extract($config); // swift smtp transport instance allow use smtp backend // delivering mail such sendgrid, amazon sms, or custom server // developer has available. pass configured host. $transport = sendmailtransport::newinstance($sendmail_path); // once have transport check presence of username // , password. if have set credentials on swift // transporter instance we'll authenticate delivery. if (isset($username)) { $transport->setusername($username); $transport->setpassword($password); } return $transport; }); } /** * services provided provider. * * @return array */ public function provides() { return array('mailer', 'swift.mailer', 'swift.transport'); } }
not tested it, should work fine. try put in routes.php , play bit:
route::get('/', function() { mail::send('emails.auth.reminder', array('token' => '123456'), function($m) { $m->from('us@example.com', 'laravel'); $m->to('foo@example.com')->cc('bar@example.com'); }); });
Comments
Post a Comment