github.com/merlinepedra/gopphish-attack@v0.9.0/controllers/api/util.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 ctx "github.com/gophish/gophish/context" 8 log "github.com/gophish/gophish/logger" 9 "github.com/gophish/gophish/models" 10 "github.com/jinzhu/gorm" 11 "github.com/sirupsen/logrus" 12 ) 13 14 // SendTestEmail sends a test email using the template name 15 // and Target given. 16 func (as *Server) SendTestEmail(w http.ResponseWriter, r *http.Request) { 17 s := &models.EmailRequest{ 18 ErrorChan: make(chan error), 19 UserId: ctx.Get(r, "user_id").(int64), 20 } 21 if r.Method != "POST" { 22 JSONResponse(w, models.Response{Success: false, Message: "Method not allowed"}, http.StatusBadRequest) 23 return 24 } 25 err := json.NewDecoder(r.Body).Decode(s) 26 if err != nil { 27 JSONResponse(w, models.Response{Success: false, Message: "Error decoding JSON Request"}, http.StatusBadRequest) 28 return 29 } 30 31 storeRequest := false 32 33 // If a Template is not specified use a default 34 if s.Template.Name == "" { 35 //default message body 36 text := "It works!\n\nThis is an email letting you know that your gophish\nconfiguration was successful.\n" + 37 "Here are the details:\n\nWho you sent from: {{.From}}\n\nWho you sent to: \n" + 38 "{{if .FirstName}} First Name: {{.FirstName}}\n{{end}}" + 39 "{{if .LastName}} Last Name: {{.LastName}}\n{{end}}" + 40 "{{if .Position}} Position: {{.Position}}\n{{end}}" + 41 "\nNow go send some phish!" 42 t := models.Template{ 43 Subject: "Default Email from Gophish", 44 Text: text, 45 } 46 s.Template = t 47 } else { 48 // Get the Template requested by name 49 s.Template, err = models.GetTemplateByName(s.Template.Name, s.UserId) 50 if err == gorm.ErrRecordNotFound { 51 log.WithFields(logrus.Fields{ 52 "template": s.Template.Name, 53 }).Error("Template does not exist") 54 JSONResponse(w, models.Response{Success: false, Message: models.ErrTemplateNotFound.Error()}, http.StatusBadRequest) 55 return 56 } else if err != nil { 57 log.Error(err) 58 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 59 return 60 } 61 s.TemplateId = s.Template.Id 62 // We'll only save the test request to the database if there is a 63 // user-specified template to use. 64 storeRequest = true 65 } 66 67 if s.Page.Name != "" { 68 s.Page, err = models.GetPageByName(s.Page.Name, s.UserId) 69 if err == gorm.ErrRecordNotFound { 70 log.WithFields(logrus.Fields{ 71 "page": s.Page.Name, 72 }).Error("Page does not exist") 73 JSONResponse(w, models.Response{Success: false, Message: models.ErrPageNotFound.Error()}, http.StatusBadRequest) 74 return 75 } else if err != nil { 76 log.Error(err) 77 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 78 return 79 } 80 s.PageId = s.Page.Id 81 } 82 83 // If a complete sending profile is provided use it 84 if err := s.SMTP.Validate(); err != nil { 85 // Otherwise get the SMTP requested by name 86 smtp, lookupErr := models.GetSMTPByName(s.SMTP.Name, s.UserId) 87 // If the Sending Profile doesn't exist, let's err on the side 88 // of caution and assume that the validation failure was more important. 89 if lookupErr != nil { 90 log.Error(err) 91 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 92 return 93 } 94 s.SMTP = smtp 95 } 96 s.FromAddress = s.SMTP.FromAddress 97 98 // Validate the given request 99 if err = s.Validate(); err != nil { 100 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 101 return 102 } 103 104 // Store the request if this wasn't the default template 105 if storeRequest { 106 err = models.PostEmailRequest(s) 107 if err != nil { 108 log.Error(err) 109 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 110 return 111 } 112 } 113 // Send the test email 114 err = as.worker.SendTestEmail(s) 115 if err != nil { 116 log.Error(err) 117 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 118 return 119 } 120 JSONResponse(w, models.Response{Success: true, Message: "Email Sent"}, http.StatusOK) 121 return 122 }