itcan's avatar

Laravel 5 mandrill guzzle custom curl options

Hey guys, I am using Mandrill and Guzzle for emailing and this works fine! But I want to add some config parameters to the GuzzleHttp\Client, because I want to specify a ip address (CURLOPT_INTERFACE) (my server has multiple ip-addresses). Can anybody help me?

0 likes
8 replies
phildawson's avatar

I would say you need to switchout Illuminate\Mail\TransportManager for your own where the driver is created if it's specifically for Mandrill.

    protected function createMandrillDriver()
    {
        $client = new HttpClient([
                'curl' => [CURLOPT_INTERFACE => '1.2.3.4']
        ]);

        $config = $this->app['config']->get('services.mandrill', []);

        return new MandrillTransport($client, $config['secret']);
    }
itcan's avatar

mmm okay... I would like to set it globally, so all Guzzle requests use this setting...

phildawson's avatar

Mm good question as it's new-d up directly in the method $client = new HttpClient; and not resolved out the container it's pointless binding like so which would be fine normally.

<?php

class Example
{
    function __construct(GuzzleHttp\Client $client)
    {
        $this->client = $client;
    }

    function example()
    {
        $this->client->request('GET', 'http://laravel.com');
    }
}

$this->app->bind(GuzzleHttp\Client::class, function()
{
    return new GuzzleHttp\Client([
        'curl' => [
            CURLOPT_INTERFACE => '1.2.3.4'
        ]
    ]);
});

get('/', function(){

    app(Example::class)->example();

    exit;
});

I can't see how a global options can be set.

@pmall any ideas?

itcan's avatar

Binding does not work, tried that also... :-(

phildawson's avatar

Yeah sorry I should have been clear, the binding won't work. The example above placed in the routes.php does work but thats because the dependency in the Example construct is being resolved out of the container, which the binding returns the config'd class.

The issue is in the TransportManager class it isn't injected but new'd up directly inside the method.

 protected function createMandrillDriver()
    {
        $client = new HttpClient;

    ...
itcan's avatar

mmm, so i need to create my own transportmanager?

phildawson's avatar
Level 26

@itcan

  1. So I would comment the default MailServiceProvider in config/app.php
// Illuminate\Mail\MailServiceProvider::class,

and add your own

App\Providers\CustomMailServiceProvider::class,
  1. Make that CustomMailServiceProvider.php it'll be something like
<?php

namespace App\Providers;

use App\CustomTransportManager;
use Illuminate\Mail\MailServiceProvider;

class CustomMailServiceProvider extends MailServiceProvider
{
    protected function registerSwiftTransport()
    {
        $this->app['swift.transport'] = $this->app->share(function ($app) {
            return new CustomTransportManager($app);
        });
    }
}
  1. Then create that CustomTransportManager.php in app
<?php

namespace App;

use Illuminate\Mail\TransportManager;

class CustomTransportManager extends TransportManager
{
    protected function createMandrillDriver()
    {
        $client = new HttpClient([
                'curl' => [CURLOPT_INTERFACE => '1.2.3.4']
        ]);

        $config = $this->app['config']->get('services.mandrill', []);

        return new MandrillTransport($client, $config['secret']);
    }
}

I haven't tested any of this but that should work.

1 like

Please or to participate in this conversation.