示例#1
0
//修改密码
func LoginChangePasswd(w http.ResponseWriter, r *http.Request) {
	data := make(map[string]interface{})
	user := new(models.User)
	t, err := template.ParseFiles("views/login_passwd_change.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	l, err := template.ParseFiles("views/login.html", "views/common/head.tpl")
	if pageNotFound(w, err) {
		return
	}
	if r.Method == "GET" {
		emailCode := r.FormValue("emailCode")
		user.Email_change = emailCode
		has, _, _ := models.SelectUser(user)
		if has {
			data["emailCode"] = emailCode
			data["user"] = user
			data["error"] = ""
			t.Execute(w, data)
			return
		} else {
			data["error"] = "密码修改失败,请重试"
			l.Execute(w, data)
			return
		}

	} else {
		pwd := r.PostFormValue("pwd")
		user.Email_change = r.PostFormValue("emailCode")
		has, userInfo, _ := models.SelectUser(user)
		if has {
			id := userInfo.Id
			user = new(models.User)
			user.Passwd = GetMd5String(pwd)
			user.Email_change = GetMd5String(userInfo.Uname + userInfo.Email + userInfo.Passwd)
			err := models.UpdateUser(id, user)
			if CheckError("密码修改失败", err) {
				data["error"] = "密码修改失败,请重试"
				l.Execute(w, data)
				return
			} else {
				data["success"] = "密码修改成功,请登录"
				l.Execute(w, data)
				return
			}
		} else {
			data["error"] = "密码修改失败,请重试"
			l.Execute(w, data)
			return
		}
	}
}
示例#2
0
文件: base.go 项目: Zxnui/go_web_demo
//发送邮件
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
}