forked from sendgrid/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp.html
More file actions
135 lines (110 loc) · 2.92 KB
/
Copy pathphp.html
File metadata and controls
135 lines (110 loc) · 2.92 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
---
layout: page
weight: 0
title: PHP
navigation:
show: true
---
<p>The following is a sample of a header that calls the SMTP API. Further down on this page you can find a complete example.</p>
{% codeblock lang:php %}
<?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;
}
}
?>
{% endcodeblock %}
{% anchor h2 %}
Example PHP Usage
{% endanchor %}
{% codeblock lang:php %}
<?php
include 'SmtpApiHeader.php';
$hdr = new SmtpApiHeader();
$receiver = array(
);
$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";
?>
{% endcodeblock %}