package main import ( "biukop.com/sfm/loan" "fmt" "github.com/brianvoe/gofakeit/v6" log "github.com/sirupsen/logrus" "net/http" "net/smtp" "strings" ) func apiV1EmailPassword(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"email-password/"):] //remove prefix u := loan.User{} e := u.Read(id) if e != nil { apiV1Client403Error(w, r, ss) return } newPass := gofakeit.Password(true, true, true, false, false, 8) log.Info("newPass", u, newPass) u.SetPass(newPass) e = u.Write() if e != nil { apiV1Client403Error(w, r, ss) return } sendEmailPassLink(u, u.Login, newPass) apiV1SendJson(true, w, r, ss) } func sendEmailPassLink(user loan.User, email string, pass string) { // Sender data. from := "mailer@biukop.com.au" password := "hpfitsrujgkewcdw" // Receiver email address. to := []string{ "patrick@biukop.com.au", email, } // smtp server configuration. smtpHost := "smtp.gmail.com" smtpPort := "587" raw := `Subject: Password Reset Dear {user}, We have reset your password to {pass}. Please logon to https://sfmarkets.com.au/broker to check your new credentials. There is no need to reply this mail. If you encounter any difficulties, please contact SuperFinance market directly. This is an automated email, and no one is monitoring this mailbox, please do not reply. Kind Regards Biukop Mailing service team. ` raw = strings.Replace(raw, "{user}", user.Display, -1) raw = strings.Replace(raw, "{pass}", pass, -1) // Message. message := []byte(raw) // Authentication. auth := smtp.PlainAuth("", from, password, smtpHost) // Sending email. err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message) if err != nil { fmt.Println(err) return } fmt.Println("Email Sent Successfully!") }