Skip to main content

Messages

All message operations are accessed via Agentmail::messages($inboxId). Each method returns a typed DTO.

use Polkachu\LaravelAgentmail\Facades\Agentmail;

List messages

Returns a MessageCollection containing a Laravel Collection of Message objects.

$collection = Agentmail::messages('inbox_abc')->list();

foreach ($collection->messages as $message) {
echo $message->messageId;
echo $message->subject;
}

Pagination

Pass limit to control page size and pageToken to advance through pages:

$collection = Agentmail::messages('inbox_abc')->list(limit: 20);

if ($collection->nextPageToken) {
$nextPage = Agentmail::messages('inbox_abc')->list(limit: 20, pageToken: $collection->nextPageToken);
}

Filtering

$collection = Agentmail::messages('inbox_abc')->list(
labels: ['INBOX'],
after: '2024-01-01T00:00:00Z',
before: '2024-12-31T23:59:59Z',
ascending: true,
includeSpam: false,
);

MessageCollection properties

PropertyTypeDescription
$messagesCollection<Message>The messages on the current page
$countintNumber of messages returned
$limit?intPage size limit used
$nextPageToken?stringCursor for the next page, or null on the last page

Get a message

$message = Agentmail::messages('inbox_abc')->get('msg_001');

echo $message->messageId;
echo $message->subject;
echo $message->from;
echo $message->timestamp->toDateTimeString();

Send a message

All parameters are optional. Pass to, subject, and text/html as needed.

$response = Agentmail::messages('inbox_abc')->send(
to: '[email protected]',
cc: '[email protected]',
subject: 'Hello from AgentMail',
text: 'This is the plain text body.',
html: '<p>This is the HTML body.</p>',
);

echo $response->messageId;
echo $response->threadId;

Update a message

Add or remove labels on an existing message.

$message = Agentmail::messages('inbox_abc')->update(
messageId: 'msg_001',
addLabels: ['STARRED'],
removeLabels: ['UNREAD'],
);

Reply to a message

$response = Agentmail::messages('inbox_abc')->reply(
messageId: 'msg_001',
text: 'Thanks for reaching out!',
);

echo $response->messageId;

Reply all to a message

$response = Agentmail::messages('inbox_abc')->replyAll(
messageId: 'msg_001',
text: 'Replying to everyone.',
);

Forward a message

$response = Agentmail::messages('inbox_abc')->forward(
messageId: 'msg_001',
to: '[email protected]',
text: 'FYI — forwarding this along.',
);

The Message DTO

PropertyTypeDescription
$inboxIdstringThe inbox this message belongs to
$threadIdstringThread identifier
$messageIdstringUnique message identifier
$labelsarrayArray of label strings (e.g. ['INBOX', 'UNREAD'])
$timestampCarbonWhen the message was sent/received
$fromstringSender address
$toarrayRecipient addresses
$sizeintMessage size in bytes
$updatedAtCarbonLast updated timestamp
$createdAtCarbonCreation timestamp
$cc?arrayCC addresses
$bcc?arrayBCC addresses
$replyTo?arrayReply-to addresses
$subject?stringEmail subject
$preview?stringShort preview text
$text?stringPlain text body
$html?stringHTML body
$extractedText?stringExtracted plain text
$extractedHtml?stringExtracted HTML
$inReplyTo?stringMessage ID this is a reply to
$references?arrayReference message IDs
$attachments?Collection<Attachment>Attachments
$headers?arrayCustom headers

The Attachment DTO

PropertyTypeDescription
$attachmentIdstringUnique attachment identifier
$sizeintAttachment size in bytes
$filename?stringOriginal filename
$contentType?stringMIME content type
$contentDisposition?stringContent disposition header value
$contentId?stringContent ID for inline attachments

The SendMessageResponse DTO

Returned by send(), reply(), replyAll(), and forward():

PropertyTypeDescription
$messageIdstringID of the newly created message
$threadIdstringThread the message belongs to

Testing

Use Laravel's Http::fake() to mock AgentMail responses without making real API calls:

use Illuminate\Support\Facades\Http;
use Polkachu\LaravelAgentmail\Facades\Agentmail;

Http::fake([
'*/v0/inboxes/inbox_abc/messages*' => Http::response([
'count' => 1,
'messages' => [[
'inbox_id' => 'inbox_abc',
'thread_id' => 'thread_xyz',
'message_id' => 'msg_001',
'labels' => ['INBOX'],
'timestamp' => '2024-01-01T10:00:00Z',
'from' => '[email protected]',
'to' => ['[email protected]'],
'size' => 1024,
'updated_at' => '2024-01-02T00:00:00Z',
'created_at' => '2024-01-01T00:00:00Z',
]],
]),
]);

$collection = Agentmail::messages('inbox_abc')->list();

expect($collection->count)->toBe(1);
expect($collection->messages->first()->messageId)->toBe('msg_001');