refactor: optimize email
This commit is contained in:
2
main.go
2
main.go
@@ -16,9 +16,9 @@ import (
|
|||||||
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
"gitea.com/texpixel/document_ai/internal/storage/dao"
|
||||||
"gitea.com/texpixel/document_ai/pkg/common"
|
"gitea.com/texpixel/document_ai/pkg/common"
|
||||||
"gitea.com/texpixel/document_ai/pkg/cors"
|
"gitea.com/texpixel/document_ai/pkg/cors"
|
||||||
|
"gitea.com/texpixel/document_ai/pkg/email"
|
||||||
"gitea.com/texpixel/document_ai/pkg/log"
|
"gitea.com/texpixel/document_ai/pkg/log"
|
||||||
"gitea.com/texpixel/document_ai/pkg/middleware"
|
"gitea.com/texpixel/document_ai/pkg/middleware"
|
||||||
"gitea.com/texpixel/document_ai/pkg/email"
|
|
||||||
"gitea.com/texpixel/document_ai/pkg/sms"
|
"gitea.com/texpixel/document_ai/pkg/sms"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/mail"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"gitea.com/texpixel/document_ai/config"
|
"gitea.com/texpixel/document_ai/config"
|
||||||
@@ -36,17 +39,18 @@ func InitEmailClient() *Client {
|
|||||||
|
|
||||||
// Send routes the email to the appropriate provider based on the recipient domain.
|
// Send routes the email to the appropriate provider based on the recipient domain.
|
||||||
func Send(ctx context.Context, to, subject, body string) error {
|
func Send(ctx context.Context, to, subject, body string) error {
|
||||||
|
if client == nil {
|
||||||
|
return fmt.Errorf("email client not initialized, call InitEmailClient first")
|
||||||
|
}
|
||||||
return client.Send(ctx, to, subject, body)
|
return client.Send(ctx, to, subject, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Send(ctx context.Context, to, subject, body string) error {
|
func (c *Client) Send(ctx context.Context, to, subject, body string) error {
|
||||||
atIdx := len(to) - 1
|
if _, err := mail.ParseAddress(to); err != nil {
|
||||||
for i, ch := range to {
|
return fmt.Errorf("invalid email address %q: %w", to, err)
|
||||||
if ch == '@' {
|
|
||||||
atIdx = i
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
domain := to[atIdx:]
|
|
||||||
|
domain := to[strings.LastIndex(to, "@")+1:]
|
||||||
if chineseDomainRe.MatchString(domain) {
|
if chineseDomainRe.MatchString(domain) {
|
||||||
return c.sendViaAliyunSMTP(ctx, to, subject, body)
|
return c.sendViaAliyunSMTP(ctx, to, subject, body)
|
||||||
}
|
}
|
||||||
@@ -66,6 +70,7 @@ func (c *Client) sendViaAliyunSMTP(ctx context.Context, to, subject, body string
|
|||||||
|
|
||||||
smtpClient, err := smtp.NewClient(conn, cfg.Host)
|
smtpClient, err := smtp.NewClient(conn, cfg.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
log.Error(ctx, "func", "sendViaAliyunSMTP", "msg", "smtp new client failed", "error", err)
|
log.Error(ctx, "func", "sendViaAliyunSMTP", "msg", "smtp new client failed", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -133,8 +138,9 @@ func (c *Client) sendViaResend(ctx context.Context, to, subject, body string) er
|
|||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||||
log.Error(ctx, "func", "sendViaResend", "msg", "resend api returned non-2xx", "status", resp.StatusCode, "to", to)
|
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
|
||||||
return fmt.Errorf("resend api returned status: %d", resp.StatusCode)
|
log.Error(ctx, "func", "sendViaResend", "msg", "resend api returned non-2xx", "status", resp.StatusCode, "to", to, "body", string(respBody))
|
||||||
|
return fmt.Errorf("resend api returned status %d: %s", resp.StatusCode, string(respBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info(ctx, "func", "sendViaResend", "msg", "email sent via resend", "to", to)
|
log.Info(ctx, "func", "sendViaResend", "msg", "email sent via resend", "to", to)
|
||||||
|
|||||||
Reference in New Issue
Block a user