From 80fc04924d212261a41d9be99b877e1b47090ebc Mon Sep 17 00:00:00 2001 From: William Woodson Date: Sat, 27 Feb 2016 08:32:10 -0600 Subject: [PATCH] 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 --- controllers/api.go | 60 ++++++++++++++++++++++++++++++++-------------- models/campaign.go | 4 ---- models/template.go | 2 +- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index c9f7d0a8..6db158f3 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -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) return } - // 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 + + // If a Template is not specified use a default + if s.Template.Name == "" { + //default message body + text := "It works!\n\nThis is an email letting you know that your gophish\nconfiguration was successful.\n" + + "Here are the details:\n\nWho you sent from: {{.From}}\n\nWho you sent to: \n" + + "{{if .FirstName}} First Name: {{.FirstName}}\n{{end}}" + + "{{if .LastName}} Last Name: {{.LastName}}\n{{end}}" + + "{{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 err == gorm.RecordNotFound { - Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name) - JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest) - } else if err != nil { - Logger.Println(err) - JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) - return + + // If a complete sending profile is provided use it + if err := s.SMTP.Validate(); err != nil { + // Otherwise get the SMTP requested by name + s.SMTP, err = models.GetSMTPByName(s.SMTP.Name, ctx.Get(r, "user_id").(int64)) + if err == gorm.RecordNotFound { + Logger.Printf("Error - Sending profile %s does not exist", s.SMTP.Name) + JSONResponse(w, models.Response{Success: false, Message: models.ErrSMTPNotFound.Error()}, http.StatusBadRequest) + } else if err != nil { + Logger.Println(err) + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) + return + } } + // Send the test email err = worker.SendTestEmail(s) if err != nil { diff --git a/models/campaign.go b/models/campaign.go index 9f976830..4775276d 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -89,12 +89,8 @@ type SendTestEmailRequest struct { // is valid. func (s *SendTestEmailRequest) Validate() error { switch { - case s.Template.Name == "": - return ErrTemplateNotSpecified case s.Email == "": return ErrEmailNotSpecified - case s.SMTP.Name == "": - return ErrSMTPNotSpecified } return nil } diff --git a/models/template.go b/models/template.go index c6027cc6..d1daa4b1 100644 --- a/models/template.go +++ b/models/template.go @@ -17,7 +17,7 @@ type Template struct { HTML string `json:"html" gorm:"column:html"` ModifiedDate time.Time `json:"modified_date"` Attachments []Attachment `json:"attachments"` -} +} // ErrTemplateNameNotSpecified is thrown when a template name is not specified var ErrTemplateNameNotSpecified = errors.New("Template name not specified")