Adding better error handling for SMTP server

JSBeautify sending_profiles.js
pull/284/head
Jordan Wright 2016-05-30 14:53:32 -05:00
parent 8211116375
commit 1933eb7ff1
3 changed files with 53 additions and 31 deletions

View File

@ -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()

View File

@ -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
}

View File

@ -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('<i class="fa fa-spinner fa-spin"></i> Sending')
// Send the test email
// Send the test email
api.send_test_email(test_email_request)
.success(function(data) {
$("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-success\">\
.success(function(data) {
$("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-success\">\
<i class=\"fa fa-check-circle\"></i> Email Sent!</div>")
$("#sendTestModalSubmit").html(btnHtml)
})
.error(function(data) {
$("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-danger\">\
$("#sendTestModalSubmit").html(btnHtml)
})
.error(function(data) {
$("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-danger\">\
<i class=\"fa fa-exclamation-circle\"></i> " + data.responseJSON.message + "</div>")
$("#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'),
"<div class='pull-right'><span data-toggle='modal' data-target='#modal'><button class='btn btn-primary' data-toggle='tooltip' data-placement='left' title='Edit Profile' onclick='edit(" + i + ")'>\
<i class='fa fa-pencil'></i>\
@ -203,7 +206,7 @@ $(document).ready(function() {
}, this));
};
$('#modal').on('hidden.bs.modal', function(event) {
dismiss()
dismiss()
});
load()
})