[Fixes #6196] Added attachments logic (#7338)

Fixes #6196
This commit is contained in:
Hermes Sbicego 2016-12-01 22:09:48 +01:00 committed by Sébastien Ros
parent 72062399ed
commit 46c3fe6e79
4 changed files with 26 additions and 46 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Web.Hosting;
using System.Web.Mvc; using System.Web.Mvc;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Email.Models; using Orchard.Email.Models;
@ -52,11 +53,7 @@ namespace Orchard.Email.Controllers {
else { else {
_smtpChannel.Process(new Dictionary<string, object> { _smtpChannel.Process(new Dictionary<string, object> {
{"Recipients", testSettings.To}, {"Recipients", testSettings.To},
{"Subject", testSettings.Subject}, {"Subject", T("Orchard CMS - SMTP settings test email").Text}
{"Body", testSettings.Body},
{"ReplyTo", testSettings.ReplyTo},
{"Bcc", testSettings.Bcc},
{"CC", testSettings.Cc}
}); });
} }
@ -64,10 +61,10 @@ namespace Orchard.Email.Controllers {
return Json(new { error = 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) { catch (Exception e) {
return Json(new {error = e.Message}); return Json(new { error = e.Message });
} }
finally { finally {
var smtpChannelComponent = _smtpChannel as Component; var smtpChannelComponent = _smtpChannel as Component;
@ -94,7 +91,6 @@ namespace Orchard.Email.Controllers {
public class TestSmtpSettings { public class TestSmtpSettings {
public string From { get; set; } public string From { get; set; }
public string ReplyTo { get; set; }
public string Host { get; set; } public string Host { get; set; }
public int Port { get; set; } public int Port { get; set; }
public bool EnableSsl { get; set; } public bool EnableSsl { get; set; }
@ -103,10 +99,6 @@ namespace Orchard.Email.Controllers {
public string UserName { get; set; } public string UserName { get; set; }
public string Password { get; set; } public string Password { get; set; }
public string To { 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; }
} }
} }
} }

View File

@ -1,4 +1,6 @@
namespace Orchard.Email.Models { using System.Collections.Generic;
namespace Orchard.Email.Models {
public class EmailMessage { public class EmailMessage {
public string Subject { get; set; } public string Subject { get; set; }
public string Body { get; set; } public string Body { get; set; }
@ -7,5 +9,9 @@
public string From { get; set; } public string From { get; set; }
public string Bcc { get; set; } public string Bcc { get; set; }
public string Cc { get; set; } public string Cc { get; set; }
/// <summary>
/// IEnumerable of strings representing attachments paths
/// </summary>
public IEnumerable<string> Attachments { get; set; }
} }
} }

View File

@ -9,6 +9,8 @@ using Orchard.ContentManagement;
using Orchard.DisplayManagement; using Orchard.DisplayManagement;
using Orchard.Logging; using Orchard.Logging;
using Orchard.Email.Models; using Orchard.Email.Models;
using System.Linq;
using System.IO;
namespace Orchard.Email.Services { namespace Orchard.Email.Services {
public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable {
@ -51,7 +53,8 @@ namespace Orchard.Email.Services {
ReplyTo = Read(parameters, "ReplyTo"), ReplyTo = Read(parameters, "ReplyTo"),
From = Read(parameters, "From"), From = Read(parameters, "From"),
Bcc = Read(parameters, "Bcc"), Bcc = Read(parameters, "Bcc"),
Cc = Read(parameters, "CC") Cc = Read(parameters, "CC"),
Attachments = (IEnumerable<string>)(parameters.ContainsKey("Attachments") ? parameters["Attachments"] : new List<string>())
}; };
if (emailMessage.Recipients.Length == 0) { if (emailMessage.Recipients.Length == 0) {
@ -119,6 +122,14 @@ namespace Orchard.Email.Services {
} }
} }
foreach (var attachmentPath in emailMessage.Attachments) {
if (File.Exists(attachmentPath)) {
mailMessage.Attachments.Add(new Attachment(attachmentPath));
}
else {
throw new FileNotFoundException(T("One or more attachments not found.").Text);
}
}
_smtpClientField.Value.Send(mailMessage); _smtpClientField.Value.Send(mailMessage);
} }
catch (Exception e) { catch (Exception e) {
@ -129,7 +140,7 @@ namespace Orchard.Email.Services {
private SmtpClient CreateSmtpClient() { private SmtpClient CreateSmtpClient() {
// If no properties are set in the dashboard, use the web.config value. // If no properties are set in the dashboard, use the web.config value.
if (String.IsNullOrWhiteSpace(_smtpSettings.Host)) { if (String.IsNullOrWhiteSpace(_smtpSettings.Host)) {
return new SmtpClient(); return new SmtpClient();
} }
var smtpClient = new SmtpClient { var smtpClient = new SmtpClient {
@ -155,7 +166,7 @@ namespace Orchard.Email.Services {
} }
private IEnumerable<string> ParseRecipients(string recipients) { private IEnumerable<string> ParseRecipients(string recipients) {
return recipients.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries); return recipients.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
} }
} }
} }

View File

@ -72,25 +72,6 @@
<label for="emailtestto">@T("To:")</label> <label for="emailtestto">@T("To:")</label>
<input type="text" id="emailtestto" class="large text" /> <input type="text" id="emailtestto" class="large text" />
</div> </div>
<div>
<label for="emailtestbcc">@T("Bcc:")</label>
<input type="text" id="emailtestbcc" class="large text" />
</div>
<div>
<label for="emailtestcc">@T("CC:")</label>
<input type="text" id="emailtestcc" 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>
<div> <div>
<button type="button" id="emailtestsend" class="button grey">@T("Send")</button> <button type="button" id="emailtestsend" class="button grey">@T("Send")</button>
</div> </div>
@ -111,12 +92,7 @@
useDefaultCredentials = $("input[name='@Html.NameFor(m => m.UseDefaultCredentials)']"), useDefaultCredentials = $("input[name='@Html.NameFor(m => m.UseDefaultCredentials)']"),
userName = $("#@Html.FieldIdFor(m => m.UserName)"), userName = $("#@Html.FieldIdFor(m => m.UserName)"),
password = $("#@Html.FieldIdFor(m => m.Password)"), password = $("#@Html.FieldIdFor(m => m.Password)"),
to = $("#emailtestto"), to = $("#emailtestto");
subject = $("#emailtestsubject"),
body = $("#emailtestbody"),
replyto = $("#emailtestreplyto"),
bcc = $("#emailtestbcc"),
cc = $("#emailtestcc");
$("#emailtestsend").click(function () { $("#emailtestsend").click(function () {
$.post(url, { $.post(url, {
@ -129,11 +105,6 @@
userName: userName.val(), userName: userName.val(),
password: password.val(), password: password.val(),
to: to.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() __RequestVerificationToken: to.closest("form").find("input[name=__RequestVerificationToken]").val()
}) })
.fail(function (data) { .fail(function (data) {