Alibaba Cloud SMS is a communication service that enables businesses to send text messages to mobile devices. The platform supports various messaging scenarios including verification codes, notification alerts, and promotional campaigns. With comprehensive API interfaces and SDKs, developers can seamlessly integrate SMS functionality into their applications.
SMS services are essential for modern applications, particularly for user authentication flows such as registration, login, and password recovery. This guide walks through the complete integration process from account setup to code implementation.
Prerequisites
Account Registration and Authentication
Before accessing Alibaba Cloud SMS services, you must have an active Alibaba Cloud account. Register at the official Alibaba Cloud portal and complete the real-name authentication process.
Authentication types include individual and enterprise verification. Enterprise authentication is recommended for most business use cases since individual accounts have restrictions on promotional SMS and mass mesasging features. Additionally, enterprise accounts can request official invoices for business expenses.
Purchasing SMS Packages
After authentication, navigate to the SMS product page to purchase message credits. The platform offers various package sizes—common options include 50,000 or 200,000 message bundles. Choose a package that aligns with your expected message volume.
Console Configuration
The SMS console provides essential tools for managing your messaging operations:
- Signature Management — Configure the sender identity that appears with your messages
- Template Management — Create and manage message templates that define your message content structure
- Campaign Sending — Configure and execute mass messaging campaigns
API Integration
Java Implementation
package com.example.sms;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.teautil.models.*;
public class SmsService {
public static Client initializeClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
public static void sendMessage(String[] args) throws Exception {
String keyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String keySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
Client smsClient = initializeClient(keyId, keySecret);
SendSmsRequest request = new SendSmsRequest();
RuntimeOptions runtimeOptions = new RuntimeOptions();
try {
smsClient.sendSmsWithOptions(request, runtimeOptions);
} catch (TeaException e) {
System.err.println("SMS Send Error: " + e.getMessage());
}
}
}
PHP Implementation
<?php
require_once __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\Tea\Exception\TeaError;
use Darabonba\OpenApi\Models\Config;
class SmsHandler {
public static function createClient($accessKeyId, $accessKeySecret) {
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
public static function sendBatch($phoneNumbers, $templateCode) {
$client = self::createClient(
getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
);
$request = new \AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendBatchSmsRequest([
'phoneNumberJson' => json_encode($phoneNumbers),
'templateCode' => $templateCode
]);
try {
$response = $client->sendBatchSmsWithOptions($request, new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions());
return $response->body;
} catch (Exception $e) {
error_log("Batch SMS failed: " . $e->getMessage());
return null;
}
}
}
Security Considerations
When implementing SMS functionaliyt, secure your access credentials carefully. Avoid hardcoding AccessKey IDs and secrets directly in source code. Instead, leverage environment variables or secure credential management systems. The examples above demonstrate environment variable usage, which provides better security than embedding secrets in application code.
For production environments, consider implementing STS (Security Token Service) for temporary credential generation, which limits the exposure risk if credentials are compromised.