diff --git a/controllers/api.go b/controllers/api.go index c0565a37..0ff02cfe 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -416,7 +416,7 @@ func API_SMTP_Id(w http.ResponseWriter, r *http.Request) { } err = s.Validate() if err != nil { - JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest) + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) return } s.ModifiedDate = time.Now() diff --git a/models/smtp.go b/models/smtp.go index 65bb7e82..298217a2 100644 --- a/models/smtp.go +++ b/models/smtp.go @@ -3,6 +3,8 @@ package models import ( "errors" "net/mail" + "strconv" + "strings" "time" ) @@ -28,6 +30,9 @@ var ErrFromAddressNotSpecified = errors.New("No From Address specified") // in the SMTP configuration var ErrHostNotSpecified = errors.New("No SMTP Host specified") +// ErrInvalidHost indicates that the SMTP server string is invalid +var ErrInvalidHost = errors.New("Invalid SMTP server address") + // TableName specifies the database tablename for Gorm to use func (s SMTP) TableName() string { return "smtp" @@ -42,6 +47,20 @@ func (s *SMTP) Validate() error { return ErrHostNotSpecified } _, err := mail.ParseAddress(s.FromAddress) + if err != nil { + return err + } + // Make sure addr is in host:port format + hp := strings.Split(s.Host, ":") + if len(hp) > 2 { + return ErrInvalidHost + } else if len(hp) < 2 { + hp = append(hp, "25") + } + _, err = strconv.Atoi(hp[1]) + if err != nil { + return ErrInvalidHost + } return err } diff --git a/static/js/app/sending_profiles.js b/static/js/app/sending_profiles.js index ff33a458..b6a33900 100644 --- a/static/js/app/sending_profiles.js +++ b/static/js/app/sending_profiles.js @@ -3,34 +3,34 @@ var profiles = [] // Attempts to send a test email by POSTing to /campaigns/ function sendTestEmail() { var test_email_request = { - template: {}, - first_name: $("input[name=to_first_name]").val(), - last_name: $("input[name=to_last_name]").val(), - email: $("input[name=to_email]").val(), - position: $("input[name=to_position]").val(), - url: '', - smtp: { - from_address: $("#from").val(), - host: $("#host").val(), - username: $("#username").val(), - password: $("#password").val(), - ignore_cert_errors: $("#ignore_cert_errors").prop("checked") - } + template: {}, + first_name: $("input[name=to_first_name]").val(), + last_name: $("input[name=to_last_name]").val(), + email: $("input[name=to_email]").val(), + position: $("input[name=to_position]").val(), + url: '', + smtp: { + from_address: $("#from").val(), + host: $("#host").val(), + username: $("#username").val(), + password: $("#password").val(), + ignore_cert_errors: $("#ignore_cert_errors").prop("checked") + } } btnHtml = $("#sendTestModalSubmit").html() $("#sendTestModalSubmit").html(' Sending') - // Send the test email + // Send the test email api.send_test_email(test_email_request) - .success(function(data) { - $("#sendTestEmailModal\\.flashes").empty().append("
\ + .success(function(data) { + $("#sendTestEmailModal\\.flashes").empty().append("
\ Email Sent!
") - $("#sendTestModalSubmit").html(btnHtml) - }) - .error(function(data) { - $("#sendTestEmailModal\\.flashes").empty().append("
\ + $("#sendTestModalSubmit").html(btnHtml) + }) + .error(function(data) { + $("#sendTestEmailModal\\.flashes").empty().append("
\ " + data.responseJSON.message + "
") - $("#sendTestModalSubmit").html(btnHtml) - }) + $("#sendTestModalSubmit").html(btnHtml) + }) } // Save attempts to POST to /smtp/ @@ -51,6 +51,9 @@ function save(idx) { load() dismiss() }) + .error(function(data) { + modalError(data.responseJSON.message) + }) } else { // Submit the profile api.SMTP.post(profile) @@ -95,12 +98,12 @@ function edit(idx) { if (idx != -1) { profile = profiles[idx] $("#name").val(profile.name) - $("#interface_type").val(profile.interface_type) - $("#from").val(profile.from_address) - $("#host").val(profile.host) - $("#username").val(profile.username) - $("#password").val(profile.password) - $("#ignore_cert_errors").prop("checked", profile.ignore_cert_errors) + $("#interface_type").val(profile.interface_type) + $("#from").val(profile.from_address) + $("#host").val(profile.host) + $("#username").val(profile.username) + $("#password").val(profile.password) + $("#ignore_cert_errors").prop("checked", profile.ignore_cert_errors) } } @@ -140,7 +143,7 @@ function load() { $.each(profiles, function(i, profile) { profileTable.row.add([ profile.name, - profile.interface_type, + profile.interface_type, moment(profile.modified_date).format('MMMM Do YYYY, h:mm:ss a'), "