Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

mailgun + translation #7

Open
wants to merge 8 commits into from

4 participants

@agborkowski

No description provided.

@eLod
Owner

hey sorry for such a long time without response.

first thank you for your PR, can you please rebase onto my master without the other commits, they seem to be in conflict with the current codebase?

also why not simply add all the aliases? e.g. this->outputFilters += compact('h'') + Message::aliases() + $this->_config['outputFilters'];? (or even putting them into _config['outputFilters'] in _init())

@flazer

With this solution about overwriting $t you are not able to use placeholders in translated text. So one possibility is to use "return" in this closure instead of echo, or just delete $t.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 49 additions and 33 deletions.
  1. +43 −31 net/mail/transport/adapter/Mailgun.php
  2. +6 −2 template/Mail.php
View
74 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.
@@ -42,13 +44,6 @@ class Mailgun extends \li3_mailer\net\mail\transport\adapter\Simple {
);
/**
- * 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.
*
* _NOTE: Uses the `messages.mime` API endpoint, not the
@@ -62,26 +57,30 @@ 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, $key, $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_RETURNTRANSFER, 1);
-
- $curl->set(CURLOPT_CUSTOMREQUEST, 'POST');
- $curl->set(CURLOPT_URL, $url);
- $curl->set(CURLOPT_POSTFIELDS, $parameters);
-
- $result = $curl->read();
- $curl->close();
+ list($url, $auth, $parameters) = $this->_parameters($message, $options);
+
+ $curl = curl_init($url);
+
+ 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);
+
+ $info = curl_getinfo($curl);
+ if ($info['http_code'] != '200') {
+ $result = false;
+ }
+ curl_close($curl);
- return $result;
+ $result = Media::decode('json', $result);
+ return $result['id'];
}
/**
@@ -105,7 +104,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');
@@ -131,13 +130,26 @@ 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 ($subject = $message->subject) {
+ $parameters += compact('subject');
+ }
+
+ 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;
@@ -160,10 +172,10 @@ protected function _parameters($message, array $options = array()) {
$parameters['v:' . $name] = $val;
}
}
+ $auth = array('username' => 'api', 'password' => $config['key']);
- return array($url, $config['key'], $parameters);
+ return array($url, $auth, $parameters);
}
-
}
?>
View
8 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])) {
Something went wrong with that request. Please try again.