Fearure:
- 增加收件箱功能(有BUG) - 增加已发送|草稿箱|发送历史 本地记录
This commit is contained in:
50
email.go
50
email.go
@@ -40,6 +40,39 @@ func (m Model) sendEmailCmd() tea.Cmd {
|
||||
to := strings.Split(m.To.Value(), ToSeparator)
|
||||
cc := strings.Split(m.Cc.Value(), ToSeparator)
|
||||
bcc := strings.Split(m.Bcc.Value(), ToSeparator)
|
||||
|
||||
deliveryMethod := "smtp"
|
||||
if m.DeliveryMethod == Resend {
|
||||
deliveryMethod = "resend"
|
||||
}
|
||||
|
||||
toStr := m.To.Value()
|
||||
ccStr := m.Cc.Value()
|
||||
bccStr := m.Bcc.Value()
|
||||
bodyText := m.Body.Value()
|
||||
|
||||
html := bytes.NewBufferString("")
|
||||
goldmark.Convert([]byte(bodyText), html)
|
||||
bodyHTML := html.String()
|
||||
|
||||
attachmentsJSON := getAttachmentsJSON(attachments)
|
||||
|
||||
emailID, saveErr := SaveEmail(
|
||||
m.From.Value(),
|
||||
toStr,
|
||||
ccStr,
|
||||
bccStr,
|
||||
m.Subject.Value(),
|
||||
bodyText,
|
||||
bodyHTML,
|
||||
attachmentsJSON,
|
||||
deliveryMethod,
|
||||
StatusDraft,
|
||||
)
|
||||
if saveErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to save email to history: %v\n", saveErr)
|
||||
}
|
||||
|
||||
switch m.DeliveryMethod {
|
||||
case SMTP:
|
||||
err = sendSMTPEmail(to, cc, bcc, m.From.Value(), m.Subject.Value(), m.Body.Value(), attachments)
|
||||
@@ -55,6 +88,13 @@ func (m Model) sendEmailCmd() tea.Cmd {
|
||||
}
|
||||
return sendEmailFailureMsg(err)
|
||||
}
|
||||
|
||||
if emailID > 0 {
|
||||
if updateErr := UpdateEmailStatus(emailID, StatusSent); updateErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to update email status: %v\n", updateErr)
|
||||
}
|
||||
}
|
||||
|
||||
return sendEmailSuccessMsg{}
|
||||
}
|
||||
}
|
||||
@@ -64,6 +104,8 @@ const gmailSMTPHost = "smtp.gmail.com"
|
||||
const gmailSMTPPort = 587
|
||||
|
||||
func sendSMTPEmail(to, cc, bcc []string, from, subject, body string, attachments []string) error {
|
||||
validFrom := getValidFromAddress(from, smtpUsername)
|
||||
|
||||
server := mail.NewSMTPClient()
|
||||
|
||||
var err error
|
||||
@@ -92,8 +134,8 @@ func sendSMTPEmail(to, cc, bcc []string, from, subject, body string, attachments
|
||||
}
|
||||
|
||||
server.KeepAlive = false
|
||||
server.ConnectTimeout = 10 * time.Second
|
||||
server.SendTimeout = 10 * time.Second
|
||||
server.ConnectTimeout = 30 * time.Second
|
||||
server.SendTimeout = 30 * time.Second
|
||||
server.TLSConfig = &tls.Config{
|
||||
InsecureSkipVerify: smtpInsecureSkipVerify, //nolint:gosec
|
||||
ServerName: server.Host,
|
||||
@@ -102,11 +144,11 @@ func sendSMTPEmail(to, cc, bcc []string, from, subject, body string, attachments
|
||||
smtpClient, err := server.Connect()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("SMTP连接失败: %w", err)
|
||||
}
|
||||
|
||||
email := mail.NewMSG()
|
||||
email.SetFrom(from).
|
||||
email.SetFrom(validFrom).
|
||||
AddTo(to...).
|
||||
AddCc(cc...).
|
||||
AddBcc(bcc...).
|
||||
|
||||
Reference in New Issue
Block a user