mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Improved SMTP test function to take settings into account in real time.
Before this change the user had to first save the settings before being able to test them.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Mvc;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Data;
|
||||
using Orchard.Email.Models;
|
||||
using Orchard.Email.Services;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
@@ -10,15 +13,20 @@ namespace Orchard.Email.Controllers {
|
||||
[Admin]
|
||||
public class EmailAdminController : Controller {
|
||||
private readonly ISmtpChannel _smtpChannel;
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly ITransactionManager _transactionManager;
|
||||
|
||||
public EmailAdminController(ISmtpChannel smtpChannel) {
|
||||
public EmailAdminController(ISmtpChannel smtpChannel, IOrchardServices orchardServices, ITransactionManager transactionManager) {
|
||||
_smtpChannel = smtpChannel;
|
||||
_orchardServices = orchardServices;
|
||||
_transactionManager = transactionManager;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public ActionResult TestMailSettings(string to, string subject, string body, string replyTo, string bcc, string cc) {
|
||||
[HttpPost]
|
||||
public ActionResult TestSettings(TestSmtpSettings testSettings) {
|
||||
ILogger logger = null;
|
||||
try {
|
||||
var fakeLogger = new FakeLogger();
|
||||
@@ -27,18 +35,37 @@ namespace Orchard.Email.Controllers {
|
||||
logger = smtpChannelComponent.Logger;
|
||||
smtpChannelComponent.Logger = fakeLogger;
|
||||
}
|
||||
_smtpChannel.Process(new Dictionary<string, object> {
|
||||
{"Recipients", to},
|
||||
{"Subject", subject},
|
||||
{"Body", body},
|
||||
{"ReplyTo",replyTo},
|
||||
{"Bcc", bcc},
|
||||
{"CC",cc}
|
||||
});
|
||||
if (!string.IsNullOrEmpty(fakeLogger.Message)) {
|
||||
|
||||
// Temporarily update settings so that the test will actually use the specified host, port, etc.
|
||||
var smtpSettings = _orchardServices.WorkContext.CurrentSite.As<SmtpSettingsPart>();
|
||||
|
||||
smtpSettings.Address = testSettings.From;
|
||||
smtpSettings.Host = testSettings.Host;
|
||||
smtpSettings.Port = testSettings.Port;
|
||||
smtpSettings.EnableSsl = testSettings.EnableSsl;
|
||||
smtpSettings.RequireCredentials = testSettings.RequireCredentials;
|
||||
smtpSettings.UserName = testSettings.UserName;
|
||||
smtpSettings.Password = testSettings.Password;
|
||||
|
||||
if (!smtpSettings.IsValid()) {
|
||||
fakeLogger.Error("Invalid settings.");
|
||||
}
|
||||
else {
|
||||
_smtpChannel.Process(new Dictionary<string, object> {
|
||||
{"Recipients", testSettings.To},
|
||||
{"Subject", testSettings.Subject},
|
||||
{"Body", testSettings.Body},
|
||||
{"ReplyTo", testSettings.ReplyTo},
|
||||
{"Bcc", testSettings.Bcc},
|
||||
{"CC", testSettings.Cc}
|
||||
});
|
||||
}
|
||||
|
||||
if (!String.IsNullOrEmpty(fakeLogger.Message)) {
|
||||
return Json(new { error = fakeLogger.Message });
|
||||
}
|
||||
return Json(new {status = T("Message sent").Text});
|
||||
|
||||
return Json(new {status = T("Message sent.").Text});
|
||||
}
|
||||
catch (Exception e) {
|
||||
return Json(new {error = e.Message});
|
||||
@@ -48,6 +75,9 @@ namespace Orchard.Email.Controllers {
|
||||
if (smtpChannelComponent != null) {
|
||||
smtpChannelComponent.Logger = logger;
|
||||
}
|
||||
|
||||
// Undo the temporarily changed smtp settings.
|
||||
_transactionManager.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,5 +92,21 @@ namespace Orchard.Email.Controllers {
|
||||
Message = exception == null ? format : exception.Message;
|
||||
}
|
||||
}
|
||||
|
||||
public class TestSmtpSettings {
|
||||
public string From { get; set; }
|
||||
public string ReplyTo { get; set; }
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; }
|
||||
public bool EnableSsl { get; set; }
|
||||
public bool RequireCredentials { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string To { get; set; }
|
||||
public string Cc { get; set; }
|
||||
public string Bcc { get; set; }
|
||||
public string Subject { get; set; }
|
||||
public string Body { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,7 +1,5 @@
|
||||
using System.ComponentModel;
|
||||
using System.Configuration;
|
||||
using System.Configuration;
|
||||
using System.Net.Configuration;
|
||||
using System.Net.Mail;
|
||||
using Orchard.ContentManagement;
|
||||
using System;
|
||||
using Orchard.ContentManagement.Utilities;
|
||||
|
@@ -64,14 +64,14 @@
|
||||
<label for="emailtestcc">@T("CC:")</label>
|
||||
<input type="text" id="emailtestcc" class="large text" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="emailtestsubject">@T("Subject:")</label>
|
||||
<input type="text" id="emailtestsubject" class="large text" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="emailtestreplyto">@T("Reply To:")</label>
|
||||
<input type="text" id="emailtestreplyto" class="large text" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="emailtestsubject">@T("Subject:")</label>
|
||||
<input type="text" id="emailtestsubject" class="large text" />
|
||||
</div>
|
||||
<div>
|
||||
<textarea id="emailtestbody"></textarea>
|
||||
</div>
|
||||
@@ -82,10 +82,17 @@
|
||||
<div id="emailtestinfo" class="message-Information" style="display:none;"></div>
|
||||
@using (Script.Foot()) {
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
var url = "@Url.Action("TestMailSettings", "EmailAdmin", new {area = "Orchard.Email"})",
|
||||
$(function () {
|
||||
var url = "@Url.Action("TestSettings", "EmailAdmin", new {area = "Orchard.Email"})",
|
||||
error = $("#emailtesterror"),
|
||||
info = $("#emailtestinfo"),
|
||||
from = $("#@Html.FieldIdFor(m => m.Address)"),
|
||||
host = $("#@Html.FieldIdFor(m => m.Host)"),
|
||||
port = $("#@Html.FieldIdFor(m => m.Port)"),
|
||||
enableSsl = $("#@Html.FieldIdFor(m => m.EnableSsl)"),
|
||||
requireCredentials = $("#@Html.FieldIdFor(m => m.RequireCredentials)"),
|
||||
userName = $("#@Html.FieldIdFor(m => m.UserName)"),
|
||||
password = $("#@Html.FieldIdFor(m => m.Password)"),
|
||||
to = $("#emailtestto"),
|
||||
subject = $("#emailtestsubject"),
|
||||
body = $("#emailtestbody"),
|
||||
@@ -93,29 +100,36 @@
|
||||
bcc = $("#emailtestbcc"),
|
||||
cc = $("#emailtestcc");
|
||||
|
||||
$("#emailtestsend").click(function() {
|
||||
$("#emailtestsend").click(function () {
|
||||
$.post(url, {
|
||||
to: to.val(),
|
||||
subject: subject.val(),
|
||||
body: body.val(),
|
||||
replyto: replyto.val(),
|
||||
bcc: bcc.val(),
|
||||
cc: cc.val(),
|
||||
__RequestVerificationToken: to.closest("form").find("input[name=__RequestVerificationToken]").val()
|
||||
})
|
||||
.fail(function(data) {
|
||||
error.html(data.statusText).show();
|
||||
from: from.val(),
|
||||
host: host.val(),
|
||||
port: port.val(),
|
||||
enableSsl: enableSsl.val(),
|
||||
requireCredentials: requireCredentials.val(),
|
||||
userName: userName.val(),
|
||||
password: password.val(),
|
||||
to: to.val(),
|
||||
subject: subject.val(),
|
||||
body: body.val(),
|
||||
replyto: replyto.val(),
|
||||
bcc: bcc.val(),
|
||||
cc: cc.val(),
|
||||
__RequestVerificationToken: to.closest("form").find("input[name=__RequestVerificationToken]").val()
|
||||
})
|
||||
.fail(function (data) {
|
||||
error.html(data.statusText).show();
|
||||
info.hide();
|
||||
})
|
||||
.done(function (data) {
|
||||
if (data.error) {
|
||||
error.html(data.error).show();
|
||||
info.hide();
|
||||
})
|
||||
.done(function(data) {
|
||||
if (data.error) {
|
||||
error.html(data.error).show();
|
||||
info.hide();
|
||||
} else {
|
||||
info.html(data.status).show();
|
||||
error.hide();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
info.html(data.status).show();
|
||||
error.hide();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user