Connect a mailbox to Squid to send email from AI agents and backend code, and to react to incoming email with events
The Mail connector's functionality
The Mail connector lets your Squid application send and receive email:
- AI agents connected to the Mail connector can compose and send emails on the user's behalf. For example, "Email the report to the team."
- Backend code can send emails programmatically, including emails with attachments.
- Receiving: the connector can watch a mailbox over IMAP and emit a
mail-receivedevent for each incoming email, which an event handler or an agent automation reacts to.
The connector works with any standard SMTP/IMAP provider, with dedicated OAuth sign-in for Microsoft 365 and Google Workspace mailboxes. Throttling and sending quotas are governed by your provider.
Adding the Mail connector to your Squid application
-
Navigate to the Connectors tab in the Squid Console.
-
Click Available Connectors.
-
Find the Mail connector, and select Add Connector.
-
Provide the following configuration details:
Connector ID: A string that uniquely identifies the connector in your code.
SMTP Host: The hostname of your SMTP server, such as smtp.example.com. Do not include a protocol prefix.
SMTP Port: The port number for your SMTP server, such as 25, 465, or 587.
SMTP Username: The username to authenticate with the SMTP server.
SMTP Password: The password to authenticate with the SMTP server. The password is stored securely as a Squid secret. If your email provider uses app-specific passwords, use one here.
SMTP Encryption: The encryption method to use when connecting to the SMTP server: tls (default), ssl, or none.
SMTP From Address: The email address that appears as the sender when a send request doesn't specify one.
Mail Service: How the connector signs in: an SMTP Server with the password above (the default), or Microsoft 365 (tenant ID, client ID, and a client secret stored as a Squid secret) or Google Workspace (a service account key with domain-wide delegation, stored as a Squid secret). The Microsoft and Google services sign in with XOAUTH2, so no app passwords are involved.
Using the Mail connector in your application
No-code Studio
-
Navigate to the Studio tab in the Squid Console.
-
Click Create AI Agent.
-
Provide an agent ID and description, such as "assistant-agent" and "This agent can send emails on the user's behalf".
-
Click Add Abilities.
-
Expand the Mail entry and select the Mail connector you created earlier.
-
Give a description for how the agent should use this connection, such as "Call this when the user asks to send an email."
-
Click on Test and try asking the agent to send a test email to yourself.
Using the Squid SDK
If you are creating your AI agent with the Squid SDK, connect the Mail connector by adding it to the connectedIntegrations option of the ask() function:
- TypeScript
- Python
await this.squid
.ai()
.agent('AGENT_ID')
.ask('Send a summary of this conversation to team@example.com', {
connectedIntegrations: [
{
integrationId: 'MAIL_CONNECTOR_ID',
integrationType: 'mail',
description: 'Call this connector to send emails',
},
],
});
await squid.ai().agent('AGENT_ID').ask(
'Send a summary of this conversation to team@example.com',
options={
'connectedIntegrations': [
{
'integrationId': 'MAIL_CONNECTOR_ID',
'integrationType': 'mail',
'description': 'Call this connector to send emails',
}
]
},
)
Sending email from backend code
For programmatic sending (including attachments, which are not available through the AI agent tool), install the @squidcloud/mail-client package and call it from your Squid backend:
npm install @squidcloud/mail-client
import { SquidMailClient } from '@squidcloud/mail-client';
const mail = new SquidMailClient(this.squid);
await mail.sendMail({
integrationId: 'MAIL_CONNECTOR_ID',
to: 'recipient@example.com',
subject: 'Monthly report',
body: '<h1>Report</h1><p>See the attached PDF.</p>',
html: true,
attachments: [
{
filename: 'report.pdf',
content: base64EncodedPdf,
encoding: 'base64',
},
],
});
If from is omitted from the request, the connector uses the configured SMTP From Address. Each attachment provides either inline content or a path to a file on the backend, but not both.
A send request also accepts cc, bcc, replyTo, and, for threading a reply into an existing conversation, inReplyTo and references. The response carries the sent message's messageId, which you can pass as a later reply's inReplyTo.
Sending mail requires a privileged backend context, such as an executable, scheduler, or trigger. Unauthenticated client-side calls are rejected.
Receiving email
Turn on Receiving in the connector's configuration to watch a mailbox over IMAP. For each incoming email that passes the sender rules, the connector emits a mail-received event. Squid stores no copy of the email: the event carries the parsed headers, body text, and a list of attachments, whose bytes stay in the mailbox.
Receiving configuration:
- Mail Service: an IMAP Server (any provider), Microsoft 365, or Google Workspace; the Microsoft and Google choices fill the server settings and use the OAuth sign-ins described for sending.
- Monitoring Enabled: turns the watching on.
- Mailbox Address: the watched email address. Folder: defaults to
INBOX. - IMAP Host, IMAP Port, IMAP Encryption (
tlsorstarttls; there is no cleartext option), IMAP Username, and IMAP Password (stored as a Squid secret), for the IMAP Server choice. - Accepted Senders: which senders produce events. Any sender accepts everyone not blocked; Allowlist requires a matching allow rule (Allow the Mailbox Domain, Allowed Domains, Allowed Senders). The block lists (Blocked Domains, Blocked Senders) always win, and Include Subdomains applies domain rules to subdomains.
Handle the events in backend code:
import { eventHandler, SquidService, TriggerEvent } from '@squidcloud/backend';
import { MAIL_RECEIVED_EVENT_TYPE, MailReceivedPayload } from '@squidcloud/client';
export class MailService extends SquidService {
@eventHandler(MAIL_RECEIVED_EVENT_TYPE)
async onMailReceived(event: TriggerEvent<MailReceivedPayload>): Promise<void> {
const mail = event.payload;
// `from` passed the sender rules, but the header itself can be forged.
console.log(`From ${mail.from.address}: ${mail.subject ?? '(no subject)'}`);
}
}
Polling runs only while a mail-received event handler or event automation exists; without one, the receiving status reports no_handler and nothing is read.
Receiving operations
The @squidcloud/mail-client package also manages the receiving side. Every method requires the app API key:
import { SquidMailClient } from '@squidcloud/mail-client';
const mail = new SquidMailClient(this.squid);
// Check access to the mailbox without moving progress.
await mail.testMailReceivingConnection({ integrationId: 'MAIL_CONNECTOR_ID' });
// The health of the receiving side: its state, and the last error if any.
const status = await mail.getMailReceivingStatus({ integrationId: 'MAIL_CONNECTOR_ID' });
console.log(status);
// Start monitoring from now, so older mail gets no events.
await mail.resetMailReceiving({ integrationId: 'MAIL_CONNECTOR_ID' });
To read an attachment, call getMailPart() with the mailbox coordinates from the event (mailboxId, the message's UID fields, and the attachment's partId); the bytes are fetched from the mailbox on demand.