Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
694 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
{ | ||
"name": "sendgrid/sendgrid", | ||
"description": "This library allows you to quickly and easily send emails through SendGrid using PHP.", | ||
"version": "2.2.1", | ||
"homepage": "http://github.com/sendgrid/sendgrid-php", | ||
"license": "MIT", | ||
"keywords": ["SendGrid", "sendgrid", "email", "send", "grid"], | ||
"require": { | ||
"php": ">=5.3", | ||
"sendgrid/smtpapi": "0.*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "3.7.*", | ||
"vlucas/phpdotenv": "1.0.2" | ||
}, | ||
"replace": { | ||
"sendgrid/sendgrid-php": "*" | ||
}, | ||
"type": "library", | ||
"autoload": { | ||
"psr-0": {"SendGrid": "lib/"} | ||
} | ||
} | ||
{ | ||
"name": "sendgrid/sendgrid", | ||
"description": "This library allows you to quickly and easily send emails through SendGrid using PHP.", | ||
"version": "3.0.0-RC1", | ||
"homepage": "http://github.com/sendgrid/sendgrid-php", | ||
"license": "MIT", | ||
"keywords": ["SendGrid", "sendgrid", "email", "send", "grid"], | ||
"require": { | ||
"php": ">=5.3", | ||
"sendgrid/smtpapi": "~0.4", | ||
"guzzle/guzzle": "~3.9" | ||
}, | ||
"require-dev": { | ||
"mockery/mockery": "~0.9", | ||
"phpunit/phpunit": "~4.4", | ||
"vlucas/phpdotenv": "~1.0" | ||
}, | ||
"replace": { | ||
"sendgrid/sendgrid-php": "*" | ||
}, | ||
"type": "library", | ||
"autoload": { | ||
"psr-0": {"SendGrid": "lib/"} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,96 @@ | ||
<?php | ||
|
||
class SendGrid { | ||
const VERSION = '2.2.1'; | ||
|
||
protected $namespace = 'SendGrid', | ||
$headers = array('Content-Type' => 'application/json'), | ||
$options, | ||
$web; | ||
public $api_user, | ||
$api_key, | ||
$url, | ||
$version = self::VERSION; | ||
|
||
|
||
public function __construct($api_user, $api_key, $options=array()) { | ||
$this->api_user = $api_user; | ||
$this->api_key = $api_key; | ||
|
||
$options['turn_off_ssl_verification'] = (isset($options['turn_off_ssl_verification']) && $options['turn_off_ssl_verification'] == true); | ||
$protocol = isset($options['protocol']) ? $options['protocol'] : 'https'; | ||
$host = isset($options['host']) ? $options['host'] : 'api.sendgrid.com'; | ||
$port = isset($options['port']) ? $options['port'] : ''; | ||
$endpoint = isset($options['endpoint']) ? $options['endpoint'] : '/api/mail.send.json'; | ||
|
||
$this->url = isset($options['url']) ? $options['url'] : $protocol . '://' . $host . ($port ? ':' . $port : '') . $endpoint; | ||
|
||
$this->options = $options; | ||
} | ||
|
||
public function send(SendGrid\Email $email) { | ||
$form = $email->toWebFormat(); | ||
$form['api_user'] = $this->api_user; | ||
$form['api_key'] = $this->api_key; | ||
|
||
$response = $this->makeRequest($form); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Makes the actual HTTP request to SendGrid | ||
* @param $form array web ready version of SendGrid\Email | ||
* @return stdClass parsed JSON returned from SendGrid | ||
*/ | ||
private function makeRequest($form) { | ||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_URL, $this->url); | ||
curl_setopt($ch, CURLOPT_POST, 1); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $form); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($ch, CURLOPT_USERAGENT, 'sendgrid/' . $this->version . ';php'); | ||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, !$this->options['turn_off_ssl_verification']); | ||
|
||
$response = curl_exec($ch); | ||
|
||
$error = curl_error($ch); | ||
if ($error) { | ||
throw new Exception($error); | ||
} | ||
|
||
curl_close($ch); | ||
|
||
return json_decode($response); | ||
} | ||
|
||
public static function register_autoloader() { | ||
spl_autoload_register(array('SendGrid', 'autoloader')); | ||
} | ||
|
||
public static function autoloader($class) { | ||
// Check that the class starts with 'SendGrid' | ||
if ($class == 'SendGrid' || stripos($class, 'SendGrid\\') === 0) { | ||
$file = str_replace('\\', '/', $class); | ||
|
||
if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) { | ||
require_once(dirname(__FILE__) . '/' . $file . '.php'); | ||
} | ||
} | ||
} | ||
} | ||
<?php | ||
|
||
class SendGrid { | ||
const VERSION = '3.0.0-RC1'; | ||
|
||
protected $namespace = 'SendGrid', | ||
$headers = array('Content-Type' => 'application/json'), | ||
$client, | ||
$options; | ||
public $api_user, | ||
$api_key, | ||
$url, | ||
$endpoint, | ||
$version = self::VERSION; | ||
|
||
|
||
public function __construct($api_user, $api_key, $options = array()) { | ||
$this->api_user = $api_user; | ||
$this->api_key = $api_key; | ||
|
||
$options['turn_off_ssl_verification'] = (isset($options['turn_off_ssl_verification']) && $options['turn_off_ssl_verification'] == true); | ||
$protocol = isset($options['protocol']) ? $options['protocol'] : 'https'; | ||
$host = isset($options['host']) ? $options['host'] : 'api.sendgrid.com'; | ||
$port = isset($options['port']) ? $options['port'] : ''; | ||
$this->options = $options; | ||
|
||
$this->url = isset($options['url']) ? $options['url'] : $protocol . '://' . $host . ($port ? ':' . $port : ''); | ||
$this->endpoint = isset($options['endpoint']) ? $options['endpoint'] : '/api/mail.send.json'; | ||
|
||
$this->client = new \Guzzle\Http\Client($this->url, array( | ||
'request.options' => array( | ||
'verify' => !$this->options['turn_off_ssl_verification'], | ||
'exceptions' => false // FIXME: This might not be wise but we don't want guzzle throwing | ||
) | ||
)); | ||
$this->client->setUserAgent('sendgrid/' . $this->version . ';php'); | ||
} | ||
|
||
/** | ||
* @return array The protected options array | ||
*/ | ||
public function getOptions() { | ||
return $this->options; | ||
} | ||
|
||
/** | ||
* Makes a post request to SendGrid to send an email | ||
* @param SendGrid\Email $email Email object built | ||
* @throws SendGrid\Exception if the response code is not 200 | ||
* @return stdClass SendGrid response object | ||
*/ | ||
public function send(SendGrid\Email $email) { | ||
$form = $email->toWebFormat(); | ||
$form['api_user'] = $this->api_user; | ||
$form['api_key'] = $this->api_key; | ||
|
||
$response = $this->postRequest($this->endpoint, $form); | ||
|
||
if ($response->code != 200) { | ||
throw new SendGrid\Exception($response->raw_body, $response->code); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Makes the actual HTTP request to SendGrid | ||
* @param $endpoint string endpoint to post to | ||
* @param $form array web ready version of SendGrid\Email | ||
* @return SendGrid\Response | ||
*/ | ||
public function postRequest($endpoint, $form) { | ||
$req = $this->client->post($endpoint, null, $form); | ||
|
||
$res = $req->send(); | ||
|
||
$response = new SendGrid\Response($res->getStatusCode(), $res->getHeaders(), $res->getBody(true), $res->json()); | ||
|
||
return $response; | ||
} | ||
|
||
public static function register_autoloader() { | ||
spl_autoload_register(array('SendGrid', 'autoloader')); | ||
} | ||
|
||
public static function autoloader($class) { | ||
// Check that the class starts with 'SendGrid' | ||
if ($class == 'SendGrid' || stripos($class, 'SendGrid\\') === 0) { | ||
$file = str_replace('\\', '/', $class); | ||
|
||
if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) { | ||
require_once(dirname(__FILE__) . '/' . $file . '.php'); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.