这是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向我们提供注释。