Skip to content

Commit

Permalink
Merge branch 'v3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiezane committed Apr 13, 2015
2 parents 02e9e58 + 623132a commit f8a9fca
Show file tree
Hide file tree
Showing 13 changed files with 694 additions and 239 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -15,6 +15,7 @@ php:
- 5.3
- 5.4
- 5.5
- 5.6
env:
global:
- S3_POLICY: ewogICJleHBpcmF0aW9uIjogIjIxMDAtMDEtMDFUMTI6MDA6MDAuMDAwWiIsCiAgImNvbmRpdGlvbnMiOiBbCiAgICB7ImFjbCI6ICJwdWJsaWMtcmVhZCIgfSwKICAgIHsiYnVja2V0IjogInNlbmRncmlkLW9wZW4tc291cmNlIiB9LAogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInNlbmRncmlkLXBocC8iXSwKICAgIFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLCAyMDQ4LCAyNjg0MzU0NTZdLAogICAgWyJlcSIsICIkQ29udGVudC1UeXBlIiwgImFwcGxpY2F0aW9uL3ppcCJdCiAgXQp9Cgo=
Expand Down
51 changes: 42 additions & 9 deletions README.md
@@ -1,13 +1,25 @@
# SendGrid-php
# SendGrid-PHP

This library allows you to quickly and easily send emails through SendGrid using PHP.

WARNING: This module was recently upgraded from [1.1.7](https://github.com/sendgrid/sendgrid-php/tree/v1.1.7) to 2.X. There were API breaking changes for various method names. See [usage](https://github.com/sendgrid/sendgrid-php#usage) for up to date method names.
WARNING: This module was recently upgraded from [2.2.x](https://github.com/sendgrid/sendgrid-php/tree/v2.2.1) to 3.X. There were API breaking changes for various method names. See [usage](https://github.com/sendgrid/sendgrid-php#usage) for up to date method names.

## PLEASE READ THIS

One of the most notable changes is how `addTo()` behaves. We are now using our Web API parameters instead of the X-SMTPAPI header. What this means is that if you call `addTo()` multiple times for an email, **ONE** email will be sent with each email address visible to everyone. To utilize the original behavior of having and individual email sent to each recipient, this includes using substitutions, you must now use `addSmtpapiTo()`.

Smtpapi addressing methods cannot be mixed with non Smtpapi addressing methods.

Please read [this](#) for further information on the difference. // TODO: Create something.

Also, the `send()` method now raises a `SendGrid\Exception` if the response code is not 200.

---

Important: This library requires PHP 5.3 or higher.

[![BuildStatus](https://travis-ci.org/sendgrid/sendgrid-php.png?branch=master)](https://travis-ci.org/sendgrid/sendgrid-php)
[![Latest Stable Version](https://poser.pugx.org/sendgrid/sendgrid/version.png)](https://packagist.org/packages/sendgrid/sendgrid)
[![BuildStatus](https://travis-ci.org/sendgrid/sendgrid-php.svg?branch=master)](https://travis-ci.org/sendgrid/sendgrid-php)
[![Latest Stable Version](https://poser.pugx.org/sendgrid/sendgrid/version.svg)](https://packagist.org/packages/sendgrid/sendgrid)

```php
$sendgrid = new SendGrid('username', 'password');
Expand All @@ -30,7 +42,7 @@ Add SendGrid to your `composer.json` file. If you are not using [Composer](http:
```json
{
"require": {
"sendgrid/sendgrid": "2.2.1"
"sendgrid/sendgrid": "3.0.0"
}
}
```
Expand Down Expand Up @@ -89,7 +101,7 @@ $sendgrid->send($email);

#### addTo

You can add one or multiple TO addresses using `addTo`.
You can add one or multiple TO addresses using `addTo` along with an optional TO name. Note: If using TO names, each address needs a name.

```php
$email = new SendGrid\Email();
Expand All @@ -98,6 +110,27 @@ $email
->addTo('another@another.com')
;
$sendgrid->send($email);

// With names
$email = new SendGrid\Email();
$email
->addTo('foo@bar.com', 'Frank Foo')
->addTo('another@another.com', 'Joe Bar')
;
$sendgrid->send($email);
```

#### addSmtpapiTo

Add a TO address to the smtpapi header.

```php
$email = new SendGrid\Email();
$email
->addSmtpapiTo('foo@bar.com')
->addSmtpapiTo('another@another.com')
;
$sendgrid->send($email);
```

#### setTos
Expand Down Expand Up @@ -415,7 +448,7 @@ $email
->addTo('harry@somewhere.com')
->addTo('Bob@somewhere.com')
...
->setHtml('Hey %name%, we\'ve seen that you\'ve been gone for a while')
->setHtml("Hey %name%, we've seen that you've been gone for a while")
->addSubstitution('%name%', array('John', 'Harry', 'Bob'))
;
```
Expand All @@ -425,7 +458,7 @@ Substitutions can also be used to customize multi-recipient subjects.
```php
$email = new SendGrid\Email();
$email
->addTos(array('john@somewhere.com', 'harry@somewhere.com', 'bob@somewhere.com'))
->addTo(array('john@somewhere.com', 'harry@somewhere.com', 'bob@somewhere.com'))
->setSubject('%subject%')
->addSubstitution(
'%subject%',
Expand All @@ -440,7 +473,7 @@ $email
```php
$email = new SendGrid\Email();
$email
->addTos(array('john@somewhere.com', 'harry@somewhere.com', 'bob@somewhere.com'))
->addTo(array('john@somewhere.com', 'harry@somewhere.com', 'bob@somewhere.com'))
->setSubject('%subject%')
->setSubstitutions(array(
'%name%' => array('John', 'Harry', 'Bob'),
Expand Down
48 changes: 25 additions & 23 deletions composer.json
@@ -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/"}
}
}
178 changes: 96 additions & 82 deletions lib/SendGrid.php
@@ -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');
}
}
}
}

0 comments on commit f8a9fca

Please sign in to comment.