Enabling the sending of e-mail messages to arbitrary recipients

that are not necessarily registered users.

--HG--
branch : 1.x
This commit is contained in:
Bertrand@Nutella
2011-11-26 16:08:37 -08:00
parent 307937b3ec
commit dfdc3596f6
4 changed files with 77 additions and 54 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Orchard.ContentManagement.Records;
using Orchard.Core.Common.Models;
using Orchard.Environment.Extensions;
using Orchard.Events;
@@ -45,42 +44,35 @@ namespace Orchard.Email.Rules {
private bool Send(dynamic context) {
var recipient = context.Properties["Recipient"];
ContentItemRecord recipientRecord = null;
var properties = new Dictionary<string, string>(context.Properties);
if (recipient == "owner") {
var content = context.Tokens["Content"] as IContent;
if (content.Has<CommonPart>()) {
recipientRecord = content.As<CommonPart>().Owner.ContentItem.Record;
var owner = content.As<CommonPart>().Owner.ContentItem;
if (owner != null && owner.Record != null) {
_messageManager.Send(owner.Record, MessageType, "email", properties);
}
_messageManager.Send(owner.As<IUser>().Email, MessageType, "email", properties);
}
}
if (recipient == "author") {
else if (recipient == "author") {
var user = _orchardServices.WorkContext.CurrentUser;
// can be null if user is anonymous
if (user != null && String.IsNullOrWhiteSpace(user.Email)) {
recipientRecord = user.ContentItem.Record;
_messageManager.Send(user.ContentItem.Record, MessageType, "email", properties);
}
}
if (recipient == "admin") {
else if (recipient == "admin") {
var username = _orchardServices.WorkContext.CurrentSite.SuperUser;
var user = _membershipService.GetUser(username);
// can be null if user is no super user is defined
if (user != null && !String.IsNullOrWhiteSpace(user.Email)) {
recipientRecord = user.ContentItem.Record;
_messageManager.Send(user.ContentItem.Record, MessageType, "email", properties);
}
}
if (recipientRecord == null) {
return true;
}
var properties = new Dictionary<string, string>(context.Properties);
_messageManager.Send(recipientRecord, MessageType, "email", properties);
return true;
}
}
@@ -99,13 +91,15 @@ namespace Orchard.Email.Rules {
if (context.MessagePrepared)
return;
var contentItem = _contentManager.Get(context.Recipient.Id);
if (contentItem == null)
return;
if (context.Recipient != null) {
var contentItem = _contentManager.Get(context.Recipient.Id);
if (contentItem == null)
return;
var recipient = contentItem.As<IUser>();
if (recipient == null)
return;
var recipient = contentItem.As<IUser>();
if (recipient == null)
return;
}
switch (context.Type) {
case MailActions.MessageType:

View File

@@ -12,15 +12,17 @@ namespace Orchard.Email.Services {
}
public void Sending(MessageContext context) {
var contentItem = _contentManager.Get(context.Recipient.Id);
if ( contentItem == null )
return;
if (context.Recipient != null) {
var contentItem = _contentManager.Get(context.Recipient.Id);
if (contentItem == null)
return;
var recipient = contentItem.As<IUser>();
if ( recipient == null )
return;
var recipient = contentItem.As<IUser>();
if (recipient == null)
return;
context.MailMessage.To.Add(recipient.Email);
context.MailMessage.To.Add(recipient.Email);
}
}
public void Sent(MessageContext context) {

View File

@@ -27,39 +27,61 @@ namespace Orchard.Messaging.Services {
Logger.Information("Sending message {0}", type);
try {
var context = new MessageContext {
Recipient = recipient,
Type = type,
Service = service
};
try {
if (properties != null) {
foreach (var key in properties.Keys)
context.Properties.Add(key, properties[key]);
}
_messageEventHandler.Sending(context);
foreach (var channel in _channels) {
channel.SendMessage(context);
}
_messageEventHandler.Sent(context);
}
finally {
context.MailMessage.Dispose();
}
Logger.Information("Message {0} sent", type);
PrepareAndSend(type, properties, context);
}
catch ( Exception e ) {
Logger.Error(e, "An error occured while sending the message {0}", type);
}
}
private void PrepareAndSend(string type, Dictionary<string, string> properties, MessageContext context) {
try {
if (properties != null) {
foreach (var key in properties.Keys)
context.Properties.Add(key, properties[key]);
}
_messageEventHandler.Sending(context);
foreach (var channel in _channels) {
channel.SendMessage(context);
}
_messageEventHandler.Sent(context);
}
finally {
context.MailMessage.Dispose();
}
Logger.Information("Message {0} sent", type);
}
public void Send(string recipientAddresses, string type, string service, Dictionary<string, string> properties = null) {
if (!HasChannels())
return;
Logger.Information("Sending message {0}", type);
try {
var context = new MessageContext {
Type = type,
Service = service
};
context.MailMessage.To.Add(recipientAddresses);
PrepareAndSend(type, properties, context);
}
catch (Exception e) {
Logger.Error(e, "An error occured while sending the message {0}", type);
}
}
public bool HasChannels() {
return _channels.Any();
}

View File

@@ -4,12 +4,17 @@ using Orchard.ContentManagement.Records;
namespace Orchard.Messaging.Services {
public interface IMessageManager : IDependency {
/// <summary>
/// Sends a message to a channel
/// Sends a message to a channel using a user content item as the recipient
/// </summary>
void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null);
/// <summary>
/// Wether at least one channel is active on the current site
/// Sends a message to a channel using a comma-separated list of recipient addresses
/// </summary>
void Send(string recipientAddresses, string type, string service, Dictionary<string, string> properties = null);
/// <summary>
/// Whether at least one channel is active on the current site
/// </summary>
bool HasChannels();