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

View File

@@ -71,6 +71,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Activities\EmailActivity.cs" /> <Compile Include="Activities\EmailActivity.cs" />
<Compile Include="Controllers\EmailAdminController.cs" />
<Compile Include="Forms\MailForms.cs" /> <Compile Include="Forms\MailForms.cs" />
<Compile Include="Forms\EmailForm.cs" /> <Compile Include="Forms\EmailForm.cs" />
<Compile Include="Migrations.cs" /> <Compile Include="Migrations.cs" />
@@ -132,6 +133,7 @@
<ItemGroup> <ItemGroup>
<Content Include="Views\Template.Smtp.Wrapper.cshtml" /> <Content Include="Views\Template.Smtp.Wrapper.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup />
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -52,4 +52,56 @@
<span class="hint">@T("The password for authentication.")</span> <span class="hint">@T("The password for authentication.")</span>
</div> </div>
</div> </div>
</fieldset>
<fieldset>
<legend>@T("Test those settings:")</legend>
<div>
<label for="emailtestto">@T("To:")</label>
<input type="text" id="emailtestto" 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>
<div>
<button type="button" id="emailtestsend" class="button grey">@T("Send")</button>
</div>
<div id="emailtesterror" class="message-Error" style="display:none;"></div>
<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"})",
error = $("#emailtesterror"),
info = $("#emailtestinfo"),
to = $("#emailtestto"),
subject = $("#emailtestsubject"),
body = $("#emailtestbody");
$("#emailtestsend").click(function() {
$.post(url, {
to: to.val(),
subject: subject.val(),
body: body.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();
} else {
info.html(data.status).show();
error.hide();
}
});
});
});
</script>
}
</fieldset> </fieldset>