Added UI to test email settings

This commit is contained in:
Bertrand Le Roy
2014-05-07 19:58:51 -07:00
parent 12230a8312
commit d5c762f51f
3 changed files with 117 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Orchard.Email.Services;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.UI.Admin;
namespace Orchard.Email.Controllers {
[Admin]
public class EmailAdminController : Controller {
private readonly ISmtpChannel _smtpChannel;
public EmailAdminController(ISmtpChannel smtpChannel) {
_smtpChannel = smtpChannel;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public ActionResult TestMailSettings(string to, string subject, string body) {
ILogger logger = null;
try {
var fakeLogger = new FakeLogger();
var smtpChannelComponent = _smtpChannel as Component;
if (smtpChannelComponent != null) {
logger = smtpChannelComponent.Logger;
smtpChannelComponent.Logger = fakeLogger;
}
_smtpChannel.Process(new Dictionary<string, object> {
{"Recipients", to},
{"Subject", subject},
{"Body", body}
});
if (!string.IsNullOrEmpty(fakeLogger.Message)) {
return Json(new { error = fakeLogger.Message });
}
return Json(new {status = T("Message sent").Text});
}
catch (Exception e) {
return Json(new {error = e.Message});
}
finally {
var smtpChannelComponent = _smtpChannel as Component;
if (smtpChannelComponent != null) {
smtpChannelComponent.Logger = logger;
}
}
}
private class FakeLogger : ILogger {
public string Message { get; set; }
public bool IsEnabled(LogLevel level) {
return true;
}
public void Log(LogLevel level, Exception exception, string format, params object[] args) {
Message = exception == null ? format : exception.Message;
}
}
}
}