Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions net/mail/transport/adapter/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -41,13 +43,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.
*
Expand All @@ -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'];
}

/**
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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);
}

}

?>
8 changes: 6 additions & 2 deletions template/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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') {
Expand All @@ -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])) {
Expand Down