diff --git a/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs b/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs index 401045236..3edcd8731 100644 --- a/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs +++ b/src/Orchard.Web/Modules/Orchard.Email/Models/EmailMessage.cs @@ -4,7 +4,8 @@ public string Body { get; set; } public string Recipients { get; set; } public string ReplyTo { get; set; } + public string From { get; set; } public string Bcc { get; set; } - public string CC { get; set; } + public string Cc { 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 9fe4f9fdd..750f04a91 100644 --- a/src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs +++ b/src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs @@ -22,6 +22,7 @@ namespace Orchard.Email.Services { IOrchardServices orchardServices, IShapeFactory shapeFactory, IShapeDisplay shapeDisplay) { + _shapeFactory = shapeFactory; _shapeDisplay = shapeDisplay; @@ -39,7 +40,6 @@ namespace Orchard.Email.Services { public void Process(IDictionary parameters) { - if (!_smtpSettings.IsValid()) { return; } @@ -49,8 +49,9 @@ namespace Orchard.Email.Services { Subject = Read(parameters, "Subject"), Recipients = Read(parameters, "Recipients"), ReplyTo = Read(parameters, "ReplyTo"), + From = Read(parameters, "From"), Bcc = Read(parameters, "Bcc"), - CC = Read(parameters, "CC") + Cc = Read(parameters, "CC") }; if (emailMessage.Recipients.Length == 0) { @@ -58,7 +59,7 @@ namespace Orchard.Email.Services { return; } - // Applying default Body alteration for SmtpChannel + // Apply default Body alteration for SmtpChannel. var template = _shapeFactory.Create("Template_Smtp_Wrapper", Arguments.From(new { Content = new MvcHtmlString(emailMessage.Body) })); @@ -69,18 +70,55 @@ namespace Orchard.Email.Services { IsBodyHtml = true }; - var section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); - mailMessage.From = !String.IsNullOrWhiteSpace(_smtpSettings.Address) - ? new MailAddress(_smtpSettings.Address) - : new MailAddress(section.From); + if (parameters.ContainsKey("Message")) { + // A full message object is provided by the sender. + + var oldMessage = mailMessage; + mailMessage = (MailMessage)parameters["Message"]; + + if (String.IsNullOrWhiteSpace(mailMessage.Subject)) + mailMessage.Subject = oldMessage.Subject; + + if (String.IsNullOrWhiteSpace(mailMessage.Body)) { + mailMessage.Body = oldMessage.Body; + mailMessage.IsBodyHtml = oldMessage.IsBodyHtml; + } + } try { - foreach (var recipient in emailMessage.Recipients.Split(new [] {',', ';'}, StringSplitOptions.RemoveEmptyEntries)) { + + foreach (var recipient in ParseRecipients(emailMessage.Recipients)) { + mailMessage.To.Add(new MailAddress(recipient)); + } + + if (!String.IsNullOrWhiteSpace(emailMessage.Cc)) { + foreach (var recipient in ParseRecipients(emailMessage.Cc)) { + mailMessage.CC.Add(new MailAddress(recipient)); + } + } + + if (!String.IsNullOrWhiteSpace(emailMessage.Bcc)) { + foreach (var recipient in ParseRecipients(emailMessage.Bcc)) { + mailMessage.Bcc.Add(new MailAddress(recipient)); + } + } + + if (!String.IsNullOrWhiteSpace(emailMessage.From)) { + mailMessage.From = new MailAddress(emailMessage.From); + } + else { + // Take 'From' address from site settings or web.config. + mailMessage.From = !String.IsNullOrWhiteSpace(_smtpSettings.Address) + ? new MailAddress(_smtpSettings.Address) + : new MailAddress(((SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp")).From); + } + + foreach (var recipient in ParseRecipients(emailMessage.Recipients)) { mailMessage.To.Add(new MailAddress(recipient)); } if (!String.IsNullOrWhiteSpace(emailMessage.ReplyTo)) { - foreach (var recipient in emailMessage.ReplyTo.Split(new [] {',', ';'}, StringSplitOptions.RemoveEmptyEntries)) { + foreach (var recipient in ParseRecipients(emailMessage.ReplyTo)) { mailMessage.ReplyToList.Add(new MailAddress(recipient)); } } @@ -93,7 +131,7 @@ namespace Orchard.Email.Services { } 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)) { return new SmtpClient(); } @@ -119,5 +157,9 @@ namespace Orchard.Email.Services { private string Read(IDictionary dictionary, string key) { return dictionary.ContainsKey(key) ? dictionary[key] as string : null; } + + private IEnumerable ParseRecipients(string recipients) { + return recipients.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries); + } } -} +} \ No newline at end of file