123 lines
3.0 KiB
Go
123 lines
3.0 KiB
Go
package smtp
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"code.yun.ink/pkg/mailx/interfaces"
|
|
)
|
|
|
|
// 使用示例
|
|
func ExampleUsage() {
|
|
ctx := context.Background()
|
|
|
|
// 创建SMTP实例
|
|
smtpClient := NewSmtp()
|
|
|
|
// 配置SMTP设置
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "587", // STARTTLS
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
IsSSL: false, // 使用STARTTLS而不是直接SSL
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("Failed to configure SMTP: %v", err)
|
|
}
|
|
|
|
// 构建邮件消息
|
|
message := interfaces.Message{
|
|
Form: "sender@example.com",
|
|
To: []string{"recipient1@example.com", "recipient2@example.com"},
|
|
Cc: []string{"cc@example.com"},
|
|
Bcc: []string{"bcc@example.com"},
|
|
Subject: "测试邮件 - Enhanced SMTP",
|
|
Body: "<h1>这是一封测试邮件</h1><p>支持HTML格式和多种功能。</p>",
|
|
ReplyTo: "noreply@example.com",
|
|
Attachment: []interfaces.Attachment{
|
|
{Content: "path/to/attachment.pdf"},
|
|
},
|
|
}
|
|
|
|
// 发送邮件
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("Failed to send email: %v", err)
|
|
}
|
|
|
|
log.Println("Email sent successfully!")
|
|
}
|
|
|
|
// SSL方式示例(465端口)
|
|
func ExampleSSLUsage() {
|
|
ctx := context.Background()
|
|
|
|
smtpClient := NewSmtp()
|
|
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.gmail.com",
|
|
Port: "465", // SSL
|
|
Username: "your-email@gmail.com",
|
|
Password: "your-app-password",
|
|
IsSSL: true,
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("Failed to configure SMTP: %v", err)
|
|
}
|
|
|
|
message := interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "SSL邮件测试",
|
|
Body: "这是通过SSL连接发送的邮件",
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("Failed to send email: %v", err)
|
|
}
|
|
}
|
|
|
|
// 企业邮箱示例
|
|
func ExampleEnterpriseEmail() {
|
|
ctx := context.Background()
|
|
|
|
smtpClient := NewSmtp()
|
|
|
|
// 企业邮箱通常使用587端口和STARTTLS
|
|
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.exmail.qq.com", // 腾讯企业邮箱
|
|
Port: "587",
|
|
Username: "admin@yourcompany.com",
|
|
Password: "your-password",
|
|
IsSSL: false,
|
|
}
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("Failed to configure SMTP: %v", err)
|
|
}
|
|
|
|
message := interfaces.Message{
|
|
Form: "系统通知 <admin@yourcompany.com>",
|
|
To: []string{"employee@yourcompany.com"},
|
|
Subject: "系统维护通知",
|
|
Body: `
|
|
<div style="font-family: Arial, sans-serif;">
|
|
<h2>系统维护通知</h2>
|
|
<p>尊敬的用户:</p>
|
|
<p>我们将于今晚进行系统维护,预计维护时间为2小时。</p>
|
|
<p>维护期间系统将暂停服务,给您带来的不便敬请谅解。</p>
|
|
<br>
|
|
<p>技术团队</p>
|
|
</div>
|
|
`,
|
|
}
|
|
|
|
if err := smtpClient.Send(ctx, message); err != nil {
|
|
log.Fatalf("Failed to send email: %v", err)
|
|
}
|
|
}
|