From e0f543121596fae4881a447255e0eb75cc9980ab Mon Sep 17 00:00:00 2001 From: Toby Chui Date: Sat, 27 Apr 2024 22:37:55 +0800 Subject: [PATCH] Fixed #129 - Removed requirements for Domain (now domain field can be empty and no error will be shown) --- src/emails.go | 6 +++--- src/mod/email/email.go | 26 +++++++++++++++++--------- src/web/components/utils.html | 4 +++- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/emails.go b/src/emails.go index 55fd604..9946d28 100644 --- a/src/emails.go +++ b/src/emails.go @@ -27,8 +27,8 @@ func HandleSMTPSet(w http.ResponseWriter, r *http.Request) { domain, err := utils.PostPara(r, "domain") if err != nil { - utils.SendErrorResponse(w, "domain cannot be empty") - return + //Assume domain is empty, raised by issue #129 + domain = "" } portString, err := utils.PostPara(r, "port") @@ -206,7 +206,7 @@ var ( ) func HandleAdminAccountResetEmail(w http.ResponseWriter, r *http.Request) { - if EmailSender.Username == "" || EmailSender.Domain == "" { + if EmailSender.Username == "" { //Reset account not setup utils.SendErrorResponse(w, "Reset account not setup.") return diff --git a/src/mod/email/email.go b/src/mod/email/email.go index 3191c7f..878b130 100644 --- a/src/mod/email/email.go +++ b/src/mod/email/email.go @@ -20,7 +20,7 @@ type Sender struct { SenderAddr string //e.g. admin@arozos.com } -//Create a new email sender object +// Create a new email sender object func NewEmailSender(hostname string, domain string, port int, username string, password string, senderAddr string) *Sender { return &Sender{ Hostname: hostname, @@ -33,13 +33,15 @@ func NewEmailSender(hostname string, domain string, port int, username string, p } /* - Send a email to a reciving addr - Example Usage: - SendEmail( - test@example.com, - "Free donuts", - "Come get your free donuts on this Sunday!" - ) +Send a email to a reciving addr +Example Usage: +SendEmail( + + test@example.com, + "Free donuts", + "Come get your free donuts on this Sunday!" + +) */ func (s *Sender) SendEmail(to string, subject string, content string) error { //Parse the email content @@ -50,7 +52,13 @@ func (s *Sender) SendEmail(to string, subject string, content string) error { content + "\n\n") //Login to the SMTP server - auth := smtp.PlainAuth("", s.Username+"@"+s.Domain, s.Password, s.Hostname) + var auth smtp.Auth + if s.Domain == "" { + auth = smtp.PlainAuth("", s.Username, s.Password, s.Hostname) + } else { + auth = smtp.PlainAuth("", s.Username+"@"+s.Domain, s.Password, s.Hostname) + } + err := smtp.SendMail(s.Hostname+":"+strconv.Itoa(s.Port), auth, s.SenderAddr, []string{to}, msg) if err != nil { return err diff --git a/src/web/components/utils.html b/src/web/components/utils.html index 9d84eab..e4dc346 100644 --- a/src/web/components/utils.html +++ b/src/web/components/utils.html @@ -345,7 +345,8 @@ form.find('input[name="hostname"]').parent().removeClass('error'); } - // validate domain + // validate domain, now allow empty string (for smtp that dont use domain as input) + /* const domain = form.find('input[name="domain"]').val().trim(); if (!domainRegex.test(domain)) { form.find('input[name="domain"]').parent().addClass('error'); @@ -353,6 +354,7 @@ } else { form.find('input[name="domain"]').parent().removeClass('error'); } + */ // validate username const username = form.find('input[name="username"]').val().trim();