Send email via Mail.app using AppleScript. No password or API key needed — Mail.app handles auth.
Use exec to run an inline osascript command. The script below is the canonical pattern — copy it exactly, then substitute values.
osascript << 'APPLESCRIPT'
tell application "Mail"
set m to make new outgoing message with properties {subject:"YOUR SUBJECT", content:"YOUR BODY", visible:false}
tell m
make new to recipient with properties {address:"recipient@example.com"}
make new to recipient with properties {address:"cc@example.com"}
end tell
send m
end tell
return "sent"
APPLESCRIPT
Escaping rules:
\"\\\n (AppleScript treats literal newlines in the string as content)Use when sending email from a Python cron script or agent workflow. Copy the function from scripts/send_email.py — do not rewrite it.
from scripts.send_email import send_email
send_email(
subject="Daily Report — 2026-01-01",
body="Here is the summary...",
to="primary@example.com",
cc="secondary@example.com", # optional
sender="your-mail-app-account@icloud.com" # must match Mail.app account
)
See scripts/send_email.py for the full implementation.
Add additional make new to recipient lines inside the tell m block:
make new to recipient with properties {address:"a@example.com"}
make new to recipient with properties {address:"b@example.com"}
| Symptom | Likely cause | Fix |
|---|---|---|
| --------- | ------------- | ----- |
osascript: Mail got an error | Mail.app not open or account not configured | Open Mail.app, check accounts |
| Email lands in Drafts | send m not reached (script errored before) | Check escape characters |
execution error: Mail got an error: Invalid recipient | Address typo | Verify recipient address |
| Email sends but empty body | Newline escape issue in content string | Use Python helper instead |
returncode != 0 with no stderr | osascript permission denied | Grant Automation permission: macOS Ventura+: System Settings → Privacy & Security → Automation; older macOS: System Preferences → Security & Privacy → Automation |
references/attachments.md共 1 个版本