//保存登录数据 func LoginPost(w http.ResponseWriter, r *http.Request) { submit := r.PostFormValue("submit") if strings.EqualFold(submit, "1") { http.Redirect(w, r, "/loginInfo", http.StatusFound) return } data := make(map[string]string) login, err := template.ParseFiles("views/login.html", "views/common/head.tpl") if pageNotFound(w, err) { return } uname := r.PostFormValue("uname") pwd := r.PostFormValue("pwd") checkbox := r.PostFormValue("autoLogin") == "on" user, has, err := models.GetUserInfo(uname) _ = CheckError("查询用户失败:", err) if has { if strings.EqualFold(user.Passwd, GetMd5String(pwd)) { if checkbox { newCookie := http.Cookie{ Name: http.CanonicalHeaderKey("uname"), Value: uname, MaxAge: 1<<31 - 1, } http.SetCookie(w, &newCookie) } else { newCookie := http.Cookie{ Name: http.CanonicalHeaderKey("uname"), Value: uname, MaxAge: 0, } http.SetCookie(w, &newCookie) } http.Redirect(w, r, "/", http.StatusFound) return } else { data["error"] = "密码错误" data["uname"] = uname login.Execute(w, data) return } } else { data["error"] = "用户不存在" login.Execute(w, data) return } }
//发送邮件 func SendToMail(toEmail, name string) error { b64 := base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") email, _ := conf.Cfg.GetValue("email", "email") password, _ := conf.Cfg.GetValue("email", "passwd") host, _ := conf.Cfg.GetValue("email", "host") subject, _ := conf.Cfg.GetValue("email", "subject") sendName, _ := conf.Cfg.GetValue("email", "sendName") from := mail.Address{sendName, email} to := mail.Address{name, toEmail} hp := strings.Split(host, ":") auth := smtp.PlainAuth("", email, password, hp[0]) header := make(map[string]string) header["From"] = from.String() header["To"] = to.String() header["Subject"] = fmt.Sprintf("=?UTF-8?B?%s?=", b64.EncodeToString([]byte(subject))) header["MIME-Version"] = "1.0" header["Content-Type"] = "text/html; charset=UTF-8" header["Content-Transfer-Encoding"] = "base64" //获取用户信息 userInfo, _, _ := models.GetUserInfo(name) EmailMd5 := GetMd5String(toEmail + name + userInfo.Passwd) newUser := new(models.User) newUser.Email_change = EmailMd5 err := models.UpdateUser(userInfo.Id, newUser) if CheckError("数据库读取失败", err) { return err } body := `<strong>修改密码</strong> <p>访问以下网址:<a href="http://` + conf.HttpWeb + `/login/changePasswd?emailCode=` + EmailMd5 + `">http://` + conf.HttpWeb + `/login/changePasswd?emailCode=` + EmailMd5 + `<a><p> <p>如果以上链接无法访问,请将该网址复制并粘贴至新的浏览器窗口中。</p>` message := "" for k, v := range header { message += fmt.Sprintf("%s: %s\r\n", k, v) } message += "\r\n" + b64.EncodeToString([]byte(body)) send_to := strings.Split(toEmail, ";") err = smtp.SendMail(host, auth, email, send_to, []byte(message)) return err }