From 3b5fea0db132150723f0b4d01f90e2d0eb0ef871 Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 11:30:30 +0200 Subject: [PATCH 1/8] Fixing mailgun authentication; use `'api'` as user. --- net/mail/transport/adapter/Mailgun.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index 276298d..2d7d10c 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -65,13 +65,13 @@ class Mailgun extends \li3_mailer\net\mail\transport\adapter\Simple { * @return mixed The return value of the `curl_exec` function. */ public function deliver($message, array $options = array()) { - list($url, $key, $parameters) = $this->_parameters($message, $options); + list($url, $auth, $parameters) = $this->_parameters($message, $options); $curl = new $this->_classes['curl'](); $curl->open(); $curl->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - $curl->set(CURLOPT_USERPWD, $key); + $curl->set(CURLOPT_USERPWD, "{$auth['user']}:{$auth['password']}"); $curl->set(CURLOPT_RETURNTRANSFER, 1); $curl->set(CURLOPT_CUSTOMREQUEST, 'POST'); @@ -105,7 +105,7 @@ public function deliver($message, array $options = array()) { * @see http://documentation.mailgun.net/api-sending.html * @param object $message The message to deliver. * @param array $options Given options. - * @return array An array including the API URL, secret key and parameters. + * @return array An array including the API URL, authentication credentials and parameters. */ protected function _parameters($message, array $options = array()) { $defaults = array('api' => 'https://api.mailgun.net/v2'); @@ -160,8 +160,9 @@ protected function _parameters($message, array $options = array()) { $parameters['v:' . $name] = $val; } } + $auth = array('user' => 'api', 'password' => $config['key']); - return array($url, $config['key'], $parameters); + return array($url, $auth, $parameters); } } From 9f7ca8209711e97ed08ec7d0362679c6b8dcdc95 Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 11:31:54 +0200 Subject: [PATCH 2/8] Use canoncial username instead of user for auth. --- net/mail/transport/adapter/Mailgun.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index 2d7d10c..0176ef9 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -71,7 +71,7 @@ public function deliver($message, array $options = array()) { $curl->open(); $curl->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - $curl->set(CURLOPT_USERPWD, "{$auth['user']}:{$auth['password']}"); + $curl->set(CURLOPT_USERPWD, "{$auth['username']}:{$auth['password']}"); $curl->set(CURLOPT_RETURNTRANSFER, 1); $curl->set(CURLOPT_CUSTOMREQUEST, 'POST'); @@ -160,7 +160,7 @@ protected function _parameters($message, array $options = array()) { $parameters['v:' . $name] = $val; } } - $auth = array('user' => 'api', 'password' => $config['key']); + $auth = array('username' => 'api', 'password' => $config['key']); return array($url, $auth, $parameters); } From a79a79a24d4f1a5134b6a5b5d1d8c04487ad65e4 Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 11:32:05 +0200 Subject: [PATCH 3/8] Removing whitespace. --- net/mail/transport/adapter/Mailgun.php | 1 - 1 file changed, 1 deletion(-) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index 0176ef9..8b818b0 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -164,7 +164,6 @@ protected function _parameters($message, array $options = array()) { return array($url, $auth, $parameters); } - } ?> \ No newline at end of file From d136a17dcedc091cb3f4421cdc6666eac564c6e1 Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 12:00:51 +0200 Subject: [PATCH 4/8] Temporarily use curl directly to get a working prototype. --- net/mail/transport/adapter/Mailgun.php | 32 +++++++++++--------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index 8b818b0..4479ba3 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -41,13 +41,6 @@ class Mailgun extends \li3_mailer\net\mail\transport\adapter\Simple { 'tracking', 'tracking-clicks', 'tracking-opens' ); - /** - * Classes used by `Mailgun`. - * - * @var array - */ - protected $_classes = array('curl' => 'lithium\net\socket\Curl'); - /** * Deliver a message with Mailgun's HTTP REST API via curl. * @@ -67,19 +60,22 @@ class Mailgun extends \li3_mailer\net\mail\transport\adapter\Simple { public function deliver($message, array $options = array()) { list($url, $auth, $parameters) = $this->_parameters($message, $options); - $curl = new $this->_classes['curl'](); - $curl->open(); - - $curl->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - $curl->set(CURLOPT_USERPWD, "{$auth['username']}:{$auth['password']}"); - $curl->set(CURLOPT_RETURNTRANSFER, 1); + $curl = curl_init($url); - $curl->set(CURLOPT_CUSTOMREQUEST, 'POST'); - $curl->set(CURLOPT_URL, $url); - $curl->set(CURLOPT_POSTFIELDS, $parameters); + curl_setopt_array($curl, array( + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, + CURLOPT_USERPWD => "{$auth['username']}:{$auth['password']}", + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $parameters + )); + $result = curl_exec($curl); - $result = $curl->read(); - $curl->close(); + $info = curl_getinfo($curl); + if ($info['http_code'] != '200') { + $result = false; + } + curl_close($curl); return $result; } From 3b86c7d9676b97fdeda7bf3cc64e111a912139f4 Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 12:01:23 +0200 Subject: [PATCH 5/8] Do not use mime endpoint for testing simplicity. --- net/mail/transport/adapter/Mailgun.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index 4479ba3..aaf5091 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -127,13 +127,23 @@ protected function _parameters($message, array $options = array()) { $error = "Domain should not end with '/'."; throw new RuntimeException($error); } - $url = array($config['api'], $config['domain'], 'messages.mime'); + $url = array($config['api'], $config['domain'], 'messages'); $url = join($url, "/"); } - $parameters = array('to' => $this->_address($message->to)); - list($headers, $body) = $this->_generate($message); - $parameters['message'] = $headers . "\r\n" . $body; + foreach (array('to', 'from', 'cc', 'bcc') as $field) { + if (!$message->$field) { + continue; + } + $parameters[$field] = $this->_address($message->$field); + } + + if ($text = $message->body('text')) { + $parameters += compact('text'); + } + if ($html = $message->body('html')) { + $parameters += compact('html'); + } foreach ($this->_extraParameters as $name => $type) { if (is_int($name)) { $name = $type; From 574c38ecf6ba7152de4f58a5aa8df2dc4a5d9c10 Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 12:04:43 +0200 Subject: [PATCH 6/8] Adding subject for mailgun. --- net/mail/transport/adapter/Mailgun.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index aaf5091..d97471d 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -137,6 +137,9 @@ protected function _parameters($message, array $options = array()) { } $parameters[$field] = $this->_address($message->$field); } + if ($subject = $message->subject) { + $parameters += compact('subject'); + } if ($text = $message->body('text')) { $parameters += compact('text'); From 52274f8c2151442dd6127215adc37c1ed82294ab Mon Sep 17 00:00:00 2001 From: David Persson Date: Mon, 30 Sep 2013 12:07:23 +0200 Subject: [PATCH 7/8] Return message id on success. --- net/mail/transport/adapter/Mailgun.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/net/mail/transport/adapter/Mailgun.php b/net/mail/transport/adapter/Mailgun.php index d97471d..954bdfa 100644 --- a/net/mail/transport/adapter/Mailgun.php +++ b/net/mail/transport/adapter/Mailgun.php @@ -3,6 +3,8 @@ namespace li3_mailer\net\mail\transport\adapter; use RuntimeException; +use lithium\net\http\Media; + /** * The `Mailgun` adapter sends email through Mailgun's HTTP REST API. @@ -55,7 +57,7 @@ class Mailgun extends \li3_mailer\net\mail\transport\adapter\Simple { * @see http://php.net/curl * @param object $message The message to deliver. * @param array $options Options (see `_parameters()`). - * @return mixed The return value of the `curl_exec` function. + * @return string The message id on success; `false` on error. */ public function deliver($message, array $options = array()) { list($url, $auth, $parameters) = $this->_parameters($message, $options); @@ -77,7 +79,8 @@ public function deliver($message, array $options = array()) { } curl_close($curl); - return $result; + $result = Media::decode('json', $result); + return $result['id']; } /** From d8c99986f78e91f11858bfeb63b44dbe4f66f25a Mon Sep 17 00:00:00 2001 From: Andrzej Grzegorz Borkowski Date: Tue, 10 Dec 2013 01:37:39 +0100 Subject: [PATCH 8/8] g11n `$t()` support for views and email templates --- template/Mail.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/template/Mail.php b/template/Mail.php index 33b3d9d..82d5170 100644 --- a/template/Mail.php +++ b/template/Mail.php @@ -4,7 +4,7 @@ use lithium\core\Object; use lithium\core\Libraries; - +use lithium\g11n\Message; /** * The `Mail` is a special `View` class that is responsible for rendering * (mail) message bodies and providing helpers. @@ -39,6 +39,7 @@ class Mail extends \lithium\template\View { */ protected function _init() { Object::_init(); + extract(Message::aliases()); $type = isset($this->_config['type']) ? $this->_config['type'] : null; if ($type === 'text') { @@ -52,7 +53,10 @@ protected function _init() { return htmlspecialchars((string) $data, ENT_QUOTES, $encoding); }; } - $this->outputFilters += compact('h') + $this->_config['outputFilters']; + $t = function($data, array $options = array()) use ($t) { + echo $t((string) $data, $options); + }; + $this->outputFilters += compact('h', 't') + $this->_config['outputFilters']; foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) {