Skip to content

Commit

Permalink
- Added attachments logic
Browse files Browse the repository at this point in the history
  • Loading branch information
HermesSbicego-Laser committed Oct 21, 2016
1 parent 59a9f0b commit 2bb81ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -7,5 +9,9 @@ public class EmailMessage {
public string From { get; set; }
public string Bcc { get; set; }
public string Cc { get; set; }
/// <summary>
/// IEnumerable of strings representing attachments paths
/// </summary>
public IEnumerable<string> Attachments { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,7 +53,8 @@ public void Process(IDictionary<string, object> parameters) {
ReplyTo = Read(parameters, "ReplyTo"),
From = Read(parameters, "From"),
Bcc = Read(parameters, "Bcc"),
Cc = Read(parameters, "CC")
Cc = Read(parameters, "CC"),
Attachments = (IEnumerable<string>)parameters["Attachments"]
};

if (emailMessage.Recipients.Length == 0) {
Expand Down Expand Up @@ -105,8 +108,7 @@ public void Process(IDictionary<string, object> parameters) {

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)
Expand All @@ -119,17 +121,21 @@ public void Process(IDictionary<string, object> parameters) {
}
}

foreach (var attachmentPath in emailMessage.Attachments) {
if (File.Exists(attachmentPath)) {
mailMessage.Attachments.Add(new Attachment(attachmentPath));
}
}
_smtpClientField.Value.Send(mailMessage);
}
catch (Exception e) {
} catch (Exception e) {
Logger.Error(e, "Could not send email");
}
}

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 {
Expand All @@ -155,7 +161,7 @@ private string Read(IDictionary<string, object> dictionary, string key) {
}

private IEnumerable<string> ParseRecipients(string recipients) {
return recipients.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries);
return recipients.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
}
}
}

0 comments on commit 2bb81ee

Please sign in to comment.