messagex/email-php-sdk
Composer 安装命令:
composer require messagex/email-php-sdk
包简介
PHP SDK to integrate with MessageX Email service
README 文档
README
Introduction
Official SDK for MessageX Email API. It's goal is to provide easy integration with MessageX Email Service in PHP and build robust applications and software with those services. We want this SDK to be community driven and led by us. You can get started in minutes by installing SDK through composer or downloading a zip file.
Request Size
When sending attachments there are no explicit file size limitations. Requests bigger then 30MB will be reject with HTTP 417 status code.
Error Handling
All exceptions in SDK extend base MxException for easier handling of all exceptions rising from SDK. For more granular error handling email service has it's own base exception called EmailException.
Transport Errors
For sending requests MessageX PHP SDK is using Guzzle HTTP Client. When API returns with status code other then 2*, Guzzle will throw exception depending on the group of status code (Server side, Client side). Guzzle related exceptions are not handled in SDK.
Prerequisites
- PHP >= 5.6
- API and Secret Key from your MessageX account
Installation
Composer
Add following to the composer.json file
{
"require": {
"messagex/email-php-sdk": "~1.0"
}
}
Or from the CLI
composer require messagex/email-php-sdk
Quickstart
Send an Email
This is a minimal code sample needed in order to send a basic email using SDK.
require 'vendor/autoload.php'; use MessageX\Service\Email\ValueObject\Email; use MessageX\Service\Email\ValueObject\Body\Body; use MessageX\Service\Email\EmailClient; $emailClient = new EmailClient([ 'credentials' => ['key' => 'XXXXX', 'secret' => 'XXXXX'] ]); $emailClient->sendEmail( (new Email('John Doe <johndoe@example.com>', ['Jane Doe <janedoe@example.com>'])) ->setSubject('Greetings') ->addBody(new Body('text/plain', 'Hi Jane)) );
Name of the receiver or sender can be added using notation showed in code sample above. Adding a name is not required, adding just email address is also supported. At the moment only following content types are support for the email body
- text/html
- text/plain
Add CC and BCC
Adding CC and BCC is also simple as
require 'vendor/autoload.php'; use MessageX\Service\Email\ValueObject\Email; use MessageX\Service\Email\ValueObject\Body\Body; use MessageX\Service\Email\EmailClient; $emailClient = new EmailClient([ 'credentials' => ['key' => 'XXXXX', 'secret' => 'XXXXX'] ]); $emailClient->sendEmail( (new Email('John Doe <johndoe@example.com>', ['Jane Doe <janedoe@example.com>'])) ->setSubject('Greetings') ->addBcc(['John Doe <bcc@example.com>']) ->addCc(['John Doe <cc@example.com>']) ->addBody(new Body('text/plain', 'Hi Jane')) );
Same rules applies regarding email address and names as in the first example.
Add Attachment
You can add one or more attachments to the email as following
require 'vendor/autoload.php'; use MessageX\Service\Email\ValueObject\Email; use MessageX\Service\Email\ValueObject\Body\Body; use MessageX\Service\Email\ValueObject\Attachment\Attachment; use MessageX\Service\Email\EmailClient; $emailClient = new EmailClient([ 'credentials' => ['key' => 'XXXXX', 'secret' => 'XXXXX'] ]); $emailClient->sendEmail( (new Email('John Doe <johndoe@example.com>', ['Jane Doe <janedoe@example.com>'])) //... Code removed ->addAttachment(Attachment::fromFile('path/to/file.pdf')) );
Custom Tags
Tags are custom text labels associated with the the email. Maximum number of tags that can be added is 5. Add you custom tags as showed in sample below
require 'vendor/autoload.php'; use MessageX\Service\Email\ValueObject\Email; use MessageX\Service\Email\ValueObject\Body\Body; use MessageX\Service\Email\ValueObject\Attachment\Attachment; use MessageX\Service\Email\EmailClient; $emailClient = new EmailClient([ 'credentials' => ['key' => 'XXXXX', 'secret' => 'XXXXX'] ]); $emailClient->sendEmail( (new Email('John Doe <johndoe@example.com>', ['Jane Doe <janedoe@example.com>'])) //... Code removed ->addTag('Tag1') );
Custom Headers
Also custom headers will be added to the email and sent to the recipients. Maximum number of custom headers that can be added is 5. Add custom headers as shown in sample below
require 'vendor/autoload.php'; use MessageX\Service\Email\ValueObject\Email; use MessageX\Service\Email\ValueObject\Body\Body; use MessageX\Service\Email\ValueObject\Attachment\Attachment; use MessageX\Service\Email\EmailClient; $emailClient = new EmailClient([ 'credentials' => ['key' => 'XXXXX', 'secret' => 'XXXXX'] ]); $emailClient->sendEmail( (new Email('John Doe <johndoe@example.com>', ['Jane Doe <janedoe@example.com>'])) //... Code removed ->addHeader('X-Custom-Header', 'Value') );
Mail Merge
Properties are representing placeholder strings in email body and value for each property is array of replacements for all recipients. Number of replacements for each placeholder must match number of recipients. Order of replacements for each placeholder must match order of recipients. Mail merge can be added as shown in the sample below
require 'vendor/autoload.php'; use MessageX\Service\Email\ValueObject\Email; use MessageX\Service\Email\ValueObject\Body\Body; use MessageX\Service\Email\ValueObject\Attachment\Attachment; use MessageX\Service\Email\EmailClient; $emailClient = new EmailClient([ 'credentials' => ['key' => 'XXXXX', 'secret' => 'XXXXX'] ]); $emailClient->sendEmail( (new Email('John Doe <johndoe@example.com>', ['Jane Doe <janedoe@example.com>', 'Alice Doe <alicedoe@example.com>'])) //... Code removed ->addBody(new Body('text/plain', 'Hi {{name}}')) ->addSubstitution('name', ['Jane', 'Alice']) );
Documentation
SDK Reference
API Reference
Getting help
messagex/email-php-sdk 适用场景与选型建议
messagex/email-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.44k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「library」 「email」 「sdk」 「MX」 「messagex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 messagex/email-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 messagex/email-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 messagex/email-php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Inbox pattern process implementation for your Laravel Applications
Extensible library for building notifications and sending them via different delivery channels.
Email+ extends Kirby's email capabilities by adding support for multiple email services using the same Kirby email API.
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
统计信息
- 总下载量: 4.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-05