调整SetOptions的方式
This commit is contained in:
@@ -15,7 +15,7 @@ func ExampleUsage() {
|
||||
smtpClient := NewSmtp()
|
||||
|
||||
// 配置SMTP设置
|
||||
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.gmail.com",
|
||||
Port: "587", // STARTTLS
|
||||
@@ -56,7 +56,7 @@ func ExampleSSLUsage() {
|
||||
|
||||
smtpClient := NewSmtp()
|
||||
|
||||
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.gmail.com",
|
||||
Port: "465", // SSL
|
||||
@@ -87,7 +87,7 @@ func ExampleEnterpriseEmail() {
|
||||
smtpClient := NewSmtp()
|
||||
|
||||
// 企业邮箱通常使用587端口和STARTTLS
|
||||
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.exmail.qq.com", // 腾讯企业邮箱
|
||||
Port: "587",
|
||||
|
||||
@@ -148,7 +148,7 @@ func TestMessageValidation2(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 配置SMTP客户端
|
||||
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.example.com",
|
||||
Port: "587",
|
||||
@@ -226,7 +226,7 @@ func TestMultipartEmailConstruction2(t *testing.T) {
|
||||
smtpClient := NewSmtp()
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err := smtpClient.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.example.com",
|
||||
Port: "587",
|
||||
|
||||
+10
-8
@@ -44,18 +44,18 @@ func NewSmtp() *Smtp {
|
||||
return smtp
|
||||
}
|
||||
|
||||
func (l *Smtp) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
|
||||
func (l *Smtp) SetOption(ctx context.Context, opt ...interfaces.Option) error {
|
||||
for _, o := range opt {
|
||||
o(&l.Options)
|
||||
}
|
||||
|
||||
if l.Options.Smtp == nil {
|
||||
return nil, fmt.Errorf("SMTP configuration is required")
|
||||
return fmt.Errorf("SMTP configuration is required")
|
||||
}
|
||||
|
||||
// 验证配置
|
||||
if err := l.validateConfig(); err != nil {
|
||||
return nil, fmt.Errorf("invalid SMTP config: %w", err)
|
||||
return fmt.Errorf("invalid SMTP config: %w", err)
|
||||
}
|
||||
|
||||
// 初始化认证
|
||||
@@ -65,11 +65,13 @@ func (l *Smtp) SetOption(ctx context.Context, opt ...interfaces.Option) (interfa
|
||||
l.Options.Logger.Infof(ctx, "SMTP configured - Host:%s Port:%s Username:%s",
|
||||
l.Options.Smtp.Host, l.Options.Smtp.Port, l.Options.Smtp.Username)
|
||||
|
||||
return l, nil
|
||||
l.IsSet = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Smtp) Send(ctx context.Context, message interfaces.Message) error {
|
||||
if l.Options.Smtp == nil {
|
||||
if !l.IsSet {
|
||||
return fmt.Errorf("SMTP not initialized")
|
||||
}
|
||||
|
||||
@@ -441,19 +443,19 @@ func (l *Smtp) writeBody(buffer *bytes.Buffer, boundary string, message interfac
|
||||
altBoundary := "alt-" + boundary
|
||||
buffer.WriteString(fmt.Sprintf("--%s\r\n", boundary))
|
||||
buffer.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=%s\r\n\r\n", altBoundary))
|
||||
|
||||
|
||||
// 纯文本版本
|
||||
buffer.WriteString(fmt.Sprintf("--%s\r\n", altBoundary))
|
||||
buffer.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
|
||||
buffer.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
|
||||
buffer.WriteString(message.TextBody + "\r\n")
|
||||
|
||||
|
||||
// HTML版本
|
||||
buffer.WriteString(fmt.Sprintf("--%s\r\n", altBoundary))
|
||||
buffer.WriteString("Content-Type: text/html; charset=utf-8\r\n")
|
||||
buffer.WriteString("Content-Transfer-Encoding: quoted-printable\r\n\r\n")
|
||||
buffer.WriteString(message.Body + "\r\n")
|
||||
|
||||
|
||||
buffer.WriteString(fmt.Sprintf("--%s--\r\n", altBoundary))
|
||||
} else {
|
||||
// 单一内容类型
|
||||
|
||||
@@ -15,13 +15,13 @@ func TestSmtpEnhanced(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 测试空配置
|
||||
_, err := smtp.SetOption(ctx)
|
||||
err := smtp.SetOption(ctx)
|
||||
if err == nil {
|
||||
t.Error("Expected error for empty config")
|
||||
}
|
||||
|
||||
// 测试不完整配置
|
||||
_, err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.example.com",
|
||||
// 缺少用户名和密码
|
||||
@@ -32,7 +32,7 @@ func TestSmtpEnhanced(t *testing.T) {
|
||||
}
|
||||
|
||||
// 测试完整配置
|
||||
_, err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
||||
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
||||
Host: "smtp.example.com",
|
||||
Port: "587",
|
||||
|
||||
+2
-5
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestMail(t *testing.T) {
|
||||
sm := smtp.NewSmtp()
|
||||
ini := smtp.NewSmtp()
|
||||
ctx := context.Background()
|
||||
|
||||
// ini, err := sm.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||
@@ -24,8 +24,7 @@ func TestMail(t *testing.T) {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
|
||||
|
||||
ini, err := sm.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||
err := ini.SetOption(ctx, interfaces.SetSmtp(&interfaces.EmailConfigDataSmtp{
|
||||
IsSSL: true,
|
||||
Username: "demo@yunink.net",
|
||||
Password: "aAhVPzHydLoc9Fj8",
|
||||
@@ -37,8 +36,6 @@ func TestMail(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// req, err := http.Get("https://baidu.com")
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user