Skip to content

Listing sent messages

listMessages() and iterateMessages() search the messages your account has sent, with their delivery status. They offer the same search as the sent messages page of the web application. Use them to reconcile deliveries for a period, find failed messages, or look up every message sent to one number.

iterateMessages() returns a Generator. It fetches the pages one at a time, as you consume them, and yields each message as a Message:

use DateTimeImmutable;
use DateTimeZone;
use Rayo\SmsSdk\Models\DeliveryStatus;
$athens = new DateTimeZone('Europe/Athens');
$messages = $client->iterateMessages([
'limit' => 100,
'dateFrom' => new DateTimeImmutable('2026-12-01 00:00:00', $athens),
'dateTo' => new DateTimeImmutable('2027-01-01 00:00:00', $athens),
'status' => [DeliveryStatus::NOT_DELIVERED, DeliveryStatus::ERROR],
]);
foreach ($messages as $message) {
printf(
"%s | %s | %s | %s | %.2f credits | %s\n",
$message->getCreatedAtInUserTimeZone(),
$message->getId(),
$message->getMobileNumber(),
$message->getStatus(),
$message->getCredits(),
$message->getErrorDescription() ?? '-'
);
}

The query is validated as soon as iterateMessages() is called, before iteration starts. The generator sends each page’s next cursor back with the same sort and filters.

listMessages() returns one page as a MessagesResponse. To get the next page, send the same query again with the page’s next cursor:

use Rayo\SmsSdk\Models\MessagesQuery;
$query = new MessagesQuery(
sort: MessagesQuery::SORT_DESC,
mobileNumber: '+306912345678',
returnMessageContent: true
);
do {
$page = $client->listMessages($query);
foreach ($page->getMessages() as $message) {
echo $message->getMessage() . PHP_EOL;
}
if ($page->getNextCursor() !== null) {
// Keeps the same sort and filters.
$query = $query->withCursor($page->getNextCursor());
}
} while ($page->getNextCursor() !== null);

MessagesQuery has many optional parameters; call its constructor with named arguments. Its withCursor() returns a copy, so the query object itself never changes.

Each page also exposes getRequestId(), getTimeZone() (the IANA time zone of your account, used for the …InUserTimeZone timestamps), getLimit(), getSort(), and hasMore(). Treat the cursor as opaque.

Every parameter is optional. The filters combine: a message is returned only when it matches every filter you set.

Parameter Description
sort asc (default) returns the oldest messages first, desc the newest first. Messages are sorted by the time they were created.
limit Messages per page, from 1 to 100. The server’s default is 25.
dateFrom Only messages created at or after this moment.
dateTo Only messages created before this moment. dateTo itself is excluded and must be later than dateFrom.
status Only messages in one of these delivery statuses: an array of DeliveryStatus constants, or a comma-separated string such as 'not_delivered,unknown_delivery'.
mobileNumber Only messages sent to a number that ends with these digits, such as 306912345678, 6912345678, or 5678. Digits only, optionally after a +.
clientRequestId Only messages whose clientRequestId is exactly this value. The match is case-sensitive.
smsServerRequestId Only messages of these requests: up to 100 smsServerRequestId values, as an array or a comma-separated string.
smsRequestScheduleId Only messages sent by this schedule: the ID that sendSms() returned when the schedule was created.
returnMessageContent true also returns each message’s text and whether it has been masked. false (default) leaves both out.
cursor The next cursor of the previous page. Omit it for the first page.

dateFrom and dateTo take a DateTimeInterface or an RFC 3339 string with an offset, such as 2026-12-01T00:00:00+02:00 or 2026-11-30T22:00:00Z. Because dateTo is excluded, set it to the start of the next period. The dateTo of one period can then be the dateFrom of the next without missing or repeating a message.

The SDK sends lists comma-separated and returnMessageContent as true or false, and URL-encodes the + of an offset or of a mobile number for you.

The sent messages page of the web application groups statuses under five labels. To get the same result through the SDK, pass these status values:

Page label status
New new,temp_locked
Processing error processing_error,failure
Sent out sent,unknown_delivery
Delivered delivered
Not delivered not_delivered,error
Getter Description
getId() The message’s ID. The delivery status callback sends it as internalSmsOutDeliveryId.
getSmsServerRequestId() The ID of the request that sent the message, or null for some messages sent before June 2016.
getSmsRequestScheduleId() The schedule that sent the message, or null if it was not scheduled.
getClientRequestId() Your own correlation ID, or null.
getSenderId(), getMobileNumber() The sender label and the recipient.
getStatus(), getErrorDescription() A DeliveryStatus value, and an error description when the status implies one.
getCredits() The credits charged. It stays 0 until the message has been sent out (sent, delivered, not_delivered, or unknown_delivery).
getCreatedAtUtc(), getCreatedAtInUserTimeZone() When the message was created. sort, dateFrom, and dateTo use this time.
getLastStatusUpdatedAtUtc(), getLastStatusUpdatedAtInUserTimeZone() When the status last changed.
isDelivered(), isFinalStatus() The same checks as on SmsDeliveryStatusItem.
getMessage(), isMasked(), hasMessageContent() The message text, and whether it has been masked. Filled only when returnMessageContent is true.

The SDK validates the query before it sends a request and throws a RayoException for an unknown sort, a limit outside 1 to 100, a date without an offset or that does not exist, a dateFrom that is not before dateTo, an unknown status, a malformed mobile number, a blank clientRequestId or smsRequestScheduleId, more than 100 smsServerRequestId values, an empty cursor, or an unknown key.

The SDK does not check that IDs look like UUIDs. Invalid parameters that reach the server, such as a malformed smsServerRequestId or a cursor issued for another sort, come back as HTTP status 422 and are thrown as a RayoException.