From 580b79c24db2da9b7d97039b4fd6db03412fb5c6 Mon Sep 17 00:00:00 2001 From: HermesSbicego-Laser Date: Fri, 21 Oct 2016 11:46:08 +0200 Subject: [PATCH] - Added attachments logic --- .../Orchard.Email/Models/EmailMessage.cs | 8 ++++++- .../Services/SmtpMessageChannel.cs | 22 +++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs b/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs index 3edcd8731aa..8c588ce91c3 100644 --- a/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs +++ b/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs @@ -1,4 +1,6 @@ -namespace Orchard.Email.Models { +using System.Collections.Generic; + +namespace Orchard.Email.Models { public class EmailMessage { public string Subject { get; set; } public string Body { get; set; } @@ -7,5 +9,9 @@ public class EmailMessage { public string From { get; set; } public string Bcc { get; set; } public string Cc { get; set; } + /// + /// IEnumerable of strings representing attachments paths + /// + public IEnumerable Attachments { get; set; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs b/src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs index 0ab833e6bbc..02e26892a37 100644 --- a/src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs +++ b/src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs @@ -9,6 +9,8 @@ using Orchard.DisplayManagement; using Orchard.Logging; using Orchard.Email.Models; +using System.Linq; +using System.IO; namespace Orchard.Email.Services { public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { @@ -51,7 +53,8 @@ public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { ReplyTo = Read(parameters, "ReplyTo"), From = Read(parameters, "From"), Bcc = Read(parameters, "Bcc"), - Cc = Read(parameters, "CC") + Cc = Read(parameters, "CC"), + Attachments = (IEnumerable)(parameters.ContainsKey("Attachments") ? parameters["Attachments"] : new List()) }; if (emailMessage.Recipients.Length == 0) { @@ -105,8 +108,7 @@ public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { if (!String.IsNullOrWhiteSpace(emailMessage.From)) { mailMessage.From = new MailAddress(emailMessage.From); - } - else { + } else { // Take 'From' address from site settings or web.config. mailMessage.From = !String.IsNullOrWhiteSpace(_smtpSettings.Address) ? new MailAddress(_smtpSettings.Address) @@ -119,9 +121,15 @@ public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { } } + 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); - } - catch (Exception e) { + } catch (Exception e) { Logger.Error(e, "Could not send email"); } } @@ -129,7 +137,7 @@ public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { private SmtpClient CreateSmtpClient() { // If no properties are set in the dashboard, use the web.config value. if (String.IsNullOrWhiteSpace(_smtpSettings.Host)) { - return new SmtpClient(); + return new SmtpClient(); } var smtpClient = new SmtpClient { @@ -155,7 +163,7 @@ public class SmtpMessageChannel : Component, ISmtpChannel, IDisposable { } private IEnumerable ParseRecipients(string recipients) { - return recipients.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries); + return recipients.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); } } } \ No newline at end of file