// URL: /forgot_password // 忘记密码,输入用户名和邮箱,如果匹配,发出邮件 func forgotPasswordHandler(handler *Handler) { form := wtforms.NewForm( wtforms.NewTextField("username", "用户名", "", wtforms.Required{}), wtforms.NewTextField("email", "电子邮件", "", wtforms.Email{}), ) if handler.Request.Method == "POST" { if form.Validate(handler.Request) { var user User c := handler.DB.C(USERS) err := c.Find(bson.M{"username": form.Value("username")}).One(&user) if err != nil { form.AddError("username", "没有该用户") } else if user.Email != form.Value("email") { form.AddError("username", "用户名和邮件不匹配") } else { message2 := `Hi %s,<br> 我们的系统收到一个请求,说你希望通过电子邮件重新设置你在 Golang中国 的密码。你可以点击下面的链接开始重设密码: <a href="%s/reset/%s">%s/reset/%s</a><br> 如果这个请求不是由你发起的,那没问题,你不用担心,你可以安全地忽略这封邮件。 如果你有任何疑问,可以回复<a href="mailto:[email protected]">[email protected]</a>向我提问。` code := strings.Replace(uuid.NewUUID().String(), "-", "", -1) c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"resetcode": code}}) message2 = fmt.Sprintf(message2, user.Username, Config.Host, code, Config.Host, code) if Config.SendMailPath == "" { webhelpers.SendMail( "[Golang中国]重设密码", message2, Config.FromEmail, []string{user.Email}, webhelpers.SmtpConfig{ Username: Config.SmtpUsername, Password: Config.SmtpPassword, Host: Config.SmtpHost, Addr: Config.SmtpAddr, }, true, ) } else { webhelpers.SendMailExec( "[Golang中国]重设密码", message2, Config.FromEmail, []string{user.Email}, Config.SendMailPath, true, ) } message(handler, "通过电子邮件重设密码", "一封包含了重设密码指令的邮件已经发送到你的注册邮箱,按照邮件中的提示,即可重设你的密码。", "success") return } } } handler.renderTemplate("account/forgot_password.html", BASE, map[string]interface{}{"form": form}) }
func main() { c := gopher.DB.C("users") var users []gopher.User c.Find(nil).All(&users) smtpConfig := webhelpers.SmtpConfig{ Username: gopher.Config.SmtpUsername, Password: gopher.Config.SmtpPassword, Host: gopher.Config.SmtpHost, Addr: gopher.Config.SmtpAddr, } for _, user := range users { subject := "Golang中国域名改为golangtc.com" message := user.Username + ",您好!\n\n由于golang.tc域名被Godaddy没收,现已不可继续使用,现在开始使用golangtc.com域名。希望继续参与到社区建设中来。\n\n Golang中国" err := webhelpers.SendMail(subject, message, gopher.Config.FromEmail, []string{user.Email}, smtpConfig, false) if err != nil { fmt.Println("error: ", err.Error()) } else { fmt.Println("send to:", user.Username, user.Email) } } }