Email SMTP Send
Core Goal
- Send outbound email via SMTP with env-configured credentials.
- Attach local files into MIME email payload when requested.
- Optionally append the sent message to IMAP sent mailbox for cross-client visibility.
- Validate SMTP and sent-sync configuration before delivery.
- Return machine-readable JSON status/error output.
Workflow
- Configure SMTP env vars (see
references/env.md and assets/config.example.env).
- Optional: configure IMAP sent-sync env vars and install
imapclient when sync is enabled.
- Validate configuration:
python3 scripts/smtp_send.py check-config
- Send one email:
python3 scripts/smtp_send.py send \
--to recipient@example.com \
--subject "SMTP test" \
--body "Hello from email-smtp-send"
- Send with attachments:
python3 scripts/smtp_send.py send \
--to recipient@example.com \
--subject "Package delivery" \
--body "See attachments." \
--attach ./report.pdf \
--attach ./appendix.xlsx
- Send and sync to sent mailbox:
python3 scripts/smtp_send.py send \
--to recipient@example.com \
--subject "Synced send" \
--body "This message will be appended to Sent Items." \
--sync-sent \
--sent-mailbox "Sent Items"
Output Contract
check-config prints sanitized SMTP config + defaults + sent-sync config JSON.
send success prints one type=status JSON object containing:
event=smtp_sent
- sender, recipient summary, subject, SMTP host/port
message_id
attachment_count and attachments[] metadata
sent_sync object with enabled, required, appended, and sync metadata/error
send failures print type=error JSON to stderr with one of:
event=smtp_send_invalid_args
event=smtp_send_failed
event=smtp_sent_sync_failed (only when sync is required and sync fails)
Parameters
send --to: required recipient, repeatable or comma-separated.
send --cc: optional CC recipients.
send --bcc: optional BCC recipients.
send --subject: optional subject (defaults from env).
send --body: optional body (defaults from env).
send --content-type: plain or html.
send --from: optional sender override.
send --attach: optional local attachment path, repeatable or comma-separated.
send --max-attachment-bytes: max bytes allowed per attachment.
send --message-id: optional Message-ID header.
send --in-reply-to: optional In-Reply-To header.
send --references: optional References header.
send --sync-sent|--no-sync-sent: force-enable/disable IMAP sent sync for this send.
send --sent-mailbox: override sent mailbox.
send --sent-flags: IMAP APPEND flags, comma-separated (default \Seen).
send --sent-sync-required: return non-zero if SMTP succeeds but sent sync fails.
Environment defaults:
SMTP_HOST, SMTP_PORT, SMTP_SSL, SMTP_STARTTLS
SMTP_USERNAME, SMTP_PASSWORD, SMTP_FROM, SMTP_CONNECT_TIMEOUT
SMTP_SUBJECT, SMTP_BODY, SMTP_CONTENT_TYPE
SMTP_MAX_ATTACHMENT_BYTES
SMTP_SYNC_SENT, SMTP_SYNC_SENT_REQUIRED
SMTP_SENT_IMAP_HOST, SMTP_SENT_IMAP_PORT, SMTP_SENT_IMAP_SSL
SMTP_SENT_IMAP_USERNAME, SMTP_SENT_IMAP_PASSWORD
SMTP_SENT_IMAP_MAILBOX, SMTP_SENT_IMAP_FLAGS, SMTP_SENT_IMAP_CONNECT_TIMEOUT
- compatibility fallbacks:
IMAP_HOST, IMAP_PORT, IMAP_SSL, IMAP_USERNAME, IMAP_PASSWORD, IMAP_CONNECT_TIMEOUT
Dependency
- Sent-sync mode requires
imapclient:
python3 -m pip install imapclient
Error Handling
- Invalid env config exits with code
2.
- Send failure exits with code
1.
- If sync is required (
--sent-sync-required or SMTP_SYNC_SENT_REQUIRED=true), sync failure exits with code 1.
References
Assets
assets/config.example.env
Scripts
1---2name: email-smtp-send3description: Send emails through SMTP with optional local attachments and optional IMAP APPEND sync to Sent mailbox. Use when tasks need reliable outbound email delivery, attachment sending, SMTP connectivity checks, or cross-client sent-mail visibility (for example appending to "Sent Items" after SMTP send).4---5
6# Email SMTP Send
7
8## Core Goal
9- Send outbound email via SMTP with env-configured credentials.
10- Attach local files into MIME email payload when requested.
11- Optionally append the sent message to IMAP sent mailbox for cross-client visibility.
12- Validate SMTP and sent-sync configuration before delivery.
13- Return machine-readable JSON status/error output.
14
15## Workflow
161. Configure SMTP env vars (see `references/env.md` and `assets/config.example.env`).
172. Optional: configure IMAP sent-sync env vars and install `imapclient` when sync is enabled.
183. Validate configuration:
19
20```bash
21python3 scripts/smtp_send.py check-config
22```
23
244. Send one email:
25
26```bash
27python3 scripts/smtp_send.py send \
28 --to recipient@example.com \
29 --subject "SMTP test" \
30 --body "Hello from email-smtp-send"
31```
32
335. Send with attachments:
34
35```bash
36python3 scripts/smtp_send.py send \
37 --to recipient@example.com \
38 --subject "Package delivery" \
39 --body "See attachments." \
40 --attach ./report.pdf \
41 --attach ./appendix.xlsx
42```
43
446. Send and sync to sent mailbox:
45
46```bash
47python3 scripts/smtp_send.py send \
48 --to recipient@example.com \
49 --subject "Synced send" \
50 --body "This message will be appended to Sent Items." \
51 --sync-sent \
52 --sent-mailbox "Sent Items"
53```
54
55## Output Contract
56- `check-config` prints sanitized SMTP config + defaults + sent-sync config JSON.
57- `send` success prints one `type=status` JSON object containing:
58 - `event=smtp_sent`
59 - sender, recipient summary, subject, SMTP host/port
60 - `message_id`
61 - `attachment_count` and `attachments[]` metadata
62 - `sent_sync` object with `enabled`, `required`, `appended`, and sync metadata/error
63- `send` failures print `type=error` JSON to stderr with one of:
64 - `event=smtp_send_invalid_args`
65 - `event=smtp_send_failed`
66 - `event=smtp_sent_sync_failed` (only when sync is required and sync fails)
67
68## Parameters
69- `send --to`: required recipient, repeatable or comma-separated.
70- `send --cc`: optional CC recipients.
71- `send --bcc`: optional BCC recipients.
72- `send --subject`: optional subject (defaults from env).
73- `send --body`: optional body (defaults from env).
74- `send --content-type`: `plain` or `html`.
75- `send --from`: optional sender override.
76- `send --attach`: optional local attachment path, repeatable or comma-separated.
77- `send --max-attachment-bytes`: max bytes allowed per attachment.
78- `send --message-id`: optional Message-ID header.
79- `send --in-reply-to`: optional In-Reply-To header.
80- `send --references`: optional References header.
81- `send --sync-sent|--no-sync-sent`: force-enable/disable IMAP sent sync for this send.
82- `send --sent-mailbox`: override sent mailbox.
83- `send --sent-flags`: IMAP APPEND flags, comma-separated (default `\Seen`).
84- `send --sent-sync-required`: return non-zero if SMTP succeeds but sent sync fails.
85
86Environment defaults:
87- `SMTP_HOST`, `SMTP_PORT`, `SMTP_SSL`, `SMTP_STARTTLS`
88- `SMTP_USERNAME`, `SMTP_PASSWORD`, `SMTP_FROM`, `SMTP_CONNECT_TIMEOUT`
89- `SMTP_SUBJECT`, `SMTP_BODY`, `SMTP_CONTENT_TYPE`
90- `SMTP_MAX_ATTACHMENT_BYTES`
91- `SMTP_SYNC_SENT`, `SMTP_SYNC_SENT_REQUIRED`
92- `SMTP_SENT_IMAP_HOST`, `SMTP_SENT_IMAP_PORT`, `SMTP_SENT_IMAP_SSL`
93- `SMTP_SENT_IMAP_USERNAME`, `SMTP_SENT_IMAP_PASSWORD`
94- `SMTP_SENT_IMAP_MAILBOX`, `SMTP_SENT_IMAP_FLAGS`, `SMTP_SENT_IMAP_CONNECT_TIMEOUT`
95- compatibility fallbacks: `IMAP_HOST`, `IMAP_PORT`, `IMAP_SSL`, `IMAP_USERNAME`, `IMAP_PASSWORD`, `IMAP_CONNECT_TIMEOUT`
96
97## Dependency
98- Sent-sync mode requires `imapclient`:
99
100```bash
101python3 -m pip install imapclient
102```
103
104## Error Handling
105- Invalid env config exits with code `2`.
106- Send failure exits with code `1`.
107- If sync is required (`--sent-sync-required` or `SMTP_SYNC_SENT_REQUIRED=true`), sync failure exits with code `1`.
108
109## References
110- `references/env.md`
111
112## Assets
113- `assets/config.example.env`
114
115## Scripts
116- `scripts/smtp_send.py`