Skip to content

Commit 2bbf727

Browse files
committed
Merge pull request #34 from kmelia/feature/add-proxy
[feature] add proxy options
2 parents a2ea884 + 9f93693 commit 2bbf727

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

DependencyInjection/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ public function getConfigTreeBuilder()
5656
->end()
5757
->scalarNode('api_key')->defaultNull()->end()
5858
->scalarNode('disable_delivery')->defaultFalse()->end()
59+
->arrayNode('proxy')
60+
->addDefaultsIfNotSet()
61+
->children()
62+
->booleanNode('use')->defaultFalse()->end()
63+
->scalarNode('host')->defaultNull()->end()
64+
->scalarNode('port')->defaultNull()->end()
65+
->scalarNode('user')->defaultNull()->end()
66+
->scalarNode('password')->defaultNull()->end()
67+
->end()
68+
->end()
5969
->end();
6070

6171
return $treeBuilder;

DependencyInjection/HipMandrillExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function load(array $configs, ContainerBuilder $container)
5151
$container->setParameter('hip_mandrill.default.sender', $config['default']['sender']);
5252
$container->setParameter('hip_mandrill.default.sender_name', $config['default']['sender_name']);
5353
$container->setParameter('hip_mandrill.default.subaccount', $config['default']['subaccount']);
54+
$container->setParameter('hip_mandrill.proxy', $config['proxy']);
5455

5556
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
5657
$loader->load('services.yml');

Dispatcher.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,29 @@ class Dispatcher
6060
*/
6161
protected $defaultSenderName;
6262

63+
/**
64+
* Proxy options
65+
*
66+
* @var array
67+
*/
68+
protected $proxy;
69+
6370
/**
6471
* @var bool
6572
*/
6673
protected $disableDelivery;
6774

68-
public function __construct($service, $defaultSender, $defaultSenderName, $subaccount, $disableDelivery) {
75+
public function __construct($service, $defaultSender, $defaultSenderName, $subaccount, $disableDelivery, $proxy) {
6976
$this->service = $service;
7077
$this->defaultSender = $defaultSender;
7178
$this->defaultSenderName = $defaultSenderName;
7279
$this->subaccount = $subaccount;
7380
$this->disableDelivery = $disableDelivery;
81+
$this->proxy = $proxy;
82+
83+
if ($this->useProxy()) {
84+
$this->addCurlProxyOptions();
85+
}
7486
}
7587

7688
/**
@@ -102,15 +114,32 @@ public function send(Message $message, $templateName = '', $templateContent = ar
102114

103115
if (!empty($templateName)) {
104116
return $this->service->messages->sendTemplate($templateName, $templateContent, $message->toArray(), $async, $ipPool, $sendAt);
105-
106117
}
107118

108119
return $this->service->messages->send($message->toArray(), $async, $ipPool, $sendAt);
109-
110120
}
111121

122+
private function useProxy()
123+
{
124+
return $this->proxy['use'];
125+
}
112126

113-
114-
115-
127+
private function addCurlProxyOptions()
128+
{
129+
if ($this->proxy['host'] !== null) {
130+
curl_setopt($this->service->ch, CURLOPT_PROXY, $this->proxy['host']);
131+
}
132+
133+
if ($this->proxy['port'] !== null) {
134+
curl_setopt($this->service->ch, CURLOPT_PROXYPORT, $this->proxy['port']);
135+
}
136+
137+
if ($this->proxy['user'] !== null && $this->proxy['password'] !== null) {
138+
curl_setopt($this->service->ch, CURLOPT_PROXYUSERPWD, sprintf(
139+
'%s:%s',
140+
$this->proxy['user'],
141+
$this->proxy['password']
142+
));
143+
}
144+
}
116145
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ hip_mandrill:
7272
7373
sender_name: John Doe
7474
subaccount: Project # Optionally define a subaccount to use
75+
proxy:
76+
use: true # when you are behing a proxy. Default value is 'false'
77+
host: example.com
78+
port: 80
79+
user: john
80+
password: doe123
7581
```
7682
7783
Now you're all set, send your first transactional mails:

Resources/config/services.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ services:
99

1010
hip_mandrill.dispatcher:
1111
class: %hip_mandrill.dispatcher.class%
12-
arguments: [@hip_mandrill.client, %hip_mandrill.default.sender%, %hip_mandrill.default.sender_name%, %hip_mandrill.default.subaccount%, %hip_mandrill.disable_delivery%]
13-
14-
12+
arguments: [@hip_mandrill.client, %hip_mandrill.default.sender%, %hip_mandrill.default.sender_name%, %hip_mandrill.default.subaccount%, %hip_mandrill.disable_delivery%, %hip_mandrill.proxy%]

0 commit comments

Comments
 (0)