這是Mailgun PHP SDK。該SDK包含用於輕鬆與Mailgun API交互的方法。以下是讓您入門的示例。有關其他示例,請在http://documentation.mailgun.com上查看我們的正式文檔
要安裝SDK,您需要在項目中使用作曲家。如果您還沒有使用作曲家,那真的很簡單!這是安裝作曲家的方法:
curl -sS https://getcomposer.org/installer | php
MailGun API客戶端並不是很難與Guzle,Buzz或任何其他發送HTTP消息的庫。相反,它使用PSR-18客戶端抽象。這將使您可以靈活地選擇要使用的PSR-7實現和HTTP客戶端。
如果您只想快速入門,則應運行以下命令:
composer require mailgun/mailgun-php symfony/http-client nyholm/psr7
您應始終在應用程序中使用Composer AutoLoader來自動加載依賴項。下面的所有示例都假設您已經將其包含在文件中:
require ' vendor/autoload.php ' ;
use Mailgun Mailgun ;
這是使用SDK發送消息的方法:
// First, instantiate the SDK with your API credentials
$ mg = Mailgun :: create ( ' key-example ' ); // For US servers
$ mg = Mailgun :: create ( ' key-example ' , ' https://api.eu.mailgun.net ' ); // For EU servers
// Now, compose and send your message.
// $mg->messages()->send($domain, $params);
$ mg -> messages ()-> send ( ' example.com ' , [
' from ' => ' [email protected] ' ,
' to ' => ' [email protected] ' ,
' subject ' => ' The PHP SDK is awesome! ' ,
' text ' => ' It is so simple to send a message. '
]);
注意: $domain
必須與您在app.mailgun.com上配置的域匹配。
# Include the Autoloader (see "Libraries" for install instructions)
require ' vendor/autoload.php ' ;
use Mailgun Mailgun ;
# Instantiate the client.
$ mgClient = Mailgun :: create ( ' KEY ' , ' FULL_DOMAIN_URL ' );
$ domain = " DOMAIN " ;
# Issue the call to the client.
$ result = $ mgClient -> domains ()-> updateWebScheme ( $ domain , ' https ' );
print_r ( $ result );
# Include the Autoloader (see "Libraries" for install instructions)
require ' vendor/autoload.php ' ;
use Mailgun Mailgun ;
# Instantiate the client.
$ mgClient = Mailgun :: create ( ' KEY ' , ' FULL_DOMAIN_URL ' );
$ domain = " DOMAIN " ;
# Issue the call to the client.
$ result = $ mgClient -> domains ()-> updateWebPrefix ( $ domain , ' tracking ' );
print_r ( $ result );
MailgunModelDomainWebPrefixResponse Object
(
[message:MailgunModelDomainAbstractDomainResponse:private] => Domain web prefix updated
[domain:MailgunModelDomainAbstractDomainResponse:private] =>
[inboundDnsRecords:MailgunModelDomainAbstractDomainResponse:private] => Array
(
)
[outboundDnsRecords:MailgunModelDomainAbstractDomainResponse:private] => Array
(
)
)
<?php
# Include the Autoloader (see "Libraries" for install instructions)
require ' vendor/autoload.php ' ;
use Mailgun Mailgun ;
# Instantiate the client.
$ mgClient = Mailgun :: create ( ' KEY ' , ' ENDPOINT ' );
$ domain = " DOMAIN " ;
$ path = ' some path ' ;
$ params = [];
# Issue the call to the client.
$ resultPost = $ mgClient -> httpClient ()-> httpPost ( $ path , $ params );
$ resultGet = $ mgClient -> httpClient ()-> httpGet ( $ path , $ params );
$ resultPut = $ mgClient -> httpClient ()-> httpPut ( $ path , $ params );
$ resultDelete = $ mgClient -> httpClient ()-> httpDelete ( $ path , $ params );
//Enable Sub Account
try {
$ items = $ mgClient -> subaccounts ()-> enable ( $ id );
} catch ( Exception $ exception ) {
echo sprintf ( ' HTTP CODE - %s, ' , $ exception -> getCode ());
echo sprintf ( ' Error - %s ' , $ exception -> getMessage ());
}
//Create a new Sub Account
try {
$ items = $ mgClient -> subaccounts ()-> create ( ' some name ' );
} catch ( Exception $ exception ) {
echo sprintf ( ' HTTP CODE - %s, ' , $ exception -> getCode ());
echo sprintf ( ' Error - %s ' , $ exception -> getMessage ());
}
//Get All
try {
$ items = $ mgClient -> subaccounts ()-> index ();
print_r ( $ items -> getItems ());
} catch ( Exception $ exception ) {
echo sprintf ( ' HTTP CODE - %s, ' , $ exception -> getCode ());
echo sprintf ( ' Error - %s ' , $ exception -> getMessage ());
}
您可以在此處閱讀更多詳細信息-https://help.mailgun.com/hc/en-us/articles/16380043681435-subaccounts#01h2vmhaw8cn4a7wxm6zfnsh4rr
$ mgClient = Mailgun :: create (
' xxx ' ,
' yyy ' ,
$ subAccountId
);
use Mailgun HttpClient HttpClientConfigurator ;
use Mailgun Hydrator NoopHydrator ;
$ configurator = new HttpClientConfigurator ();
$ configurator -> setEndpoint ( ' http://bin.mailgun.net/aecf68de ' );
$ configurator -> setApiKey ( ' key-example ' );
$ configurator -> setSubAccountId ( $ subAccountId )
<?php
# Include the Autoloader (see "Libraries" for install instructions)
require ' vendor/autoload.php ' ;
use Mailgun Mailgun ;
# Instantiate the client.
$ mgClient = Mailgun :: create ( ' xxx ' );
$ domain = " xxx.mailgun.org " ;
$ result = $ mgClient -> metrics ()-> loadMetrics ([
' start ' => ' Wed, 11 Sep 2024 18:29:02 +0300 ' ,
' end ' => ' Wed, 25 Sep 2024 18:29:02 +0300 ' ,
' metrics ' => [
" failed_count " , " opened_count " , " sent_count " , " delivered_count "
],
' resolution ' => ' month ' ,
' precision ' => ' day ' ,
' dimensions ' => [
' time ' ,
],
' include_aggregates ' => true ,
' include_subaccounts ' => true ,
]);
print_r ( $ result -> getItems ());
您會在/doc和https://documentation.mailgun.com上找到更多詳細的文檔。
默認情況下,API調用的結果是域對象。這將使不閱讀文檔的響應很容易理解響應。人們可以在響應類中讀取DOC塊。這提供了出色的IDE集成。
$ mg = Mailgun :: create ( ' key-example ' );
$ dns = $ mg -> domains ()-> show ( ' example.com ' )-> getInboundDNSRecords ();
foreach ( $ dns as $ record ) {
echo $ record -> getType ();
}
如果您寧願與數組一起工作,而不是對象,則可以將ArrayHydrator
注入MailGun類。
use Mailgun Hydrator ArrayHydrator ;
$ configurator = new HttpClientConfigurator ();
$ configurator -> setApiKey ( ' key-example ' );
$ mg = new Mailgun ( $ configurator , new ArrayHydrator ());
$ data = $ mg -> domains ()-> show ( ' example.com ' );
foreach ( $ data [ ' receiving_dns_records ' ] as $ record ) {
echo isset ( $ record [ ' record_type ' ]) ? $ record [ ' record_type ' ] : null ;
}
您也可以使用NoopHydrator
從API調用中返回PSR7響應。
警告:使用NoopHydrator
時,非2000響應將沒有例外。
當事情不正確時,調試PHP SDK可能會有所幫助。要調試SDK,這裡有一些建議:
設置Mailgun郵政局的終點。 Postbin是一款允許您發布數據的Web服務,然後您可以通過瀏覽器顯示它。使用Postbin是一種快速確定您要傳輸到MailGun API的數據的簡便方法。
步驟1-創建一個新的郵政局。轉到http://bin.mailgun.net。郵報將產生一個特殊的URL。保存該網址。
步驟2-使用Postbin實例化Mailgun客戶端。
提示:bin ID將是bin.mailgun.net之後的URL部分。它將是隨機生成的字母和數字。例如,此URL中的bin ID(http://bin.mailgun.net/aecf68de)是aecf68de
。
use Mailgun HttpClient HttpClientConfigurator ;
use Mailgun Hydrator NoopHydrator ;
$ configurator = new HttpClientConfigurator ();
$ configurator -> setEndpoint ( ' http://bin.mailgun.net/aecf68de ' );
$ configurator -> setApiKey ( ' key-example ' );
$ configurator -> setDebug ( true );
$ mg = new Mailgun ( $ configurator , new NoopHydrator ());
# Now, compose and send your message.
$ mg -> messages ()-> send ( ' example.com ' , [
' from ' => ' [email protected] ' ,
' to ' => ' [email protected] ' ,
' subject ' => ' The PHP SDK is awesome! ' ,
' text ' => ' It is so simple to send a message. '
]);
對於每個API端點上的使用示例,請轉到我們的官方文檔頁面。
此SDK包括一個消息構建器,批處理消息。
消息構建器允許您通過調用每個參數的方法來快速創建發送消息所需的參數數組。批處理消息是消息構建器的擴展,可讓您在幾秒鐘內輕鬆發送批處理消息作業。批處理消息的複雜性被消除了!
如果您使用的是框架,則可以考慮這些作曲家軟件包,以使集成框架更容易。
此SDK是MIT許可證下的開源。因此,它是由合作者和貢獻者維護的。
隨時以任何方式貢獻。例如,您可以:
dev-master
代碼如果要運行測試,則應運行以下命令:
git clone [email protected]:mailgun/mailgun-php.git
cd mailgun-php
composer update
composer test
請務必訪問Mailgun官方文檔網站,以獲取有關我們API的更多信息。
如果找到錯誤,請直接在GitHub中提交問題。 MailGun-PHP問題
與往常一樣,如果您需要額外的幫助,請通過您的帳戶https://app.mailgun.com/support向我們提供註釋。