PHP SDK tutorial

Last Updated: Sep 26, 2017

Create an AccessKey

  1. Log on to the Access Key console.
  2. Click Create Access Key at the upper right corner of the page.
  3. Read API Terms of Use , and then click Agree and Create.

Note: You can use the AccessKey created with Alibaba Cloud Resource Access Management service.

Install the PHP SDK

Development environment

Alibaba Cloud DirectMail PHP SDK supports PHP 5.3 or later.

Download the SDK

Download the SDK directly.

Install the SDK

Fresh installation

Unzip the downloaded package and find the folders: “aliyun-php-sdk-core” and “aliyun-php-sdk-dm” . Place them to the directory of your website. Now, you can use the PHP SDK of Alibaba Cloud DirectMail.

Incremental installation

If you have used PHP SDKs of other Alibaba Cloud products (such as ECS, Green, MTS, Push, RDS, Server Load Balancer and STS), we recommend that you follow those steps for installation:

  1. Place “the aliyun-php-sdk-dm” folder to the same directory of the “aliyun-php-sdk-core” folder.

  2. Open the “aliyun-php-sdk-core/Config.php” in your website directory and add the DirectMail SDK to the automatic load list:
    1. Autoloader::addAutoloadPath("aliyun-php-sdk-dm");
  3. Overwrite the previous files with “aliyun-php-sdk-core/RpcAcsRequest.php”, “aliyun-php-sdk-core/DefaultAcsClient.php” and “aliyun-php-sdk-core\Http\HttpHelper.php” in the package. (The objective of overwriting the files is to support a longer email body.)

Email sending example

Take calling an API to send a single email as an example (For how to call the interface, see SingleSendMail ):

  1. <?php
  2. include_once 'aliyun-php-sdk-core/Config.php';
  3. use Dm\Request\V20151123 as Dm;
  4. $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
  5. $client = new DefaultAcsClient($iClientProfile);
  6. $request = new Dm\SingleSendMailRequest();
  7. $request->setAccountName("Sender address created in the console");
  8. $request->setFromAlias("Sender nickname");
  9. $request->setAddressType(1);
  10. $request->setTagName("Tag created in the console");
  11. $request->setReplyToAddress("true");
  12. $request->setToAddress("Destination address");
  13. $request->setSubject("Subject");
  14. $request->setHtmlBody("Body");
  15. try {
  16. $response = $client->getAcsResponse($request);
  17. print_r($response);
  18. }
  19. catch (ClientException $e) {
  20. print_r($e->getErrorCode());
  21. print_r($e->getErrorMessage());
  22. }
  23. catch (ServerException $e) {
  24. print_r($e->getErrorCode());
  25. print_r($e->getErrorMessage());
  26. }
  27. ?>
Thank you! We've received your feedback.