#21002: EMail with CC Bcc etc.

Merge branch 'IBN-Labs/MailPlusCC' of https://git01.codeplex.com/forks/qt1/orchad4ibn into PR

Conflicts:
	src/Orchard.Web/Modules/Orchard.Email/Services/SmtpMessageChannel.cs

Work Item: 21002
This commit is contained in:
Sipke Schoorstra
2015-02-28 00:05:54 +01:00
parent 596d4a914d
commit 81fb0d7bf5
2 changed files with 55 additions and 12 deletions

View File

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

View File

@@ -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<string, object> 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<string, object> dictionary, string key) {
return dictionary.ContainsKey(key) ? dictionary[key] as string : null;
}
private IEnumerable<string> ParseRecipients(string recipients) {
return recipients.Split(new[] {',', ';'}, StringSplitOptions.RemoveEmptyEntries);
}
}
}