여기에는 두 가지 가능성이 있습니다. 사용자 지정 워크 플로 작업과 간단한 토큰입니다.
작업부터 시작하겠습니다. 나는 간단한 데모를 만들었습니다. 여기 코드는 다음과 같습니다 당신은 루프에서받는 사람을 던져, 그리고 그들 각각에 대해 새 이메일을 보내도록 할 수 있습니다
활동/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