The following is a sample of a header that calls the SMTP API. Further down on this page you can find a complete example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

# Version 1.0
# Last Updated 6/22/2009

class SmtpApiHeader
{
    var $data;

    function addTo($tos)
    {
        if (!isset($this->data['to'])) {
            $this->data['to'] = array();
        }
        $this->data['to'] = array_merge($this->data['to'], (array) $tos);
    }

    function addSubVal($var, $val)
    {
        if (!isset($this->data['sub'])) {
            $this->data['sub'] = array();
        }

        if (!isset($this->data['sub'][$var])) {
            $this->data['sub'][$var] = array();
        }
        $this->data['sub'][$var] = array_merge($this->data['sub'][$var], (array) $val);
    }

    function setUniqueArgs($val)
    {
        if (!is_array($val))
            return;
        // checking for associative array
        $diff = array_diff_assoc($val, array_values($val));
        if (((empty($diff)) ? false : true)) {
            $this->data['unique_args'] = $val;
        }
    }

    function setCategory($cat)
    {
        $this->data['category'] = $cat;
    }

    function addFilterSetting($filter, $setting, $value)
    {
        if (!isset($this->data['filters'])) {
            $this->data['filters'] = array();
        }

        if (!isset($this->data['filters'][$filter])) {
            $this->data['filters'][$filter] = array();
        }

        if (!isset($this->data['filters'][$filter]['settings'])) {
            $this->data['filters'][$filter]['settings'] = array();
        }
        $this->data['filters'][$filter]['settings'][$setting] = $value;
    }

    function asJSON()
    {
        $json = json_encode($this->data);
        // Add spaces so that the field can be folded
        $json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);
        return $json;
    }

    function as_string()
    {
        $json = $this->asJSON();
        $str  = "X-SMTPAPI: " . wordwrap($json, 76, "\n ");
        return $str;
    }

}

?>

Example PHP Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

include 'SmtpApiHeader.php';

$hdr      = new SmtpApiHeader();
$receiver = array(
    'kyle@somewhere.com',
    'bob@someplace.net',
    'someguy@googlemailz.coms'
);
$times    = array(
    '1pm',
    '2pm',
    '3pm'
);
$names    = array(
    'kyle',
    'bob',
    'someguy'
);

$hdr->addFilterSetting('subscriptiontrack', 'enable', 1);
$hdr->addFilterSetting('twitter', 'enable', 1); #please check the apps available for your current package at www.sendgrid.com/pricing.html ?
$hdr->addTo($receiver);

$hdr->addSubVal('-time-', $times);

$hdr->addSubVal('-name-', $names);
$hdr->setUniqueArgs(array(
    'test' => 1,
    'foo' => 2
));

echo $hdr->as_string();
echo "\n";

?>