mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-08 00:56:58 +03:00
Improved error message when -email-from is empty while sending mail
This commit is contained in:
parent
360ae9e4b9
commit
d6e674bcf6
2 changed files with 30 additions and 0 deletions
|
@ -8,6 +8,26 @@ import (
|
||||||
mail "github.com/xhit/go-simple-mail/v2"
|
mail "github.com/xhit/go-simple-mail/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SmtpMailErrorCode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
EmptyEmailFrom SmtpMailErrorCode = 1 + iota
|
||||||
|
)
|
||||||
|
|
||||||
|
type SmtpMailError struct {
|
||||||
|
Msg string
|
||||||
|
Code SmtpMailErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err SmtpMailError) Error() string {
|
||||||
|
switch err.Code {
|
||||||
|
case EmptyEmailFrom:
|
||||||
|
return "-email-from is not specified or is empty.\nCheck the usage with --help."
|
||||||
|
default:
|
||||||
|
panic("Not Implemented")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type SmtpMail struct {
|
type SmtpMail struct {
|
||||||
hostname string
|
hostname string
|
||||||
port int
|
port int
|
||||||
|
@ -65,6 +85,10 @@ func (o *SmtpMail) Send(toName string, to string, subject string, content string
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if o.from == "" {
|
||||||
|
return SmtpMailError{Msg: "", Code: EmptyEmailFrom}
|
||||||
|
}
|
||||||
|
|
||||||
email := mail.NewMSG()
|
email := mail.NewMSG()
|
||||||
email.SetFrom(addressField(o.from, o.fromName)).
|
email.SetFrom(addressField(o.from, o.fromName)).
|
||||||
AddTo(addressField(to, toName)).
|
AddTo(addressField(to, toName)).
|
||||||
|
|
|
@ -275,6 +275,12 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
_, isMailError := err.(emailer.SmtpMailError)
|
||||||
|
if isMailError {
|
||||||
|
fmt.Println("SmtpMailError", err)
|
||||||
|
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "An error occurred while sending mail. Contact your administrator."})
|
||||||
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()})
|
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue