Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active June 13, 2018 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caffe1neAdd1ct/7809a32a0a447947b0902873c24ab7f5 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/7809a32a0a447947b0902873c24ab7f5 to your computer and use it in GitHub Desktop.
Laravel 4.2 Mandrill Guzzle 4,5,6 Support
<?php
/** app/config/app.php */
return [
'providers' => [
Custom\Support\MailServiceProvider::class,
],
];
{
"autoload": {
"psr-0": {
"Custom": "app/lib"
}
},
"require": {
"guzzlehttp/guzzle": "~6.0",
}
}
<?php
/** app/lib/Custom/Support/MailMandrillTransport.php */
namespace Custom\Support;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_Mime_Message;
use GuzzleHttp\Client;
/**
* Extension on in-built laravel mail mandrill transport to maintain compatibility with newer versions of Guzzle.
* @see MandrillTransport
* @author Kevin Andrews <kevin@zvps.uk>
*/
class MailMandrillTransport extends MandrillTransport
{
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$client = $this->getHttpClient();
$request = $this->createRequest($client, $message);
$response = $client->send($request);
}
/**
* Switch between Guzzle methods depending on versions
* @param Client $client
* @param string $message
* @return \GuzzleHttp\Psr7\Request
*/
public function createRequest(Client $client, $message)
{
$request = false;
$bodyParams = [
'key' => $this->key,
'raw_message' => (string) $message,
'async' => false,
];
if(version_compare($client::VERSION, "6.0.0", '>=')) {
$body = \GuzzleHttp\json_encode($bodyParams);
$request = new \GuzzleHttp\Psr7\Request(
'post',
'https://mandrillapp.com/api/1.0/messages/send-raw.json',
['Content-Type' => 'application/json'],
$body
);
} elseif (version_compare($client::VERSION, "6.0.0", '<=')) {
$request = $client->createRequest(
'post',
'https://mandrillapp.com/api/1.0/messages/send-raw.json',
['body' => [$bodyParams]]
);
}
return $request;
}
}
<?php
/** app/lib/Custom/Support/MailServiceProvider.php */
namespace Custom\Support;
/**
* Extension on in-built laravel mail provider to maintain compatibility with newer versions of Guzzle.
* @see MailServiceProvider
* @author Kevin Andrews <kevin@zvps.uk>
*/
class MailServiceProvider extends \Illuminate\Mail\MailServiceProvider
{
public function register()
{
parent::register();
if ($this->app->runningInConsole()) {
$this->registerArtisanCommands();
}
}
/**
* Register the artisan commands.
*/
protected function registerArtisanCommands()
{
$this->commands([\Custom\Support\MailTestCommand::class]);
}
/**
* Register the Mandrill Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerMandrillTransport($config)
{
$mandrill = $this->app['config']->get('services.mandrill', array());
$this->app->bindShared('swift.transport', function() use ($mandrill)
{
return new MailMandrillTransport($mandrill['secret']);
});
}
}
<?php
/** app/lib/Custom/Support/MailTestCommand.php */
namespace Custom\Support;
use Illuminate\Console\Command;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\Message;
use Symfony\Component\Console\Input\InputOption;
class MailTestCommand extends Command
{
/**
* Laravel 4 signature
* @var string
*/
protected $name = 'mail:test';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a test email and send it to address specified using --address=';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
*
*/
public function fire()
{
$this->handle();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if(empty($this->option('address'))) {
$this->error('Missing required option --address=test@address.tld');
return;
}
/* @var $mailer Mailer */
$mailer = app('mailer');
$data = [
'subject' => 'This is a test email from your laravel application.',
'name' => 'Mailer Test',
'email' => [$this->option('address')],
'msg' => 'Hopefully you recieve this message.'
];
$mailer->send('emails.blank', $data, function($message) use ($data) {
/* @var $message Message */
$message->to($data['email'],$data['name'])->subject($data['subject']);
});
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['address', null, InputOption::VALUE_REQUIRED, 'Email address to send the test email to.'],
];
}
}
@Caffe1neAdd1ct
Copy link
Author

To correctly autoload the classes inside app/lib/Custom/ the following command will need to be run:

php artisan dump-autoload

This will create the needed entries inside the composer autoload generated file.

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