Perl
Send SMS or MMS with Perl
Learn how to send your first SMS using the CCAI Perl SDK
Prerequisites
To get the most out of this guide, you'll need to:
- Sign up for a CCAI Paid Plan
- Create an API Key
- Get your Client ID
- Watch this video if you need help.
1. Install
Get the CCAI Perl SDK
git clone https://github.com/cloudcontactai/ccai-perl.git
cd ccai-perl
cpanm --installdeps .
# Verify SSL configuration
perl verify_ssl.pl
#In examples/sms_example.pl
#Update YOUR-CLIENT-ID and API-KEY-TOKEN
#Update the account object to send messages to a valid phone number.
#After you make changes to examples/sms_example.pl
./run_sms_example.sh
2. Send SMS message
#Update YOUR-CLIENT-ID and API-KEY-TOKEN
#Update the account object to send messages to a valid phone number.
use lib '.';
use CCAI;
# Initialize the client
my $ccai = CCAI->new({
client_id => 'YOUR-CLIENT-ID',
api_key => 'API-KEY-TOKEN'
});
# Send an SMS to multiple recipients
my @accounts = (
{
firstName => "John",
lastName => "Doe",
phone => "+15551234567"
},
{
firstName => "Jane",
lastName => "Smith",
phone => "+15559876543"
}
);
my $response = $ccai->sms->send(
\@accounts,
"Hello \${firstName} \${lastName}, this is a test message!",
"Test Campaign"
);
if ($response->{success}) {
print "SMS sent successfully! Campaign ID: " . $response->{data}->{campaign_id} . "\n";
} else {
print "Error: " . $response->{error} . "\n";
}
# Send an SMS to a single recipient
my $single_response = $ccai->sms->send_single(
"Jane",
"Smith",
"+15559876543",
"Hi \${firstName}, thanks for your interest!",
"Single Message Test"
);
if ($single_response->{success}) {
print "Single SMS sent successfully!\n";
} else {
print "Error: " . $single_response->{error} . "\n";
}
3. Send MMS Message
#Update YOUR-CLIENT-ID and API-KEY-TOKEN
#Update the account object to send messages to a valid phone number.
#Update path/to/your/image.jpg to be an actual jpg file
use lib '.';
use CCAI;
# Initialize the client
my $ccai = CCAI->new({
client_id => 'YOUR-CLIENT-ID',
api_key => 'API-KEY-TOKEN'
});
# Define progress callback
my $progress_callback = sub {
my $status = shift;
print "Progress: $status\n";
};
# Create options with progress tracking
my $options = {
timeout => 60000,
on_progress => $progress_callback
};
# Complete MMS workflow (get URL, upload image, send MMS)
sub send_mms_with_image {
# Path to your image file
my $image_path = 'path/to/your/image.jpg';
my $content_type = 'image/jpeg';
# Define recipient
my @accounts = ({
firstName => 'John',
lastName => 'Doe',
phone => '+15551234567'
});
# Send MMS with image in one step
my $response = $ccai->mms->send_with_image(
$image_path,
$content_type,
\@accounts,
"Hello \${firstName}, check out this image!",
"MMS Campaign Example",
$options
);
if ($response->{success}) {
print "MMS sent! Campaign ID: " . $response->{data}->{campaign_id} . "\n";
} else {
print "Error sending MMS: " . $response->{error} . "\n";
}
}
# Call the function
send_mms_with_image();
4. Try it yourself
See the full source code here.
Updated about 2 hours ago