이것은 Mailgun PHP SDK입니다. 이 SDK에는 Mailgun API와 쉽게 상호 작용하는 방법이 포함되어 있습니다. 다음은 시작하기위한 예입니다. 추가 예를 들어 보려면 공식 문서를 참조하십시오. http://documentation.mailgun.com을 참조하십시오.
SDK를 설치하려면 프로젝트에서 작곡가를 사용해야합니다. 아직 작곡가를 사용하지 않는다면 정말 간단합니다! 작곡가를 설치하는 방법은 다음과 같습니다.
curl -sS https://getcomposer.org/installer | php
Mailgun API 클라이언트는 HTTP 메시지를 보내는 Guzzle, Buzz 또는 기타 라이브러리와 어렵지 않습니다. 대신 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#01h2vmhaw8cn4a7wxm6zfnsh4r
$ 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
사용하면 비 200 년 응답에 대한 예외가 없습니다.
PHP SDK를 디버깅하는 것은 일이 제대로 작동하지 않을 때 도움이 될 수 있습니다. SDK를 디버깅하려면 다음은 다음과 같습니다.
종말점을 Mailgun의 Postbin으로 설정하십시오. Postbin은 데이터를 게시 할 수있는 웹 서비스로 브라우저를 통해 데이터를 표시 할 수 있습니다. Postbin을 사용하는 것은 Mailgun의 API에 전송하는 데이터를 신속하게 결정하는 쉬운 방법입니다.
1 단계 - 새 PostBin을 만듭니다. http://bin.mailgun.net으로 이동하십시오. Postbin은 특수 URL을 생성합니다. 해당 URL을 저장하십시오.
2 단계 - postbin을 사용하여 Mailgun 클라이언트를 인스턴스화하십시오.
팁 : 빈 ID는 bin.mailgun.net 다음에 URL 부분이됩니다. 무작위로 생성 된 글자와 숫자가 될 것입니다. 예를 들어,이 URL (http://bin.mailgun.net/aecf68de)의 bin id는 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에는 메시지 빌더, 배치 메시지가 포함되어 있습니다.
Message Builder를 사용하면 각 매개 변수의 메소드를 호출하여 메시지를 보내는 데 필요한 매개 변수 배열을 신속하게 만들 수 있습니다. 배치 메시지는 메시지 빌더의 확장이며 몇 초 안에 배치 메시지 작업을 쉽게 보낼 수 있습니다. 배치 메시징의 복잡성이 제거됩니다!
프레임 워크를 사용하는 경우이 작곡가 패키지를 고려하여 프레임 워크 통합을보다 쉽게 만들 수 있습니다.
이 SDK는 MIT 라이센스에 따른 오픈 소스입니다. 따라서 공동 작업자와 기고자에 의해 유지됩니다.
어떤 식 으로든 자유롭게 기여하십시오. 예를 들어 :
dev-master
코드를 사용해보십시오테스트를 실행하려면 다음 명령을 실행해야합니다.
git clone [email protected]:mailgun/mailgun-php.git
cd mailgun-php
composer update
composer test
API에 대한 추가 정보는 Mailgun 공식 문서 웹 사이트를 방문하십시오.
버그를 찾으면 문제를 GitHub에 직접 제출하십시오. Mailgun-PHP 문제
언제나 그렇듯이 추가 지원이 필요한 경우 https://app.mailgun.com/support에서 계정을 통해 메모를 보내십시오.