Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
[![BuildStatus](https://travis-ci.org/sendgrid/sendgrid-php.svg?branch=master)](https://travis-ci.org/sendgrid/sendgrid-php)
2
 
3
Please see our announcement regarding [breaking changes](https://github.com/sendgrid/sendgrid-php/issues/290). Your support is appreciated!
4
 
5
**This library allows you to quickly and easily use the SendGrid Web API v3 via PHP.**
6
 
7
Version 5.X.X of this library provides full support for all SendGrid [Web API v3](https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html) endpoints, including the new [v3 /mail/send](https://sendgrid.com/blog/introducing-v3mailsend-sendgrids-new-mail-endpoint).
8
 
9
This library represents the beginning of a new path for SendGrid. We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create [issues](https://github.com/sendgrid/sendgrid-php/issues) and [pull requests](https://github.com/sendgrid/sendgrid-php/blob/master/CONTRIBUTING.md) or simply upvote or comment on existing issues or pull requests.
10
 
11
Please browse the rest of this README for further detail.
12
 
13
We appreciate your continued support, thank you!
14
 
15
# Table of Contents
16
 
17
* [Installation](#installation)
18
* [Quick Start](#quick_start)
19
* [Usage](#usage)
20
* [Use Cases](#use_cases)
21
* [Announcements](#announcements)
22
* [Roadmap](#roadmap)
23
* [How to Contribute](#contribute)
24
* [Troubleshooting](#troubleshooting)
25
* [About](#about)
26
 
27
<a name="installation"></a>
28
# Installation
29
 
30
## Prerequisites
31
 
32
- PHP version 5.6 or 7.0
33
- The SendGrid service, starting at the [free level](https://sendgrid.com/free?source=sendgrid-php)
34
 
35
## Setup Environment Variables
36
 
37
Update the development environment with your [SENDGRID_API_KEY](https://app.sendgrid.com/settings/api_keys), for example:
38
 
39
```bash
40
echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
41
echo "sendgrid.env" >> .gitignore
42
source ./sendgrid.env
43
```
44
 
45
## Install Package
46
 
47
Add SendGrid to your `composer.json` file. If you are not using [Composer](http://getcomposer.org), you should be. It's an excellent way to manage dependencies in your PHP application.
48
 
49
```json
50
{
51
  "require": {
52
    "sendgrid/sendgrid": "~5.4"
53
  }
54
}
55
```
56
 
57
Then at the top of your PHP script require the autoloader:
58
 
59
```bash
60
require 'vendor/autoload.php';
61
```
62
 
63
#### Alternative: Install package from zip
64
 
65
If you are not using Composer, simply download and install the **[latest packaged release of the library as a zip](https://sendgrid-open-source.s3.amazonaws.com/sendgrid-php/sendgrid-php.zip)**.
66
 
67
[**⬇︎ Download Packaged Library ⬇︎**](https://sendgrid-open-source.s3.amazonaws.com/sendgrid-php/sendgrid-php.zip)
68
 
69
Then require the library from package:
70
 
71
```php
72
require("path/to/sendgrid-php/sendgrid-php.php");
73
```
74
 
75
Previous versions of the library can be found in the [version index](https://sendgrid-open-source.s3.amazonaws.com/index.html).
76
 
77
## Dependencies
78
 
79
- The SendGrid Service, starting at the [free level](https://sendgrid.com/free?source=sendgrid-php)
80
- [php-HTTP-Client](https://github.com/sendgrid/php-http-client)
81
 
82
<a name="quick_start"></a>
83
# Quick Start
84
 
85
## Hello Email
86
 
87
The following is the minimum needed code to send an email with the [/mail/send Helper](https://github.com/sendgrid/sendgrid-php/tree/master/lib/helpers/mail) ([here](https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L22) is a full example):
88
 
89
```php
90
<?php
91
// If you are using Composer (recommended)
92
require 'vendor/autoload.php';
93
 
94
// If you are not using Composer
95
// require("path/to/sendgrid-php/sendgrid-php.php");
96
 
97
$from = new SendGrid\Email(null, "test@example.com");
98
$subject = "Hello World from the SendGrid PHP Library!";
99
$to = new SendGrid\Email(null, "test@example.com");
100
$content = new SendGrid\Content("text/plain", "Hello, Email!");
101
$mail = new SendGrid\Mail($from, $subject, $to, $content);
102
 
103
$apiKey = getenv('SENDGRID_API_KEY');
104
$sg = new \SendGrid($apiKey);
105
 
106
$response = $sg->client->mail()->send()->post($mail);
107
echo $response->statusCode();
108
echo $response->headers();
109
echo $response->body();
110
```
111
 
112
The `SendGrid\Mail` constructor creates a [personalization object](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html) for you. [Here](https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L16) is an example of how to add to it.
113
 
114
### Without Mail Helper Class
115
 
116
The following is the minimum needed code to send an email without the /mail/send Helper ([here](https://github.com/sendgrid/sendgrid-php/blob/master/examples/mail/mail.php#L28) is a full example):
117
 
118
```php
119
<?php
120
// If you are using Composer (recommended)
121
require 'vendor/autoload.php';
122
 
123
// If you are not using Composer
124
// require("path/to/sendgrid-php/sendgrid-php.php");
125
 
126
$request_body = json_decode('{
127
  "personalizations": [
128
    {
129
      "to": [
130
        {
131
          "email": "test@example.com"
132
        }
133
      ],
134
      "subject": "Hello World from the SendGrid PHP Library!"
135
    }
136
  ],
137
  "from": {
138
    "email": "test@example.com"
139
  },
140
  "content": [
141
    {
142
      "type": "text/plain",
143
      "value": "Hello, Email!"
144
    }
145
  ]
146
}');
147
 
148
$apiKey = getenv('SENDGRID_API_KEY');
149
$sg = new \SendGrid($apiKey);
150
 
151
$response = $sg->client->mail()->send()->post($request_body);
152
echo $response->statusCode();
153
echo $response->body();
154
echo $response->headers();
155
```
156
 
157
## General v3 Web API Usage (With Fluent Interface)
158
 
159
```php
160
<?php
161
// If you are using Composer (recommended)
162
require 'vendor/autoload.php';
163
 
164
// If you are not using Composer
165
// require("path/to/sendgrid-php/sendgrid-php.php");
166
 
167
$apiKey = getenv('SENDGRID_API_KEY');
168
$sg = new \SendGrid($apiKey);
169
 
170
$response = $sg->client->suppressions()->bounces()->get();
171
 
172
print $response->statusCode();
173
print $response->headers();
174
print $response->body();
175
```
176
 
177
## General v3 Web API Usage (Without Fluent Interface)
178
 
179
```php
180
<?php
181
// If you are using Composer (recommended)
182
require 'vendor/autoload.php';
183
 
184
// If you are not using Composer
185
// require("path/to/sendgrid-php/sendgrid-php.php");
186
 
187
$apiKey = getenv('SENDGRID_API_KEY');
188
$sg = new \SendGrid($apiKey);
189
 
190
$response = $sg->client->_("suppression/bounces")->get();
191
 
192
print $response->statusCode();
193
print $response->headers();
194
print $response->body();
195
```
196
 
197
<a name="usage"></a>
198
# Usage
199
 
200
- [SendGrid Docs](https://sendgrid.com/docs/API_Reference/index.html)
201
- [Library Usage
202
    Documentation](https://github.com/sendgrid/sendgrid-php/tree/master/USAGE.md)
203
- [Example Code](https://github.com/sendgrid/sendgrid-php/tree/master/examples)
204
- [How-to: Migration from v2 to v3](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/how_to_migrate_from_v2_to_v3_mail_send.html)
205
- [v3 Web API Mail Send Helper](https://github.com/sendgrid/sendgrid-php/tree/master/lib/helpers/mail/README.md) - build a request object payload for a v3 /mail/send API call.
206
 
207
<a name="use_cases">
208
# Use Cases
209
 
210
[Examples of common API use cases](https://github.com/sendgrid/sendgrid-php/blob/master/USE_CASES.md), such as how to send an email with a transactional template.
211
 
212
<a name="announcements"></a>
213
# Announcements
214
 
215
Please see our announcement regarding [breaking changes](https://github.com/sendgrid/sendgrid-php/issues/290). Your support is appreciated!
216
 
217
All updates to this library is documented in our [CHANGELOG](https://github.com/sendgrid/sendgrid-php/blob/master/CHANGELOG.md) and [releases](https://github.com/sendgrid/sendgrid-php/releases)
218
 
219
<a name="roadmap"></a>
220
# Roadmap
221
 
222
If you are interested in the future direction of this project, please take a look at our open [issues](https://github.com/sendgrid/sendgrid-php/issues) and [pull requests](https://github.com/sendgrid/sendgrid-php/pulls). We would love to hear your feedback.
223
 
224
<a name="contribute"></a>
225
# How to Contribute
226
 
227
We encourage contribution to our libraries (you might even score some nifty swag), please see our [CONTRIBUTING](https://github.com/sendgrid/sendgrid-php/blob/master/CONTRIBUTING.md) guide for details.
228
 
229
Quick links:
230
 
231
- [Feature Request](https://github.com/sendgrid/sendgrid-php/blob/master/CONTRIBUTING.md#feature_request)
232
- [Bug Reports](https://github.com/sendgrid/sendgrid-php/blob/master/CONTRIBUTING.md#submit_a_bug_report)
233
- [Sign the CLA to Create a Pull Request](https://github.com/sendgrid/sendgrid-php/blob/master/CONTRIBUTING.md#cla)
234
- [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-php/blob/master/CONTRIBUTING.md#improvements_to_the_codebase)
235
 
236
<a name="troubleshooting"></a>
237
# Troubleshooting
238
 
239
Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-php/blob/master/TROUBLESHOOTING.md) for common library issues.
240
 
241
<a name="about"></a>
242
# About
243
 
244
sendgrid-php is guided and supported by the SendGrid [Developer Experience Team](mailto:dx@sendgrid.com).
245
 
246
sendgrid-php is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-php are trademarks of SendGrid, Inc.
247
 
248
![SendGrid Logo](https://uiux.s3.amazonaws.com/2016-logos/email-logo%402x.png)
249