forked from mailgun/mailgun-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
188 lines (148 loc) · 11.2 KB
/
index.html
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>mailgun-php by mailgun</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<script src="javascripts/scale.fix.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header>
<h1 class="header">mailgun-php</h1>
<p class="header">Official Mailgun PHP SDK</p>
<ul>
<li class="download"><a class="buttons" href="https://github.com/mailgun/mailgun-php/zipball/master">Download ZIP</a></li>
<li class="download"><a class="buttons" href="https://github.com/mailgun/mailgun-php/tarball/master">Download TAR</a></li>
<li><a class="buttons github" href="https://github.com/mailgun/mailgun-php">View On GitHub</a></li>
</ul>
<p class="header">This project is maintained by <a class="header name" href="https://github.com/mailgun">mailgun</a></p>
</header>
<section>
<h1>
<a name="mailgun-php" class="anchor" href="#mailgun-php"><span class="octicon octicon-link"></span></a>Mailgun-PHP</h1>
<p>This is the Mailgun PHP SDK. This SDK contains methods for easily interacting
with the Mailgun API.
Below are examples to get you started. For additional examples, please see our
official documentation
at <a href="http://documentation.mailgun.com">http://documentation.mailgun.com</a></p>
<p><a href="https://packagist.org/packages/mailgun/mailgun-php"><img src="https://poser.pugx.org/mailgun/mailgun-php/v/stable.png" alt="Latest Stable Version"></a>
<a href="https://travis-ci.org/mailgun/mailgun-php"><img src="https://travis-ci.org/mailgun/mailgun-php.png" alt="Build Status"></a></p>
<h2>
<a name="installation" class="anchor" href="#installation"><span class="octicon octicon-link"></span></a>Installation</h2>
<p>To install the SDK, you will need to be using <a href="http://getcomposer.org/">Composer</a>
in your project.
If you aren't using Composer yet, it's really simple! Here's how to install
composer and the Mailgun SDK.</p>
<div class="highlight highlight-PHP"><pre><span class="c1"># Install Composer</span>
<span class="nx">curl</span> <span class="o">-</span><span class="nx">sS</span> <span class="nx">https</span><span class="o">://</span><span class="nx">getcomposer</span><span class="o">.</span><span class="nx">org</span><span class="o">/</span><span class="nx">installer</span> <span class="o">|</span> <span class="nx">php</span>
<span class="c1"># Add Mailgun as a dependency</span>
<span class="nx">php</span> <span class="nx">composer</span><span class="o">.</span><span class="nx">phar</span> <span class="k">require</span> <span class="nx">mailgun</span><span class="o">/</span><span class="nx">mailgun</span><span class="o">-</span><span class="nx">php</span><span class="o">:~</span><span class="mf">1.2</span>
</pre></div>
<p>For shared hosts with SSH access, you might need to run this instead (contact
your shared host for assistance): </p>
<pre><code>php -d detect_unicode=Off -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
</code></pre>
<p>Next, require Composer's autoloader, in your application, to automatically
load the Mailgun SDK in your project:</p>
<div class="highlight highlight-PHP"><pre><span class="k">require</span> <span class="s1">'vendor/autoload.php'</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Mailgun\Mailgun</span><span class="p">;</span>
</pre></div>
<h2>
<a name="usage" class="anchor" href="#usage"><span class="octicon octicon-link"></span></a>Usage</h2>
<p>Here's how to send a message using the SDK:</p>
<div class="highlight highlight-php"><pre><span class="c1"># First, instantiate the SDK with your API credentials and define your domain. </span>
<span class="nv">$mg</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Mailgun</span><span class="p">(</span><span class="s2">"key-example"</span><span class="p">);</span>
<span class="nv">$domain</span> <span class="o">=</span> <span class="s2">"example.com"</span><span class="p">;</span>
<span class="c1"># Now, compose and send your message.</span>
<span class="nv">$mg</span><span class="o">-></span><span class="na">sendMessage</span><span class="p">(</span><span class="nv">$domain</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'from'</span> <span class="o">=></span> <span class="s1">'bob@example.com'</span><span class="p">,</span>
<span class="s1">'to'</span> <span class="o">=></span> <span class="s1">'sally@example.com'</span><span class="p">,</span>
<span class="s1">'subject'</span> <span class="o">=></span> <span class="s1">'The PHP SDK is awesome!'</span><span class="p">,</span>
<span class="s1">'text'</span> <span class="o">=></span> <span class="s1">'It is so simple to send a message.'</span><span class="p">));</span>
</pre></div>
<p>Or obtain the last 25 log items: </p>
<div class="highlight highlight-php"><pre><span class="c1"># First, instantiate the SDK with your API credentials and define your domain. </span>
<span class="nv">$mg</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Mailgun</span><span class="p">(</span><span class="s2">"key-example"</span><span class="p">);</span>
<span class="nv">$domain</span> <span class="o">=</span> <span class="s2">"example.com"</span><span class="p">;</span>
<span class="c1"># Now, issue a GET against the Logs endpoint.</span>
<span class="nv">$mg</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s2">"</span><span class="si">$domain</span><span class="s2">/log"</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'limit'</span> <span class="o">=></span> <span class="mi">25</span><span class="p">,</span>
<span class="s1">'skip'</span> <span class="o">=></span> <span class="mi">0</span><span class="p">));</span>
</pre></div>
<h2>
<a name="response" class="anchor" href="#response"><span class="octicon octicon-link"></span></a>Response</h2>
<p>The results, provided by the endpoint, are returned as an object, which you
can traverse like an array. </p>
<p>Example: </p>
<div class="highlight highlight-php"><pre><span class="nv">$mg</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Mailgun</span><span class="p">(</span><span class="s2">"key-example"</span><span class="p">);</span>
<span class="nv">$domain</span> <span class="o">=</span> <span class="s2">"example.com"</span><span class="p">;</span>
<span class="nv">$result</span> <span class="o">=</span> <span class="nv">$mg</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s2">"</span><span class="si">$domain</span><span class="s2">/log"</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'limit'</span> <span class="o">=></span> <span class="mi">25</span><span class="p">,</span>
<span class="s1">'skip'</span> <span class="o">=></span> <span class="mi">0</span><span class="p">));</span>
<span class="nv">$httpResponseCode</span> <span class="o">=</span> <span class="nv">$result</span><span class="o">-></span><span class="na">http_response_code</span><span class="p">;</span>
<span class="nv">$httpResponseBody</span> <span class="o">=</span> <span class="nv">$result</span><span class="o">-></span><span class="na">http_response_body</span><span class="p">;</span>
<span class="c1"># Iterate through the results and echo the message IDs.</span>
<span class="nv">$logItems</span> <span class="o">=</span> <span class="nv">$result</span><span class="o">-></span><span class="na">http_response_body</span><span class="o">-></span><span class="na">items</span><span class="p">;</span>
<span class="k">foreach</span><span class="p">(</span><span class="nv">$logItems</span> <span class="k">as</span> <span class="nv">$logItem</span><span class="p">){</span>
<span class="k">echo</span> <span class="nv">$logItem</span><span class="o">-></span><span class="na">message_id</span> <span class="o">.</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>Example Contents:<br><strong>$httpResponseCode</strong> will contain an integer. You can find how we use HTTP response
codes in our documentation:
<a href="http://documentation.mailgun.com/api-intro.html?highlight=401#errors">http://documentation.mailgun.com/api-intro.html?highlight=401#errors</a></p>
<p><strong>$httpResponseBody</strong> will contain an object of the API response. In the above
example, a var_dump($result) would contain the following: </p>
<pre><code>object(stdClass)#26 (2) {
["http_response_body"]=>
object(stdClass)#26 (2) {
["total_count"]=>
int(12)
["items"]=>
array(1) {
[0]=>
object(stdClass)#31 (5) {
["hap"]=>
string(9) "delivered"
["created_at"]=>
string(29) "Tue, 20 Aug 2013 20:24:34 GMT"
["message"]=>
string(66) "Delivered: me@samples.mailgun.org → travis@mailgunhq.com 'Hello'"
["type"]=>
string(4) "info"
["message_id"]=>
string(46) "20130820202406.24739.21973@samples.mailgun.org"
}
}
}
}
</code></pre>
<p>For usage examples on each API endpoint, head over to our official documentation
pages. </p>
<p>This SDK includes a <a href="src/Mailgun/Messages/README.md">Message Builder</a>,
<a href="src/Mailgun/Messages/README.md">Batch Message</a> and <a href="src/Mailgun/Lists/README.md">Opt-In Handler</a> component.</p>
<p>Message Builder allows you to quickly create the array of parameters, required
to send a message, by calling a methods for each parameter.
Batch Message is an extension of Message Builder, and allows you to easily send
a batch message job within a few seconds. The complexity of
batch messaging is eliminated! </p>
<h2>
<a name="support-and-feedback" class="anchor" href="#support-and-feedback"><span class="octicon octicon-link"></span></a>Support and Feedback</h2>
<p>Be sure to visit the Mailgun official
<a href="http://documentation.mailgun.com/">documentation website</a> for additional
information about our API. </p>
<p>If you find a bug, please submit the issue in Github directly.
<a href="https://github.com/mailgun/Mailgun-PHP/issues">Mailgun-PHP Issues</a></p>
<p>As always, if you need additional assistance, drop us a note at
<a href="mailto:support@mailgun.com">support@mailgun.com</a>.</p>
</section>
<footer>
<p><small>Hosted on <a href="https://pages.github.com">GitHub Pages</a> using the Dinky theme</small></p>
</footer>
</div>
<!--[if !IE]><script>fixScale(document);</script><![endif]-->
</body>
</html>