357 lines
9.2 KiB
Go
357 lines
9.2 KiB
Go
package mailx
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"code.yun.ink/pkg/mailx/interfaces"
|
|
"code.yun.ink/pkg/mailx/smtp"
|
|
)
|
|
|
|
// HTML邮件发送示例
|
|
func ExampleHTMLEmail() {
|
|
ctx := context.Background()
|
|
smtpClient := smtp.NewSmtp()
|
|
|
|
// 配置SMTP
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "587",
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("SMTP配置失败: %v", err)
|
|
}
|
|
|
|
// 创建HTML邮件
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "HTML邮件测试 - 包含图片和附件",
|
|
BodyType: interfaces.ContentTypeHTML,
|
|
Body: `
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>HTML邮件测试</title>
|
|
</head>
|
|
<body>
|
|
<h1 style="color: #333;">欢迎使用HTML邮件</h1>
|
|
<p>这是一封包含<strong>HTML格式</strong>的邮件。</p>
|
|
|
|
<h2>内嵌图片示例</h2>
|
|
<p>下面是一张内嵌图片:</p>
|
|
<img src="cid:logo" alt="Logo" style="width: 200px; height: auto;">
|
|
|
|
<h2>表格示例</h2>
|
|
<table border="1" style="border-collapse: collapse;">
|
|
<tr>
|
|
<th>姓名</th>
|
|
<th>邮箱</th>
|
|
<th>状态</th>
|
|
</tr>
|
|
<tr>
|
|
<td>张三</td>
|
|
<td>zhangsan@example.com</td>
|
|
<td style="color: green;">已激活</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>如果您无法查看此邮件,请<a href="https://example.com">点击这里</a>。</p>
|
|
</body>
|
|
</html>
|
|
`,
|
|
TextBody: `
|
|
欢迎使用HTML邮件
|
|
|
|
这是一封包含HTML格式的邮件。
|
|
|
|
内嵌图片示例
|
|
下面是一张内嵌图片:[图片: Logo]
|
|
|
|
表格示例
|
|
姓名 邮箱 状态
|
|
张三 zhangsan@example.com 已激活
|
|
|
|
如果您无法查看此邮件,请访问: https://example.com
|
|
`,
|
|
InlineImage: []interfaces.Attachment{
|
|
{
|
|
Content: "/path/to/logo.png",
|
|
ContentType: "image/png",
|
|
Name: "logo.png",
|
|
CID: "logo", // 对应HTML中的cid:logo
|
|
Type: interfaces.AttachmentTypeInline,
|
|
},
|
|
},
|
|
Attachment: []interfaces.Attachment{
|
|
{
|
|
Content: "/path/to/document.pdf",
|
|
ContentType: "application/pdf",
|
|
Name: "重要文档.pdf",
|
|
Type: interfaces.AttachmentTypeFile,
|
|
},
|
|
},
|
|
}
|
|
|
|
// 发送邮件
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("邮件发送失败: %v", err)
|
|
}
|
|
|
|
fmt.Println("HTML邮件发送成功!")
|
|
}
|
|
|
|
// 纯文本邮件示例
|
|
func ExampleTextEmail() {
|
|
ctx := context.Background()
|
|
smtpClient := smtp.NewSmtp()
|
|
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "587",
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("SMTP配置失败: %v", err)
|
|
}
|
|
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "纯文本邮件测试",
|
|
BodyType: interfaces.ContentTypeText,
|
|
Body: `
|
|
欢迎使用邮件服务
|
|
|
|
这是一封纯文本邮件,不包含任何HTML格式。
|
|
|
|
主要特点:
|
|
- 简洁明了
|
|
- 兼容性好
|
|
- 加载速度快
|
|
|
|
如有疑问,请回复此邮件。
|
|
`,
|
|
Attachment: []interfaces.Attachment{
|
|
{
|
|
Content: "/path/to/readme.txt",
|
|
Name: "说明文档.txt",
|
|
Type: interfaces.AttachmentTypeFile,
|
|
},
|
|
},
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("邮件发送失败: %v", err)
|
|
}
|
|
|
|
fmt.Println("纯文本邮件发送成功!")
|
|
}
|
|
|
|
// 自动检测内容类型示例
|
|
func ExampleAutoDetectContentType() {
|
|
ctx := context.Background()
|
|
smtpClient := smtp.NewSmtp()
|
|
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "587",
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("SMTP配置失败: %v", err)
|
|
}
|
|
|
|
// 自动检测为HTML
|
|
htmlMessage := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "自动检测HTML邮件",
|
|
BodyType: interfaces.ContentTypeAuto, // 自动检测
|
|
Body: "<h1>这会被自动识别为HTML</h1><p>因为包含HTML标签。</p>",
|
|
}
|
|
|
|
// 自动检测为纯文本
|
|
textMessage := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "自动检测纯文本邮件",
|
|
BodyType: interfaces.ContentTypeAuto, // 自动检测
|
|
Body: "这会被自动识别为纯文本,因为不包含HTML标签。",
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, htmlMessage); err != nil {
|
|
log.Printf("HTML邮件发送失败: %v", err)
|
|
} else {
|
|
fmt.Println("自动检测HTML邮件发送成功!")
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, textMessage); err != nil {
|
|
log.Printf("文本邮件发送失败: %v", err)
|
|
} else {
|
|
fmt.Println("自动检测文本邮件发送成功!")
|
|
}
|
|
}
|
|
|
|
// 使用字节数据作为附件示例
|
|
func ExampleByteDataAttachment() {
|
|
ctx := context.Background()
|
|
smtpClient := smtp.NewSmtp()
|
|
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "587",
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("SMTP配置失败: %v", err)
|
|
}
|
|
|
|
// 动态生成的CSV数据
|
|
csvData := `姓名,邮箱,部门
|
|
张三,zhangsan@example.com,技术部
|
|
李四,lisi@example.com,市场部
|
|
王五,wangwu@example.com,人事部`
|
|
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "动态生成的附件",
|
|
BodyType: interfaces.ContentTypeHTML,
|
|
Body: "<h1>员工名单</h1><p>请查看附件中的详细信息。</p>",
|
|
Attachment: []interfaces.Attachment{
|
|
{
|
|
Data: []byte(csvData),
|
|
ContentType: "text/csv",
|
|
Name: "员工名单.csv",
|
|
Type: interfaces.AttachmentTypeFile,
|
|
},
|
|
},
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("邮件发送失败: %v", err)
|
|
}
|
|
|
|
fmt.Println("包含动态附件的邮件发送成功!")
|
|
}
|
|
|
|
// 复杂HTML邮件示例(包含多个内嵌图片)
|
|
func ExampleComplexHTMLEmail() {
|
|
ctx := context.Background()
|
|
smtpClient := smtp.NewSmtp()
|
|
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "587",
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("SMTP配置失败: %v", err)
|
|
}
|
|
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "复杂HTML邮件 - 多图片和附件",
|
|
BodyType: interfaces.ContentTypeHTML,
|
|
Body: `
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<style>
|
|
body { font-family: Arial, sans-serif; }
|
|
.header { background-color: #f0f0f0; padding: 20px; }
|
|
.content { padding: 20px; }
|
|
.footer { background-color: #333; color: white; padding: 10px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="header">
|
|
<img src="cid:header_logo" alt="公司Logo" style="height: 50px;">
|
|
<h1>月度报告</h1>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<h2>销售数据</h2>
|
|
<p>本月销售情况如下图所示:</p>
|
|
<img src="cid:sales_chart" alt="销售图表" style="width: 100%; max-width: 600px;">
|
|
|
|
<h2>用户增长</h2>
|
|
<p>用户增长趋势:</p>
|
|
<img src="cid:user_growth" alt="用户增长图" style="width: 100%; max-width: 600px;">
|
|
</div>
|
|
|
|
<div class="footer">
|
|
<p>详细数据请查看附件。如有疑问,请联系我们。</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`,
|
|
TextBody: `
|
|
月度报告
|
|
|
|
销售数据
|
|
本月销售情况请查看附件中的图表。
|
|
|
|
用户增长
|
|
用户增长趋势请查看附件中的图表。
|
|
|
|
详细数据请查看附件。如有疑问,请联系我们。
|
|
`,
|
|
InlineImage: []interfaces.Attachment{
|
|
{
|
|
Content: "/path/to/header_logo.png",
|
|
ContentType: "image/png",
|
|
Name: "header_logo.png",
|
|
CID: "header_logo",
|
|
Type: interfaces.AttachmentTypeInline,
|
|
},
|
|
{
|
|
Content: "/path/to/sales_chart.jpg",
|
|
ContentType: "image/jpeg",
|
|
Name: "sales_chart.jpg",
|
|
CID: "sales_chart",
|
|
Type: interfaces.AttachmentTypeInline,
|
|
},
|
|
{
|
|
Content: "/path/to/user_growth.png",
|
|
ContentType: "image/png",
|
|
Name: "user_growth.png",
|
|
CID: "user_growth",
|
|
Type: interfaces.AttachmentTypeInline,
|
|
},
|
|
},
|
|
Attachment: []interfaces.Attachment{
|
|
{
|
|
Content: "/path/to/detailed_report.xlsx",
|
|
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
Name: "详细报告.xlsx",
|
|
Type: interfaces.AttachmentTypeFile,
|
|
},
|
|
{
|
|
Content: "/path/to/summary.pdf",
|
|
ContentType: "application/pdf",
|
|
Name: "摘要报告.pdf",
|
|
Type: interfaces.AttachmentTypeFile,
|
|
},
|
|
},
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("邮件发送失败: %v", err)
|
|
}
|
|
|
|
fmt.Println("复杂HTML邮件发送成功!")
|
|
} |