智能电子邮件 API 的 API 包装器。
要求
该软件包需要 PHP 7.4 及更高版本。
通过作曲家安装
composer require pion/smart-emailing-v3
使用您的用户名和 apiKey 创建一个 Api 实例。
use SmartEmailing v3 Api ;
. . .
$ api = new Api ( ' username ' , ' api-key ' );
然后将$api
与所需的方法/组件一起使用。
// Creates a new instance
$ api -> importRequest ()-> addContact ( new Contact ( ' [email protected] ' ))-> send ();
或者
// Creates a new instance
$ import = $ api -> importRequest ();
$ contact = new Contact ( ' [email protected] ' );
$ contact -> setName ( ' Martin ' )-> setNameDay ( ' 2017-12-11 11:11:11 ' );
$ import -> addContact ( $ contact );
// Create new contact that will be inserted in the contact list
$ contact2 = $ import -> newContact ( ' [email protected] ' );
$ contact2 -> setName ( ' Test ' );
// Create new contact that will be inserted in the contact list
$ import -> newContact ( ' [email protected] ' )-> setName ( ' Test ' );
$ import -> send ();
发送任何请求时,您都可以捕获错误异常RequestException
。
use SmartEmailing v3 Exceptions RequestException ;
try {
$ api -> ping ();
} catch ( RequestException $ exception ) {
$ exception -> response (); // to get the real response, will hold status and message (also data if provided)
$ exception -> request (); // Can be null if the request was 200/201 but API returned error status text
}
导入包含 2 个主要数据点:
$import->settings()->setUpdate(true)
$import->newContact() : Contact
, $import->contacts() : array
和$import->addContact($contact) : self
使用示例如上。
导入包含 3 个主要数据点:
$contact->customFields()
用于添加新字段$contact->contactLists()
用于添加新的联系人列表请参阅源代码以了解您可以使用的所有方法/属性
使用带有create
/ add
/ get
/ isEmpty
/ toArray
/ jsonSerialize
方法的数据持有者。
$ field = $ contact -> customFields ()-> create ( 12 , ' test ' )
$ list = $ contact -> contactLists ()-> create ( 12 , ' confirmed ' )
导入包含 2 个主要数据点:
$import->settings()->setSkipInvalidOrders(true)
$import->newOrder() : Order
, $import->orders() : array
和$import->addOrder($order) : self
使用示例如上。
customFields 对与自定义字段相关的每个请求使用包装器。要创建新实例,请调用$api->customFields()
。在此对象上,您可以创建当前已实现的任何请求。见下文。
使用所需的自定义字段创建请求的快速方法
use SmartEmailing v3 Models CustomFieldDefinition ;
. . .
// Create the new customField and send the request now.
$ customField = new CustomFieldDefinition ( ' test ' , CustomFieldDefinition:: TEXT );
$ data = $ api -> customFields ()-> create ( $ customField );
// Get the customField in data
$ customFieldId = $ data -> id ;
或者
$ request = $ api -> customFields ()-> createRequest (); // You can pass the customField object
// Setup customField
$ customField = new CustomField ();
$ request -> setCustomField ( $ customField );
// Setup data
$ customField -> setType (CustomField:: RADIO )-> setName ( ' test ' );
// Send the request
$ response = $ request -> send ();
$ data = $ response -> data ();
$ customFieldId = $ data -> id ;
API文档
启用搜索并提供过滤/排序支持的自定义字段。每页结果限制为 100 个。响应通过调用$response->data()
返回元数据 (MetaDataInterface) 和ModelsCustomFieldDefinition
数组。
ModelsCustomFieldDefinition
stdClass
(在MetaDataInterface
中定义) 创建搜索请求并仅设置$page
或$limit
。来自 api 的完整响应,带有customfield_options_url
或
$ data = $ api -> customFields ()-> list ();
/** @var SmartEmailingv3ModelsCustomFieldDefinition $customField */
foreach ( $ data as $ customField ) {
echo $ customField -> id ;
echo $ customField -> name ;
echo $ customField -> type ;
}
$ request = $ api -> customFields ()-> searchRequest ( 1 );
// Search by name
$ request -> filter ()-> byName ( ' test ' );
$ request -> sortBy ( ' name ' );
// Send the request
$ response = $ request -> send ();
$ data = $ response -> data ();
filter()
返回一个过滤器设置 - 更多内容见下文使用此参数,“customfield_options_url”属性将被包含扩展数据的“customfield_options”替换。请参阅下面的示例有关详细信息,请参阅“/customfield-options”端点。
允许的值:“customfield_options”
要选择的属性的逗号分隔列表。例如。 "?select=id,name" 如果未提供,则选择所有字段。
允许的值:“id”、“名称”、“类型”
左侧以逗号分隔的排序键列表。在任意键前面添加“-”以表示递减方向,例如。 “?排序=类型,-名称”
允许的值:“id”、“名称”、“类型”
设置当前页面
设置单个查询的结果限制
允许使用多个过滤条件过滤自定义字段。
使用名称过滤器运行搜索查询并检查是否在自定义字段中找到给定名称。返回false
或CustomFieldsCustomField
。使用发送逻辑(抛出 RequestException)。
// Can throw RequestException - uses send.
if ( $ customField = $ api -> customFields ()-> getByName ( ' name ' )) {
return $ customField -> id ;
} else {
throw new Exception ( ' Not found! ' , 404 );
}
API 调用send/transactional-emails-bulk
的实现:https://app.smartemailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_transactional_emails
$ transactionEmail = $ api -> transactionalEmailsRequest ();
$ credentials = new SenderCredentials ();
$ credentials -> setFrom ( ' [email protected] ' );
$ credentials -> setReplyTo ( ' [email protected] ' );
$ credentials -> setSenderName ( ' Jean-Luc Picard ' );
$ recipient = new Recipient ();
$ recipient -> setEmailAddress ( ' [email protected] ' );
$ replace1 = new Replace ();
$ replace1 -> setKey ( ' key1 ' );
$ replace1 -> setContent ( ' content1 ' );
$ replace2 = new Replace ();
$ replace2 -> setKey ( ' key2 ' );
$ replace2 -> setContent ( ' content2 ' );
$ templateVariable = new TemplateVariable ();
$ templateVariable -> setCustomData ([
' foo ' => ' bar ' ,
' products ' => [
[ ' name ' => ' prod1 ' , ' desc ' => ' desc1 ' ],
[ ' name ' => ' prod1 ' , ' desc ' => ' desc2 ' ]
]
]);
$ attachment1 = new Attachment ();
$ attachment1 -> setContentType ( ' image/png ' );
$ attachment1 -> setFileName ( ' picture.png ' );
$ attachment1 -> setDataBase64 ( ' data1 ' );
$ attachment2 = new Attachment ();
$ attachment2 -> setContentType ( ' image/gif ' );
$ attachment2 -> setFileName ( ' sun.gif ' );
$ attachment2 -> setDataBase64 ( ' data2 ' );
$ task = new Task ();
$ task -> setRecipient ( $ recipient );
$ task -> addReplace ( $ replace1 );
$ task -> addReplace ( $ replace2 );
$ task -> setTemplateVariables ( $ templateVariable );
$ task -> addAttachment ( $ attachment1 );
$ task -> addAttachment ( $ attachment2 );
$ messageContents = new MessageContents ();
$ messageContents -> setTextBody ( ' text_body ' );
$ messageContents -> setHtmlBody ( ' html_body ' );
$ messageContents -> setSubject ( ' subject ' );
$ transactionEmail -> setTag ( ' tag_tag ' );
$ transactionEmail -> setEmailId ( 5 );
$ transactionEmail -> setSenderCredentials ( $ credentials );
$ transactionEmail -> addTask ( $ task );
$ transactionEmail -> setMessageContents ( $ messageContents );
$ transactionEmail -> send ();
API 调用send/custom-emails-bulk
的实现:https://app.smartmailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_bulk_custom_emails
$ transactionEmail = $ api -> customEmailsBulkRequest ();
$ credentials = new SenderCredentials ();
$ credentials -> setFrom ( ' [email protected] ' );
$ credentials -> setReplyTo ( ' [email protected] ' );
$ credentials -> setSenderName ( ' Jean-Luc Picard ' );
$ recipient = new Recipient ();
$ recipient -> setEmailAddress ( ' [email protected] ' );
$ replace1 = new Replace ();
$ replace1 -> setKey ( ' key1 ' );
$ replace1 -> setContent ( ' content1 ' );
$ replace2 = new Replace ();
$ replace2 -> setKey ( ' key2 ' );
$ replace2 -> setContent ( ' content2 ' );
$ templateVariable = new TemplateVariable ();
$ templateVariable -> setCustomData ([
' foo ' => ' bar ' ,
' products ' => [
[ ' name ' => ' prod1 ' , ' desc ' => ' desc1 ' ],
[ ' name ' => ' prod1 ' , ' desc ' => ' desc2 ' ]
]
]);
$ task = new Task ();
$ task -> setRecipient ( $ recipient );
$ task -> addReplace ( $ replace1 );
$ task -> addReplace ( $ replace2 );
$ task -> setTemplateVariables ( $ templateVariable );
$ transactionEmail -> setTag ( ' tag_tag ' );
$ transactionEmail -> setEmailId ( 5 );
$ transactionEmail -> setSenderCredentials ( $ credentials );
$ transactionEmail -> addTask ( $ task );
$ transactionEmail -> send ();
API调用send/custom-sms-bulk
的实现:https://app.smartmailing.cz/docs/api/v3/index.html#api-Custom_campaigns-Send_bulk_custom_SMS
$ bulkCustomSms = $ api -> customSmsBulkRequest ();
$ recipient = new Recipient ();
$ recipient -> setEmailAddress ( ' [email protected] ' );
$ recipient -> setCellphone ( ' +420777888777 ' );
$ replace1 = new Replace ();
$ replace1 -> setKey ( ' key1 ' );
$ replace1 -> setContent ( ' content1 ' );
$ replace2 = new Replace ();
$ replace2 -> setKey ( ' key2 ' );
$ replace2 -> setContent ( ' content2 ' );
$ task = new Task ();
$ task -> setRecipient ( $ recipient );
$ task -> addReplace ( $ replace1 );
$ task -> addReplace ( $ replace2 );
$ bulkCustomSms -> setTag ( ' tag_tag ' );
$ bulkCustomSms -> setSmsId ( 5 );
$ bulkCustomSms -> addTask ( $ task );
$ bulkCustomSms -> send ();
请参阅 UPGRADE.md 了解如何升级到新版本。
请参阅 CONTRIBUTING.md 了解如何贡献更改。欢迎所有贡献。
smart-emailing-v3 由 Martin Kluska 编写,根据 MIT 许可证发布。
版权所有 (c) 2016 - 2022 Martin Kluska 和贡献者