Added handling to /util/send_test_email to use default on empty template and accept a validated SMTP object from send test email workflow on sending profiles page

pull/169/head
William Woodson 2016-02-27 08:32:10 -06:00
parent ffb14b7927
commit 80fc04924d
3 changed files with 43 additions and 23 deletions

View File

@ -525,26 +525,50 @@ func API_Send_Test_Email(w http.ResponseWriter, r *http.Request) {
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
return return
} }
// Get the template requested by name
s.Template, err = models.GetTemplateByName(s.Template.Name, ctx.Get(r, "user_id").(int64)) // If a Template is not specified use a default
if err == gorm.RecordNotFound { if s.Template.Name == "" {
Logger.Printf("Error - Template %s does not exist", s.Template.Name) //default message body
JSONResponse(w, models.Response{Success: false, Message: models.ErrTemplateNotFound.Error()}, http.StatusBadRequest) text := "It works!\n\nThis is an email letting you know that your gophish\nconfiguration was successful.\n" +
} else if err != nil { "Here are the details:\n\nWho you sent from: {{.From}}\n\nWho you sent to: \n" +
Logger.Println(err) "{{if .FirstName}} First Name: {{.FirstName}}\n{{end}}" +
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) "{{if .LastName}} Last Name: {{.LastName}}\n{{end}}" +
return "{{if .Position}} Position: {{.Position}}\n{{end}}" +
"{{if .TrackingURL}} Tracking URL: {{.TrackingURL}}\n{{end}}" +
"\nNow go send some phish!"
t := models.Template{
Subject: "Default Email from Gophish",
Text: text,
}
s.Template = t
// Try to lookup the Template by name
} else {
// Get the Template requested by name
s.Template, err = models.GetTemplateByName(s.Template.Name, ctx.Get(r, "user_id").(int64))
if err == gorm.RecordNotFound {
Logger.Printf("Error - Template %s does not exist", s.Template.Name)
JSONResponse(w, models.Response{Success: false, Message: models.ErrTemplateNotFound.Error()}, http.StatusBadRequest)
} else if err != nil {
Logger.Println(err)
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
return
}
} }
// Get the sending profile requested by name
s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64)) // If a complete sending profile is provided use it
if err == gorm.RecordNotFound { if err := s.SMTP.Validate(); err != nil {
Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name) // Otherwise get the SMTP requested by name
JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest) s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64))
} else if err != nil { if err == gorm.RecordNotFound {
Logger.Println(err) Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name)
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest)
return } else if err != nil {
Logger.Println(err)
JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
return
}
} }
// Send the test email // Send the test email
err = worker.SendTestEmail(s) err = worker.SendTestEmail(s)
if err != nil { if err != nil {

View File

@ -89,12 +89,8 @@ type SendTestEmailRequest struct {
// is valid. // is valid.
func (s *SendTestEmailRequest) Validate() error { func (s *SendTestEmailRequest) Validate() error {
switch { switch {
case s.Template.Name == "":
return ErrTemplateNotSpecified
case s.Email == "": case s.Email == "":
return ErrEmailNotSpecified return ErrEmailNotSpecified
case s.SMTP.Name == "":
return ErrSMTPNotSpecified
} }
return nil return nil
} }