// basic 展示单个通道直接发送的完整用法。 // // 功能:用 SMTP 发送一封含 HTML 正文、抄送、附件、内嵌图片的邮件。 // // 怎么运行: // 1. 把下方 smtp.Config 的 4 个配置改成你自己的 // 2. 在项目根目录执行:go run ./examples/basic package main import ( "context" "fmt" "code.yun.ink/pkg/mailx" "code.yun.ink/pkg/mailx/smtp" ) func main() { ctx := context.Background() // ===== 第 1 步:创建 SMTP 通道 ===== client := smtp.New(smtp.Config{ Host: "smtp.qq.com", // SMTP 服务器地址 Port: 587, // 端口:465=SSL,587/25=STARTTLS User: "sender@qq.com", // 账号 Password: "your-auth-code", // 授权码(非邮箱密码) From: "sender@qq.com", // 默认发件人(可选,Message.From 未设置时使用) ReplyTo: "sender@qq.com", // 默认回复地址(可选) }) // ===== 第 2 步:构建消息 ===== // 地址都可带显示名,如 "张三" ;收件人/抄送/密送可传多个 msg := mailx.NewMessage(). From(`"客服中心" `). // 发件人(带显示名) To("receiver@example.com"). // 收件人 Cc("manager@example.com"). // 抄送(可选) Bcc("leader@example.com"). // 密送(可选) Subject("hello from mailx"). Text("如果邮件客户端不支持 HTML,会显示这段纯文本。"). // 纯文本正文(推荐) HTML(`

Hello

This email is sent by mailx.

`). ReplyTo("support@example.com"). // 回复地址(可选,覆盖 Config.ReplyTo) Attach("report.txt"). // 按路径添加附件(路径需真实存在) AttachBytes("summary.txt", []byte("summary content")). // 内存附件 InlineImageBytes("logo1", "logo.png", mustPNG()). // 内嵌图片(HTML 中 src="cid:logo1") Build() // ===== 第 3 步:发送 ===== if err := client.Send(ctx, msg); err != nil { fmt.Println("send failed:", err) return } fmt.Println("send success") } // mustPNG 返回一个极小的 1x1 PNG(1 像素透明图)用于演示内嵌图片。 // 实际使用中请替换为真实的图片文件路径或字节内容。 func mustPNG() []byte { // 一个合法的 1x1 透明 PNG return []byte{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x15, 0xc4, 0x89, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0d, 0x0a, 0x2d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, } }