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() err = s.Validate()
if err != nil { 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 return
} }
s.ModifiedDate = time.Now() s.ModifiedDate = time.Now()

View File

@ -3,6 +3,8 @@ package models
import ( import (
"errors" "errors"
"net/mail" "net/mail"
"strconv"
"strings"
"time" "time"
) )
@ -28,6 +30,9 @@ var ErrFromAddressNotSpecified = errors.New("No From Address specified")
// in the SMTP configuration // in the SMTP configuration
var ErrHostNotSpecified = errors.New("No SMTP Host specified") 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 // TableName specifies the database tablename for Gorm to use
func (s SMTP) TableName() string { func (s SMTP) TableName() string {
return "smtp" return "smtp"
@ -42,6 +47,20 @@ func (s *SMTP) Validate() error {
return ErrHostNotSpecified return ErrHostNotSpecified
} }
_, err := mail.ParseAddress(s.FromAddress) _, 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 return err
} }

View File

@ -3,34 +3,34 @@ var profiles = []
// Attempts to send a test email by POSTing to /campaigns/ // Attempts to send a test email by POSTing to /campaigns/
function sendTestEmail() { function sendTestEmail() {
var test_email_request = { var test_email_request = {
template: {}, template: {},
first_name: $("input[name=to_first_name]").val(), first_name: $("input[name=to_first_name]").val(),
last_name: $("input[name=to_last_name]").val(), last_name: $("input[name=to_last_name]").val(),
email: $("input[name=to_email]").val(), email: $("input[name=to_email]").val(),
position: $("input[name=to_position]").val(), position: $("input[name=to_position]").val(),
url: '', url: '',
smtp: { smtp: {
from_address: $("#from").val(), from_address: $("#from").val(),
host: $("#host").val(), host: $("#host").val(),
username: $("#username").val(), username: $("#username").val(),
password: $("#password").val(), password: $("#password").val(),
ignore_cert_errors: $("#ignore_cert_errors").prop("checked") ignore_cert_errors: $("#ignore_cert_errors").prop("checked")
} }
} }
btnHtml = $("#sendTestModalSubmit").html() btnHtml = $("#sendTestModalSubmit").html()
$("#sendTestModalSubmit").html('<i class="fa fa-spinner fa-spin"></i> Sending') $("#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) api.send_test_email(test_email_request)
.success(function(data) { .success(function(data) {
$("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-success\">\ $("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-success\">\
<i class=\"fa fa-check-circle\"></i> Email Sent!</div>") <i class=\"fa fa-check-circle\"></i> Email Sent!</div>")
$("#sendTestModalSubmit").html(btnHtml) $("#sendTestModalSubmit").html(btnHtml)
}) })
.error(function(data) { .error(function(data) {
$("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-danger\">\ $("#sendTestEmailModal\\.flashes").empty().append("<div style=\"text-align:center\" class=\"alert alert-danger\">\
<i class=\"fa fa-exclamation-circle\"></i> " + data.responseJSON.message + "</div>") <i class=\"fa fa-exclamation-circle\"></i> " + data.responseJSON.message + "</div>")
$("#sendTestModalSubmit").html(btnHtml) $("#sendTestModalSubmit").html(btnHtml)
}) })
} }
// Save attempts to POST to /smtp/ // Save attempts to POST to /smtp/
@ -51,6 +51,9 @@ function save(idx) {
load() load()
dismiss() dismiss()
}) })
.error(function(data) {
modalError(data.responseJSON.message)
})
} else { } else {
// Submit the profile // Submit the profile
api.SMTP.post(profile) api.SMTP.post(profile)
@ -95,12 +98,12 @@ function edit(idx) {
if (idx != -1) { if (idx != -1) {
profile = profiles[idx] profile = profiles[idx]
$("#name").val(profile.name) $("#name").val(profile.name)
$("#interface_type").val(profile.interface_type) $("#interface_type").val(profile.interface_type)
$("#from").val(profile.from_address) $("#from").val(profile.from_address)
$("#host").val(profile.host) $("#host").val(profile.host)
$("#username").val(profile.username) $("#username").val(profile.username)
$("#password").val(profile.password) $("#password").val(profile.password)
$("#ignore_cert_errors").prop("checked", profile.ignore_cert_errors) $("#ignore_cert_errors").prop("checked", profile.ignore_cert_errors)
} }
} }
@ -140,7 +143,7 @@ function load() {
$.each(profiles, function(i, profile) { $.each(profiles, function(i, profile) {
profileTable.row.add([ profileTable.row.add([
profile.name, profile.name,
profile.interface_type, profile.interface_type,
moment(profile.modified_date).format('MMMM Do YYYY, h:mm:ss a'), 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 + ")'>\ "<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>\ <i class='fa fa-pencil'></i>\
@ -203,7 +206,7 @@ $(document).ready(function() {
}, this)); }, this));
}; };
$('#modal').on('hidden.bs.modal', function(event) { $('#modal').on('hidden.bs.modal', function(event) {
dismiss() dismiss()
}); });
load() load()
}) })