# sendmail **Repository Path**: wiseai/sendmail ## Basic Information - **Project Name**: sendmail - **Description**: 发送邮件 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-21 - **Last Updated**: 2026-04-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Sendmail 一个简洁的命令行邮件发送工具,支持 TLS 加密传输,配置文件默认嵌入到二进制文件中。 ## 特性 - 支持 TLS/STARTTLS 加密发送 - 支持 SMTP 认证 - 支持纯文本和 HTML 邮件内容 - 支持附件发送 - 配置文件嵌入二进制,无需额外配置即可运行 - 命令行参数优先,灵活覆盖配置 ## 安装 ### 从源码编译 ```bash # 克隆仓库 git clone https://github.com/your-repo/sendmail.git cd sendmail # 编译当前平台 go build -o sendmail . # 或使用构建脚本编译多架构 chmod +x build.sh ./build.sh ``` ### 下载预编译版本 从 [Release](https://github.com/your-repo/sendmail/releases) 页面下载对应平台的二进制文件。 ## 快速开始 ### 1. 配置(可选) 将 `config.example.json` 复制为 `config.json` 并修改配置: ```json { 'from': 'your-email@example.com', 'smtp': 'smtp.example.com:465', 'user': 'your-username', 'pass': 'your-password' } ``` ### 2. 发送邮件 ```bash # 发送纯文本邮件 ./sendmail -to user@example.com -subject 'Hello' -body 'World' # 发送 HTML 邮件(用单引号包裹,无需转义特殊字符) ./sendmail -to user@example.com -subject 'Hello' -html '

Hello

World

' ``` ## 命令行参数 | 参数 | 说明 | 默认值 | |------|------|--------| | -from | 发件人邮箱 | (嵌入配置) | | -to | 收件人邮箱 | (必填) | | -subject | 邮件主题 | (必填) | | -body | 邮件纯文本正文 | '' | | -html | 邮件 HTML 正文 | '' | | -smtp | SMTP 服务器地址 | (嵌入配置) | | -user | SMTP 用户名 | (嵌入配置) | | -pass | SMTP 密码 | (嵌入配置) | | -attach | 附件文件路径 | - | | -tls | 使用 TLS 连接 | true | | -config | 配置文件路径 | config.json | **优先级**: 命令行参数 > 嵌入配置 > 外部配置文件 ## 邮件内容格式 ### 支持的格式 | 格式 | 参数 | Content-Type | |------|------|--------------| | 纯文本 | -body | text/plain; charset=UTF-8 | | HTML | -html | text/html; charset=UTF-8 | ### 发送纯文本邮件 ```bash ./sendmail -to user@example.com -subject '测试' -body '这是一封纯文本邮件' ``` ### 发送 HTML 邮件 ```bash ./sendmail -to user@example.com -subject '测试' -html '

标题

内容

' ``` ### 发送多格式邮件 同时指定 `-body` 和 `-html` 时,会发送 multipart/alternative 邮件,客户端会自动选择合适格式显示。 ```bash ./sendmail -to user@example.com -subject '测试' -body '纯文本内容' -html '

HTML内容

' ``` ## 使用示例 ### 基本发送 ```bash ./sendmail -from sender@example.com -to user@example.com \ -subject '测试邮件' -body '这是一封测试邮件' ``` ### 发送 HTML 邮件 ```bash ./sendmail -from sender@example.com -to user@example.com \ -subject '月度报告' -html '

报告摘要

销售额1000
' ``` ### 发送带附件的邮件 ```bash ./sendmail -from sender@example.com -to user@example.com \ -subject '月度报告' -html '

请查收附件

' \ -attach /path/to/report.pdf ``` ### 禁用 TLS(不推荐) ```bash ./sendmail -from sender@example.com -to user@example.com \ -subject 'Test' -body 'Hello' -tls=false ``` ### 使用 QQ 邮箱 ```bash ./sendmail -from your-email@qq.com -to recipient@example.com \ -subject '测试' -html '

Hello

' \ -smtp smtp.qq.com:465 -user your-email@qq.com -pass your-auth-code ``` > 注意:QQ 邮箱需要使用授权码而非登录密码。 ### 使用 Gmail ```bash ./sendmail -from your-email@gmail.com -to recipient@example.com \ -subject '测试' -html '

Hello

' \ -smtp smtp.gmail.com:587 -user your-email@gmail.com -pass your-app-password ``` > 注意:Gmail 需要启用'应用专用密码'。 ## 配置文件说明 | 字段 | 说明 | 示例 | |------|------|------| | from | 发件人邮箱 | user@example.com | | smtp | SMTP 服务器地址 | smtp.example.com:465 | | user | SMTP 用户名 | user@example.com | | pass | SMTP 密码或授权码 | ******** | ### 常用 SMTP 服务器端口 | 服务商 | SMTPS (TLS) | STARTTLS | |--------|-------------|----------| | QQ 邮箱 | smtp.qq.com:465 | smtp.qq.com:587 | | Gmail | smtp.gmail.com:465 | smtp.gmail.com:587 | | 网易邮箱 | smtp.163.com:465 | smtp.163.com:587 | | 企业邮箱 | smtp.exmail.qq.com:465 | smtp.exmail.qq.com:587 | ## 构建多平台版本 ```bash # 编译 AMD64 和 ARM64 版本(带 UPX 压缩) ./build.sh # 输出目录 ls -la release/ ``` ## 依赖 - Go 1.21+ ## 许可证 MIT License --- ## 附录:Email 包函数详解 ### Email 结构体 Email 是邮件消息的核心类型,包含以下字段: | 字段 | 类型 | 说明 | |------|------|------| | From | string | 发件人地址 | | To | []string | 收件人列表 | | Cc | []string | 抄送人列表 | | Bcc | []string | 密送人列表 | | Subject | string | 邮件主题 | | Text | []byte | 纯文本正文 | | HTML | []byte | HTML 正文 | | Headers | textproto.MIMEHeader | 自定义头部 | | Attachments | []*Attachment | 附件列表 | ### NewEmail() ```go func NewEmail() *Email ``` 创建一个新的 Email 对象。 ### Attach() ```go func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) ``` 添加附件。 ### AttachFile() ```go func (e *Email) AttachFile(filename string) (a *Attachment, err error) ``` 添加文件附件,自动检测 Content-Type。 ### Bytes() ```go func (e *Email) Bytes() ([]byte, error) ``` 将 Email 对象转换为 RFC 2822 格式的字节数组。 ### Send() ```go func (e *Email) Send(addr string, a smtp.Auth) error ``` 通过 SMTP 发送邮件。 ### SendWithTLS() ```go func (e *Email) SendWithTLS(addr string, a smtp.Auth, t *tls.Config) error ``` 通过 TLS 直连发送邮件(适用于端口 465)。 ### SendWithStartTLS() ```go func (e *Email) SendWithStartTLS(addr string, a smtp.Auth, t *tls.Config) error ``` 通过 STARTTLS 发送邮件(适用于端口 587)。 ### 错误变量 | 变量 | 说明 | |------|------| | ErrMissingBoundary | 多部分实体缺少边界参数 | | ErrMissingContentType | MIME 实体缺少 Content-Type |