keneasson/php-sparkpost
Composer 安装命令:
composer require keneasson/php-sparkpost
包简介
Client library for interfacing with the SparkPost API with IP Pools.
README 文档
README
Sign up for a SparkPost account and visit our Developer Hub for even more content.
SparkPost PHP Library
The official PHP library for using the SparkPost REST API.
Note: We understand that the ivory-http-adapter we use in this library is deprecated in favor of httplug. We use Ivory internally to make it simple for you to use whatever HTTP library you want. The deprecation won't affect or limit our ongoing support of this PHP library.
Before using this library, you must have a valid API Key. To get an API Key, please log in to your SparkPost account and generate one in the Settings page.
Installation
The recommended way to install the SparkPost PHP Library is through composer.
# Install Composer
curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the SparkPost PHP Library:
composer require sparkpost/php-sparkpost
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php'; use SparkPost\SparkPost;
Setting up a Request Adapter
Because of dependency collision, we have opted to use a request adapter rather than requiring a request library. This means that your application will need to pass in a request adapter to the constructor of the SparkPost Library. We use the [Ivory HTTP Adapter] (https://github.com/egeloen/ivory-http-adapter) in SparkPost. Please visit their repo for a list of supported adapters. If you don't currently use a request library, you will need to require one and create an adapter from it and pass it along. The example below uses the GuzzleHttp Client Library.
An Adapter can be setup like so:
use SparkPost\SparkPost; use GuzzleHttp\Client; use Ivory\HttpAdapter\Guzzle6HttpAdapter; $httpAdapter = new Guzzle6HttpAdapter(new Client()); $sparky = new SparkPost($httpAdapter, ['key'=>'YOUR API KEY']);
Getting Started: Your First Mailing
For this example to work as is, Guzzle 6 will need to be installed. Otherwise another adapter can be used for your specific setup. See "Setting up a Request Adapter" above.
require 'vendor/autoload.php'; use SparkPost\SparkPost; use GuzzleHttp\Client; use Ivory\HttpAdapter\Guzzle6HttpAdapter; $httpAdapter = new Guzzle6HttpAdapter(new Client()); $sparky = new SparkPost($httpAdapter, ['key'=>'YOUR API KEY']); try { // Build your email and send it! $results = $sparky->transmission->send([ 'from'=>[ 'name' => 'From Envelope', 'email' => 'from@sparkpostbox.com>' ], 'html'=>'<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>', 'text'=>'Congratulations, {{name}}!! You just sent your very first mailing!', 'substitutionData'=>['name'=>'YOUR FIRST NAME'], 'subject'=>'First Mailing From PHP', 'recipients'=>[ [ 'address'=>[ 'name'=>'YOUR FULL NAME', 'email'=>'YOUR EMAIL ADDRESS' ] ] ] ]); echo 'Woohoo! You just sent your first mailing!'; } catch (\Exception $err) { echo 'Whoops! Something went wrong'; var_dump($err); }
Learn More
- For more detailed examples, check our examples:
- Read our REST API documentation - http://www.sparkpost.com/docs/introduction
Field Descriptions
Transmissions
| Field Name | Required? | Description | Data Type |
|---|---|---|---|
| attachments | no | Field for attaching files - see Attachment Attributes in the Transmssions API docs | Array of Objects |
| campaign | no | Field for assigning a given transmission to a specific campaign, which is a logical container for similar transmissions | String |
| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) |
| description | no | Field for describing what this transmission is for the user | String |
| from | yes** | Field for setting the from line of a given transmission | Object |
| html | yes** | Field for setting the HTML content of a given transmission | String |
| inlineCss | no | Field for enabling/disabling CSS inlining | Boolean |
| inlineImages | no | Field for providing inline images - see Inline Image Attributes in the Transmssions API docs | Array of Objects |
| metadata | no | Field for adding arbitrary key/value pairs which will be included in open/click tracking | Object (Simple) |
| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String |
| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects |
| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String |
| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String |
| subject | yes | Field for setting the subject line of a given transmission | String |
| substitutionData | no | Field for adding transmission level substitution data, which can be used in a variety of fields and in content | Object (Complex) |
| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String |
| text | yes** | Field for setting the Plain Text content of a given transmission | String |
| trackClicks | no | Field for enabling/disabling transmission level click tracking (default: true) | Boolean |
| trackOpens | no | Field for enabling/disabling transmission level open tracking (default: true) | Boolean |
| transactional | no | Field for marking email as transactional (default: false) | Boolean |
| useDraftTemplate | no | Field for allowing the sending of a transmission using a draft of a stored template (default: false) | Boolean |
** - If using inline content then html or text are required. If using RFC-822 Inline Content, then rfc822 is required. If using a stored recipient list, then recipientList is required. If using a stored template, then template is required but from is not as the values from the template will be used.
Tips and Tricks
General
- You must provide at least an API key when instantiating the SparkPost Library -
[ 'key'=>'184ac5480cfdd2bb2859e4476d2e5b1d2bad079bf' ] - The library's features are namespaced under the various SparkPost API names.
Transmissions
- If you specify a stored recipient list and inline recipients in a Transmission, you will receive an error.
- If you specify HTML and/or Plain Text content and then provide RFC-822 encoded content, you will receive an error.
- RFC-822 content is not valid with any other content type.
- If you specify a stored template and also provide inline content via
htmlortext, you will receive an error. - By default, open and click tracking are enabled for a transmission.
- By default, a transmission will use the published version of a stored template.
Contributing
See contributing.
keneasson/php-sparkpost 适用场景与选型建议
keneasson/php-sparkpost 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 keneasson/php-sparkpost 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 keneasson/php-sparkpost 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache
- 更新时间: 2016-09-23