mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
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:
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Orchard.ContentManagement.Records;
|
|
||||||
using Orchard.Core.Common.Models;
|
using Orchard.Core.Common.Models;
|
||||||
using Orchard.Environment.Extensions;
|
using Orchard.Environment.Extensions;
|
||||||
using Orchard.Events;
|
using Orchard.Events;
|
||||||
@@ -45,42 +44,35 @@ namespace Orchard.Email.Rules {
|
|||||||
|
|
||||||
private bool Send(dynamic context) {
|
private bool Send(dynamic context) {
|
||||||
var recipient = context.Properties["Recipient"];
|
var recipient = context.Properties["Recipient"];
|
||||||
ContentItemRecord recipientRecord = null;
|
var properties = new Dictionary<string, string>(context.Properties);
|
||||||
|
|
||||||
if (recipient == "owner") {
|
if (recipient == "owner") {
|
||||||
var content = context.Tokens["Content"] as IContent;
|
var content = context.Tokens["Content"] as IContent;
|
||||||
if (content.Has<CommonPart>()) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (recipient == "author") {
|
||||||
if (recipient == "author") {
|
|
||||||
var user = _orchardServices.WorkContext.CurrentUser;
|
var user = _orchardServices.WorkContext.CurrentUser;
|
||||||
|
|
||||||
// can be null if user is anonymous
|
// can be null if user is anonymous
|
||||||
if (user != null && String.IsNullOrWhiteSpace(user.Email)) {
|
if (user != null && String.IsNullOrWhiteSpace(user.Email)) {
|
||||||
recipientRecord = user.ContentItem.Record;
|
_messageManager.Send(user.ContentItem.Record, MessageType, "email", properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (recipient == "admin") {
|
||||||
if (recipient == "admin") {
|
|
||||||
var username = _orchardServices.WorkContext.CurrentSite.SuperUser;
|
var username = _orchardServices.WorkContext.CurrentSite.SuperUser;
|
||||||
var user = _membershipService.GetUser(username);
|
var user = _membershipService.GetUser(username);
|
||||||
|
|
||||||
// can be null if user is no super user is defined
|
// can be null if user is no super user is defined
|
||||||
if (user != null && !String.IsNullOrWhiteSpace(user.Email)) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,13 +91,15 @@ namespace Orchard.Email.Rules {
|
|||||||
if (context.MessagePrepared)
|
if (context.MessagePrepared)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var contentItem = _contentManager.Get(context.Recipient.Id);
|
if (context.Recipient != null) {
|
||||||
if (contentItem == null)
|
var contentItem = _contentManager.Get(context.Recipient.Id);
|
||||||
return;
|
if (contentItem == null)
|
||||||
|
return;
|
||||||
|
|
||||||
var recipient = contentItem.As<IUser>();
|
var recipient = contentItem.As<IUser>();
|
||||||
if (recipient == null)
|
if (recipient == null)
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (context.Type) {
|
switch (context.Type) {
|
||||||
case MailActions.MessageType:
|
case MailActions.MessageType:
|
||||||
|
|||||||
@@ -12,15 +12,17 @@ namespace Orchard.Email.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Sending(MessageContext context) {
|
public void Sending(MessageContext context) {
|
||||||
var contentItem = _contentManager.Get(context.Recipient.Id);
|
if (context.Recipient != null) {
|
||||||
if ( contentItem == null )
|
var contentItem = _contentManager.Get(context.Recipient.Id);
|
||||||
return;
|
if (contentItem == null)
|
||||||
|
return;
|
||||||
|
|
||||||
var recipient = contentItem.As<IUser>();
|
var recipient = contentItem.As<IUser>();
|
||||||
if ( recipient == null )
|
if (recipient == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
context.MailMessage.To.Add(recipient.Email);
|
context.MailMessage.To.Add(recipient.Email);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Sent(MessageContext context) {
|
public void Sent(MessageContext context) {
|
||||||
|
|||||||
@@ -27,39 +27,61 @@ namespace Orchard.Messaging.Services {
|
|||||||
|
|
||||||
Logger.Information("Sending message {0}", type);
|
Logger.Information("Sending message {0}", type);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
var context = new MessageContext {
|
var context = new MessageContext {
|
||||||
Recipient = recipient,
|
Recipient = recipient,
|
||||||
Type = type,
|
Type = type,
|
||||||
Service = service
|
Service = service
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
PrepareAndSend(type, properties, context);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
catch ( Exception e ) {
|
catch ( Exception e ) {
|
||||||
Logger.Error(e, "An error occured while sending the message {0}", type);
|
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() {
|
public bool HasChannels() {
|
||||||
return _channels.Any();
|
return _channels.Any();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,17 @@ using Orchard.ContentManagement.Records;
|
|||||||
namespace Orchard.Messaging.Services {
|
namespace Orchard.Messaging.Services {
|
||||||
public interface IMessageManager : IDependency {
|
public interface IMessageManager : IDependency {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a message to a channel
|
/// Sends a message to a channel using a user content item as the recipient
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null);
|
void Send(ContentItemRecord recipient, string type, string service, Dictionary<string, string> properties = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
bool HasChannels();
|
bool HasChannels();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user