All Products
Document Center

PHP SDK tutorial

Last Updated: Dec 18, 2017

Create an AccessKey

  1. Log on to the AccessKey console.
  2. Click Create AccessKey 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

Click here to download the SDK.

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. //Need to set up the corresponding region name, such as east China 1 region is set to cn-hangzhou, Singapore region is set to ap-southeast-1, Australia region is set to ap-southeast-2.
  5. $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "<your accessKey>", "<your accessSecret>");
  6. //Singapore or Australia region need to set the address of the server, east China 1 (hangzhou) do not need to be set.
  7. //$iClientProfile::addEndpoint("ap-southeast-1","ap-southeast-1","Dm","");
  8. //$iClientProfile::addEndpoint("ap-southeast-2","ap-southeast-2","Dm","");
  9. $client = new DefaultAcsClient($iClientProfile);
  10. $request = new Dm\SingleSendMailRequest();
  11. //Singapore or Australia region need to set the version of the SDK, east China 1 (hangzhou) do not need to be set.
  12. //$request->setVersion("2017-06-22");
  13. $request->setAccountName("Sender address created in the console");
  14. $request->setFromAlias("Sender nickname");
  15. $request->setAddressType(1);
  16. $request->setTagName("Tag created in the console");
  17. $request->setReplyToAddress("true");
  18. $request->setToAddress("Destination address");
  19. $request->setSubject("Subject");
  20. $request->setHtmlBody("Body");
  21. try {
  22. $response = $client->getAcsResponse($request);
  23. print_r($response);
  24. }
  25. catch (ClientException $e) {
  26. print_r($e->getErrorCode());
  27. print_r($e->getErrorMessage());
  28. }
  29. catch (ServerException $e) {
  30. print_r($e->getErrorCode());
  31. print_r($e->getErrorMessage());
  32. }
  33. ?>