Files
mailx/examples/README.md
T
2026-08-15 01:38:05 +08:00

51 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# mailx 使用示例
本目录包含多个由浅入深的示例,方便快速接入。
## 示例总览
| 示例 | 难度 | 说明 |
| --- | --- | --- |
| [`quickstart`](./quickstart) | 入门 | 最简 SMTP 发送,逐行注释,适合第一次接触 |
| [`basic`](./basic) | 入门 | 单通道直接发送,含附件/抄送/回复地址 |
| [`manager`](./manager) | 入门 | 多通道管理器:注册、路由、默认通道、临时指定配置 |
| [`with_env`](./with_env) | 进阶 | 用环境变量管理多通道凭据,避免密钥写死在代码里 |
| [`advanced`](./advanced) | 进阶 | 主备切换、实例遍历、日志注入、超时、内嵌图片、错误分类 |
| [`custom_sender`](./custom_sender) | 进阶 | 实现自定义通道 + 自定义 Logger |
## 快速开始(5 分钟)
```bash
# 1. 进入最简示例
cd examples/quickstart
# 2. 打开 main.go,把里面的 4 个 SMTP 配置改成你自己的
# (QQ 邮箱需先开启 SMTP 服务并获取授权码)
# 3. 运行
go run main.go
```
看到 `send success` 即表示发送成功。
## 运行方式
所有示例都在 mailx 主模块内,直接在示例目录执行 `go run .` 即可:
```bash
# 示例:运行多通道管理器示例
cd examples/manager
go run .
# 示例:运行环境变量示例(先配置好 .env)
cd examples/with_env
go run .
```
## 注意事项
1. **凭据**:所有示例中的密钥都是占位符,运行前必须替换为真实值
2. **SMTP 授权码**:QQ/163 等邮箱不是用邮箱登录密码,而是需要在邮箱后台开启 SMTP 服务后生成的授权码
3. **AWS/阿里云**:需要先完成服务开通、域名验证(发件地址需为已验证身份)
4. **`.env`**`with_env` 示例依赖 `github.com/joho/godotenv`,首次运行前执行 `go get github.com/joho/godotenv``with_env/.env` 含敏感信息,勿提交 git