Connect email
Connect a domain, publish its DNS records, and send your agent its first email.
Give your agent an address on your own domain, such as
agent@mail.example.com. You connect the domain, publish the DNS records
Photon shows you, and send a first email to the agent.
Prerequisites
You need:
- A project from Set up your project.
- A domain you control, and access to edit its DNS records.
- A separate inbox to send test email from.
Connect your domain
Open Email in
your project. Under Connect a domain, enter the domain or subdomain and
select Connect domain. Enter only the domain, without https:// or an
email address.
Connect the domain with code instead
This replaces Connect domain in the dashboard; don't do both. Your API
key needs platforms:write to connect the domain and platforms:read
to poll its operation. Generate a unique setup key for this attempt:
export PHOTON_EMAIL_DOMAIN="mail.example.com"
export PHOTON_SETUP_KEY="first-agent-email-$(date +%s)"Save the generated PHOTON_SETUP_KEY. If the result is uncertain,
rerun only the setup program with the same domain and saved key. A new
timestamp starts a new attempt, not a retry.
Save the example as setup.ts, setup.py, src/main.rs, or main.go in the
directory you prepared in project setup.
import { Photon } from "@photon-ai/api";
const required = (name: string): string => {
const value = process.env[name];
if (!value) throw new Error(`Set ${name}`);
return value;
};
const photon = new Photon({
headers: { Authorization: `Bearer ${required("PHOTON_API_KEY")}` },
});
try {
const operation = await photon.projects.platforms.connectEmailDomain({
path: { id: required("PHOTON_PROJECT_ID") },
headers: { idempotencyKey: required("PHOTON_SETUP_KEY") },
body: { domain: required("PHOTON_EMAIL_DOMAIN") },
});
console.log(operation.operationId);
} catch {
console.error("Could not connect the domain. Check the key, permissions, and domain name.");
process.exitCode = 1;
}import os
from photon_api import Photon, PhotonError
from photon_api.generated.models import ConnectEmailDomainRequest
from photon_api.rpc_generated import (
ConnectEmailDomainHeader,
ConnectEmailDomainInput,
ConnectEmailDomainPath,
)
try:
with Photon(headers={"Authorization": f"Bearer {os.environ['PHOTON_API_KEY']}"}) as photon:
operation = photon.projects.platforms.connect_email_domain(
ConnectEmailDomainInput(
path=ConnectEmailDomainPath(id=os.environ["PHOTON_PROJECT_ID"]),
headers=ConnectEmailDomainHeader.model_validate(
{"Idempotency-Key": os.environ["PHOTON_SETUP_KEY"]}
),
body=ConnectEmailDomainRequest(domain=os.environ["PHOTON_EMAIL_DOMAIN"]),
)
)
print(operation.operationId)
except PhotonError:
raise SystemExit("Could not connect the domain. Check the key, permissions, and domain name.") from Noneuse photon_ai_api::{types, Credential, PhotonClientBuilder, SecretString};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = PhotonClientBuilder::new()
.credential(
"projectApiKey",
Credential::Bearer(SecretString::from(std::env::var("PHOTON_API_KEY")?)),
)
.build()?;
let operation = client
.connect_email_domain(
std::env::var("PHOTON_SETUP_KEY")?,
std::env::var("PHOTON_PROJECT_ID")?,
&types::ConnectEmailDomainRequest {
domain: std::env::var("PHOTON_EMAIL_DOMAIN")?,
},
)
.await
.map_err(|_| "Could not connect the domain. Check the key, permissions, and domain name.")?;
println!("{}", operation.inner().operation_id);
Ok(())
}package main
import (
"context"
"fmt"
"log"
"os"
photonapi "github.com/photon-hq/api/packages/go"
)
func required(name string) string {
value := os.Getenv(name)
if value == "" {
log.Fatalf("Set %s", name)
}
return value
}
func main() {
client := photonapi.NewClient(photonapi.WithAPIKey(required("PHOTON_API_KEY")))
operation, err := client.Projects.Platforms.ConnectEmailDomain(
context.Background(), &photonapi.ConnectEmailDomainParams{
ID: required("PHOTON_PROJECT_ID"),
IdempotencyKey: required("PHOTON_SETUP_KEY"),
Body: photonapi.ConnectEmailDomainRequest{
Domain: required("PHOTON_EMAIL_DOMAIN"),
},
},
)
if err != nil {
log.Fatal("Could not connect the domain. Check the key, permissions, and domain name.")
}
fmt.Println(operation.OperationID)
}node setup.tspython setup.pycargo run --bin photon-first-agentgo run main.goThe program prints an operation ID. Return to Email and open the domain to publish its DNS records.
Publish the DNS records
Open the domain's setup page. At your DNS provider, add every record Photon shows, with the same type, host, value, and MX priority. The values are specific to your domain; don't copy them from this image.
| Record | What it does |
|---|---|
| DKIM CNAME | Verifies your domain and signs the email your agent sends. |
| Bounce MX and SPF TXT | Set the return path for delivery failures. |
| Receiving MX | Routes incoming email for the domain to Photon. The domain doesn't connect without it. |
Each MX record must be the only MX record at its host. If your DNS provider appends the zone name to hosts, enter the host without the domain. If it offers a proxy for CNAME records, choose DNS only.
Wait for Connected
Photon checks your DNS automatically. Wait until the domain shows Connected and the verification checks pass. DNS changes can take a while to spread; reloading the page doesn't speed them up. Photon waits up to 72 hours per verification stage. If verification fails or times out, fix the records and connect the domain again. If you use the API, use a new idempotency key. See Domains for the records and errors.
Email your agent
Pick any address at exactly the connected domain, such as
agent@mail.example.com. A subdomain of the connected domain is not
covered. You don't create a mailbox first: Photon resolves the receiving
address when the first email arrives.
From your separate inbox, send a plain-text email to that address. Then open the domain in Email and select Inbox. Your email appears there.
If your email doesn't arrive
| What you see | What to check |
|---|---|
| The domain stays Waiting for DNS | Compare every record with your DNS provider: a doubled domain in the host, MX priorities, or a proxied CNAME. Then give DNS time to update. |
| The domain is Connected, but no email arrives | Check that you emailed the exact connected domain, not a subdomain of it. Photon drops mail to a domain that isn't connected without creating a Message. |
| Email arrives in the inbox, but not in your code later | The API key belongs to another project, or lacks project:read. |
Next: receive messages
For HTML bodies, attachments, recipients, delivery events, sending limits and errors, see Email.
Get each email to your agent as an event in your code.