All Products
Search
Document Center

Direct Mail:PHP SDK manual

Last Updated:May 31, 2023

This topic describes how to install and use the PHP SDK.

Create an Access Key

(Note: You can also use the Access Key created by Alibaba Cloud RAM Service.)

  1. Log on to the Access Key Management console.

  2. Click Create Access Key in the upper-right corner of the page. The Create Access Key dialog box appears.

  3. Read the API usage specifications and click Agree and Create.

Install the PHP SDK

Development Environment

The PHP SDK Direct Mail by Alibaba Cloud supports PHP 5.3 or later.

(Recommend)Install the SDK

Install through composer

1. Add dependency package

You can obtain the new version number on the OpenAPI page and "SDK dependency information".

composer require alibabacloud/dm-20151123 1.0.x

2. Jump to openAPI for debugging, select the development language, enter parameters, and download the automatically generated code (the parameter value will be included).

You do not need to enter the Key value on the debugging page.

Warning

Do not hard coding the "accessKeyId" and "accessKeySecret" values in the code to avoid disclosure.

  • The Alibaba Cloud SDK supports defining defining the values of ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET in the environment variables of the operating system, after obtaining the values from the environment variables in the code, authentication can be performed.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

image

(Not recommend)The installation method of the old SDK.

SDK download

Direct download: https://aliyundm.oss-cn-hangzhou.aliyuncs.com/example/aliyun-php-sdk-dmV3.1.1.zip

Install the SDK

New installation

Find the aliyun-php-sdk-core and aliyun-php-sdk-dm folders in the downloaded package decompression folder and place them in the local directory of your website. Then, you can use the PHP SDK Direct Mail by Alibaba Cloud.

Incremental installation

If you have used the PHP SDK of other Alibaba Cloud products (such as ECS, Green, MTS, Push, RDS, and SLB), we recommend that you perform the following steps to install the SDK:

  1. Place the unzipped aliyun-php-sdk-dm folder of the downloaded package in the same directory as the aliyun-php-sdk-core.

  2. Open the aliyun-php-sdk-core/Config.php of your website directory and add the Direct Mail product SDK to the auto-load list:

    Autoloader::addAutoloadPath("aliyun-php-sdk-dm");
  3. Overwrite the previous file with the downloaded aliyun-php-sdk-core/RpcAcsRequest.php, aliyun-php-sdk-core/DefaultAcsClient.php, and aliyun-php-sdk-core\Http\HttpHelper.php. (Overwriting the file is to support a longer message body.)

Example of sending email

Example of calling a single sender API (SingleSendMail ):

<?php    
    include_once 'aliyun-php-sdk-core/Config.php';
    use Dm\Request\V20151123 as Dm;            
    // Set the corresponding region name. For example, set China (Hangzhou) to cn-hangzhou, Singapore Region to ap-southeast-1, and Australia Region to ap-southeast-2. 
	//Please configure in the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID,ALIBABA_CLOUD_ACCESS_KEY_SECRET.
    //Reference documents:https://www.alibabacloud.com/help/en/directmail/latest/configure-authentication-accesskey-in-environment-variables    
	$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));        
    // You need to set the server address in the Singapore or Australia region. You do not need to set the address in China (Hangzhou). 
    //$iClientProfile::addEndpoint("ap-southeast-1","ap-southeast-1","Dm","dm.ap-southeast-1.aliyuncs.com");
    //$iClientProfile::addEndpoint("ap-southeast-2","ap-southeast-2","Dm","dm.ap-southeast-2.aliyuncs.com");
    $client = new DefaultAcsClient($iClientProfile);    
    $request = new Dm\SingleSendMailRequest();     
    // You need to set the version of the SDK in the Singapore or Australia region. You do not need to set the version in China (Hangzhou). 
    //$request->setVersion("2017-06-22");
    $request->setAccountName("The sender address created in the console");
    $request->setFromAlias("Sender's nickname");
    $request->setAddressType(1);
    $request->setTagName("Tags created in the console");
    $request->setReplyToAddress("true");
    $request->setToAddress("Destination address");
    // You can send emails to multiple recipients. The recipients are separated by commas. If you call the template to send letters in batches, we recommend that you use the BatchSendMailRequest method.
    //$request->setToAddress("Email 1, Email 2");
    $request->setSubject("Email subject");
    $request->setHtmlBody("Email body");        
    try {
        $response = $client->getAcsResponse($request);
        print_r($response);
    }
    catch (ClientException  $e) {
        print_r($e->getErrorCode());   
        print_r($e->getErrorMessage());   
    }
    catch (ServerException  $e) {        
        print_r($e->getErrorCode());   
        print_r($e->getErrorMessage());
    }
?>

Also call the template to batch send letters, please use the BatchSendMailRequest, please refer to related modifications:

$request->setTemplateName("test template");
$request->setReceiversName("Test-test"); 

Where "test template" is the template name; "test-test" is the name of the recipient list.