diff --git a/emailer/smtp.go b/emailer/smtp.go index 38ac995..7935ff9 100644 --- a/emailer/smtp.go +++ b/emailer/smtp.go @@ -8,6 +8,26 @@ import ( 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 { hostname string port int @@ -65,6 +85,10 @@ func (o *SmtpMail) Send(toName string, to string, subject string, content string return err } + if o.from == "" { + return SmtpMailError{Msg: "", Code: EmptyEmailFrom} + } + email := mail.NewMSG() email.SetFrom(addressField(o.from, o.fromName)). AddTo(addressField(to, toName)). diff --git a/handler/routes.go b/handler/routes.go index da7ecd2..54a6a40 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -275,6 +275,12 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon ) 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()}) }