调整SetOptions的方式
This commit is contained in:
+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 {
|
||||
// 单一内容类型
|
||||
|
||||
Reference in New Issue
Block a user