ここには2つの可能性があります。カスタムワークフロータスクと単純なトークン。
タスクを開始しましょう。私はシンプルなデモを作成しました。ここでのコードは次のとおりです。あなたは、ループ内の受信者を投げると、それらのそれぞれのための新しい電子メールを送信したい場合があります
活動/ RoleMailerTask.cs
namespace My.Module
{
using System.Collections.Generic;
using System.Linq;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Data;
using Orchard.Email.Activities;
using Orchard.Email.Services;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Messaging.Services;
using Orchard.Roles.Models;
using Orchard.Roles.Services;
using Orchard.Users.Models;
using Orchard.Workflows.Models;
using Orchard.Workflows.Services;
[OrchardFeature(Statics.FeatureNames.RoleMailerTask)]
public class RoleMailerTask : Task
{
private readonly IOrchardServices services;
public const string TaskName = "RoleMailer";
public RoleMailerTask(IOrchardServices services)
{
this.services = services;
this.T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public override string Name
{
get { return TaskName; }
}
public override string Form
{
get { return TaskName; }
}
public override LocalizedString Category
{
get { return this.T("Messaging"); }
}
public override LocalizedString Description
{
get { return this.T("Sends mails to all users with this chosen role"); }
}
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext)
{
yield return this.T("Done");
}
public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext)
{
var recipientsRole = int.Parse(activityContext.GetState<string>("RecipientsRole"));
var role = this.services.WorkContext.Resolve<IRoleService>().GetRole(recipientsRole);
var userRolesRepo = this.services.WorkContext.Resolve<IRepository<UserRolesPartRecord>>();
var recepientIds = userRolesRepo.Table.Where(x => x.Role.Name == role.Name).Select(x => x.UserId);
var recipients = this.services.ContentManager.GetMany<UserPart>(recepientIds, VersionOptions.Published, QueryHints.Empty)
.Where(x => !string.IsNullOrEmpty(x.Email))
.Select(x => x.Email);
var body = activityContext.GetState<string>("Body");
var subject = activityContext.GetState<string>("Subject");
var replyTo = activityContext.GetState<string>("ReplyTo");
var bcc = activityContext.GetState<string>("Bcc");
var cc = activityContext.GetState<string>("CC");
var parameters = new Dictionary<string, object> {
{"Subject", subject},
{"Body", body},
{"Recipients", string.Join(",", recipients)},
{"ReplyTo", replyTo},
{"Bcc", bcc},
{"CC", cc}
};
var queued = activityContext.GetState<bool>("Queued");
if (!queued)
{
this.services.WorkContext.Resolve<IMessageService>().Send(SmtpMessageChannel.MessageType, parameters);
}
else
{
var priority = activityContext.GetState<int>("Priority");
this.services.WorkContext.Resolve<IJobsQueueService>().Enqueue("IMessageService.Send", new { type = SmtpMessageChannel.MessageType, parameters = parameters }, priority);
}
yield return T("Done");
}
}
}
。上記のバージョンでは、誰もが皆のメールアドレスが表示されます...
フォーム/ RoleMailerTaskForm.cs
namespace My.Module
{
using System;
using System.Linq;
using System.Web.Mvc;
using Activities;
using Orchard;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Environment.Extensions;
using Orchard.Forms.Services;
using Orchard.Roles.Services;
[OrchardFeature(Statics.FeatureNames.RoleMailerTask)]
public class RoleMailerTaskForm : Component, IFormProvider
{
private readonly IRoleService roleService;
public RoleMailerTaskForm(IShapeFactory shapeFactory, IRoleService roleService)
{
this.roleService = roleService;
this.Shape = shapeFactory;
}
public dynamic Shape { get; set; }
public void Describe(DescribeContext context)
{
Func<IShapeFactory, dynamic> formFactory = shape =>
{
var form = this.Shape.Form(
Id: RoleMailerTask.TaskName,
_Type: this.Shape.FieldSet(
Title: this.T("Send to"),
_RecepientsRole: this.Shape.SelectList(
Id: "recipientsRole",
Name: "RecipientsRole",
Title: this.T("Recepient role"),
Description: this.T("The role of the users that should be notified"),
Items: this.roleService.GetRoles().Select(r => new SelectListItem { Value = r.Id.ToString(), Text = r.Name })
),
_Bcc: this.Shape.TextBox(
Id: "bcc",
Name: "Bcc",
Title: this.T("Bcc"),
Description: this.T("Specify a comma-separated list of email addresses for a blind carbon copy"),
Classes: new[] { "large", "text", "tokenized" }),
_CC: this.Shape.TextBox(
Id: "cc",
Name: "CC",
Title: this.T("CC"),
Description: this.T("Specify a comma-separated list of email addresses for a carbon copy"),
Classes: new[] { "large", "text", "tokenized" }),
_ReplyTo: this.Shape.Textbox(
Id: "reply-to",
Name: "ReplyTo",
Title: this.T("Reply To Address"),
Description: this.T("If necessary, specify an email address for replies."),
Classes: new[] { "large", "text", "tokenized" }),
_Subject: this.Shape.Textbox(
Id: "Subject", Name: "Subject",
Title: this.T("Subject"),
Description: this.T("The subject of the email message."),
Classes: new[] { "large", "text", "tokenized" }),
_Message: this.Shape.Textarea(
Id: "Body", Name: "Body",
Title: this.T("Body"),
Description: this.T("The body of the email message."),
Classes: new[] { "tokenized" })
));
return form;
};
context.Form(RoleMailerTask.TaskName, formFactory);
}
}
これは、タスクを設定する必要がありますフォームです。このフォームは、Orchard.Email/Forms/EmailFormから離れています。私が変更したのは、フォームの上部にある選択リストだけです。
これだけです。上記のタスクに示すように、ユーザーを、その役割名を解析し、取得:トークンアプローチの
は、あなただけの
{TheRoleYouWant RoleRecpients}のようなトークンを定義する必要があります。
現時点までに何を送信していますか?カスタムワークフローアクティビティを作成する必要があるようです。 – rtpHarry