智慧型電子郵件 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 和貢獻者