Skip to content
Merged
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
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

; Works with some editors only
quote_type = single
max_line_length = 120
spaces_around_brackets = true
spaces_around_operators = true
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v0.3.0 (2015-03-26)
===================

* Add IP Pool support
* Normalize project file using PHP-FIG convention

--

v0.2.0 (2014-01-23)
===================

Expand Down
99 changes: 54 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# smtpapi-php
# SMTPAPI-PHP

This php library allows you to quickly and more easily generate SendGrid X-SMTPAPI headers.

Expand All @@ -12,11 +12,11 @@ The following recommended installation requires [http://getcomposer.org](compose
Add the following to your `composer.json` file.

```json
{
"minimum-stability" : "dev",
"require": {
"sendgrid/smtpapi": "0.2.0"
}
{
"minimum-stability" : "dev",
"require": {
"sendgrid/smtpapi": "0.3.0"
}
}
```

Expand All @@ -33,7 +33,7 @@ If you are not using Composer, simply download and install the **[latest package
Then require the library from package:

```php
require("path/to/smtpapi-php/smtpapi-php.php");
require('path/to/smtpapi-php/smtpapi-php.php');
```

Previous versions of the library can be found in the [version index](https://sendgrid-open-source.s3.amazonaws.com/index.html).
Expand All @@ -43,58 +43,58 @@ Previous versions of the library can be found in the [version index](https://sen
### Initializing

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
```

### jsonString

This gives you back the stringified json formatted X-SMTPAPI header. Use this with your [smtp client](https://github.com/andris9/simplesmtp) of choice.

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->jsonString();
```

If you don't want to unicode escape, you can set options parameter for jsonString() (PHP 5.4 or later).

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->jsonString(JSON_UNESCAPED_UNICODE);
```

### addTo

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addTo('[email protected]');
$header->addTo('[email protected]');
```

### setTos

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setTos(array('[email protected]', '[email protected]'));
```

### setSendAt

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setSendAt(1409348513);
```

### setSendEachAt

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setSendEachAt(array(1409348513, 1409348514, 1409348515));
```

### addSendEachAt

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addSendEachAt(1409348513);
$header->addSendEachAt(1409348514);
$header->addSendEachAt(1409348515);
Expand All @@ -103,86 +103,93 @@ $header->addSendEachAt(1409348515);
### addSubstitution

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addSubstitution('keep', array('secret')); // sub = {keep: ['secret']}
$header->addSubstitution('other', array('one', 'two')); // sub = {keep: ['secret'], other: ['one', 'two']}
```

### setSubstitutions

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setSubstitutions(array('keep' => array('secret'))); // sub = {keep: ['secret']}
```
### addUniqueArg

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addUniqueArg('cat', 'dogs');
```

### setUniqueArgs

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setUniqueArgs(array('cow' => 'chicken'));
$header->setUniqueArgs(array('dad' => 'proud'));
```

### addCategory

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addCategory('tactics'); // category = ['tactics']
$header->addCategory('advanced'); // category = ['tactics', 'advanced']
```

### setCategories

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setCategories(array('tactics', 'advanced')); // category = ['tactics', 'advanced']
```

### addSection

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addSection('-charge-': 'This ship is useless.');
$header->addSection('-bomber-', 'Only for sad vikings.');
```

### setSections

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setSections(array('-charge-' => 'This ship is useless.'));
```

### setASMGroupID

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->setASMGroupID(42);
```

## setIpPool

```php
$header = new Smtpapi\Header();
$header->setIpPool('pool_name');
```

### addFilter

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$header->addFilter('footer', 'enable', 1);
$header->addFilter('footer', 'text/html', '<strong>boo</strong>');
```

### setFilters

```php
$header = new Smtpapi\Header();
$header = new Smtpapi\Header();
$filter = array(
'footer' => array(
'setting' => array(
'enable' => 1,
"text/plain" => 'You can haz footers!'
'text/plain' => 'You can haz footers!'
)
)
);
Expand All @@ -194,29 +201,31 @@ $header->setFilters($filter);
The following example builds the X-SMTPAPI headers and adds them to swiftmailer. [Swiftmailer](http://swiftmailer.org/) then sends the email through SendGrid. You can use this same code in your application or optionally you can use [sendgrid-php](http://github.com/sendgrid/sendgrid-php).

```php
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
$transport->setUsername("sendgrid_username");
$transport->setPassword("sendgrid_password");
use Smtpapi\Header;

$transport = \Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
$transport->setUsername('sendgrid_username');
$transport->setPassword('sendgrid_password');

$mailer = Swift_Mailer::newInstance($transport);
$mailer = \Swift_Mailer::newInstance($transport);

$message = new Swift_Message();
$message->setTos(array("[email protected]"));
$message->setFrom("[email protected]");
$message->setSubject("Hello");
$message->setBody("%how% are you doing?");
$message = new \Swift_Message();
$message->setTos(array('[email protected]'));
$message->setFrom('[email protected]');
$message->setSubject('Hello');
$message->setBody('%how% are you doing?');

$header = new Smtpapi\Header();
$header->addSubstitution("%how%", array("Owl"));
$header = new Header();
$header->addSubstitution('%how%', array('Owl'));

$message_headers = $message->getHeaders();
$message_headers->addTextHeader("x-smtpapi", $header->jsonString());
$message_headers = $message->getHeaders();
$message_headers->addTextHeader(HEADER::NAME, $header->jsonString());

try {
$response = $mailer->send($message);
print_r($response);
$response = $mailer->send($message);
print_r($response);
} catch(\Swift_TransportException $e) {
print_r('Bad username / password');
print_r('Bad username / password');
}
```

Expand All @@ -232,8 +241,8 @@ try {

The existing tests in the `test` directory can be run using [PHPUnit](https://github.com/sebastianbergmann/phpunit/) with the following command:

````bash
composer update --dev
```bash
composer install
cd test
../vendor/bin/phpunit
```
Expand Down
39 changes: 19 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"name": "sendgrid/smtpapi",
"description": "Build SendGrid X-SMTPAPI headers in PHP.",
"version": "0.2.0",
"homepage": "http://github.com/sendgrid/smtpapi-php",
"license": "MIT",
"keywords": ["SendGrid", "sendgrid", "email", "send", "grid", "smtpapi", "smtp", "api", "xsmtp", "X-SMTP"],
"require": {
"php": ">=5.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"replace": {
"sendgrid/smtpapi-php": "*"
},
"type": "library",
"autoload": {
"psr-0": {"Smtpapi": "lib/"}
}
"name": "sendgrid/smtpapi",
"description": "Build SendGrid X-SMTPAPI headers in PHP.",
"version": "0.3.0",
"homepage": "http://github.com/sendgrid/smtpapi-php",
"license": "MIT",
"keywords": ["SendGrid", "sendgrid", "email", "send", "grid", "smtpapi", "smtp", "api", "xsmtp", "X-SMTP"],
"require": {
"php": ">=5.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"replace": {
"sendgrid/smtpapi-php": "*"
},
"type": "library",
"autoload": {
"psr-0": {"Smtpapi": "lib/"}
}
}

29 changes: 16 additions & 13 deletions lib/Smtpapi.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?php

class Smtpapi {
const VERSION = '0.2.0';
class Smtpapi
{
const VERSION = '0.3.0';

public static function register_autoloader() {
spl_autoload_register(array('Smtpapi', 'autoloader'));
}
public static function register_autoloader()
{
spl_autoload_register(array('Smtpapi', 'autoloader'));
}

public static function autoloader($class) {
// Check that the class starts with 'Smtpapi'
if ($class == 'Smtpapi' || stripos($class, 'Smtpapi\\') === 0) {
$file = str_replace('\\', '/', $class);
public static function autoloader($class)
{
// Check that the class starts with 'Smtpapi'
if ($class == 'Smtpapi' || stripos($class, 'Smtpapi\\') === 0) {
$file = str_replace('\\', '/', $class);

if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
require_once(dirname(__FILE__) . '/' . $file . '.php');
}
if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
require_once(dirname(__FILE__) . '/' . $file . '.php');
}
}
}
}
}
Loading