请注意,本站并不支持低于IE8的浏览器,为了获得最佳效果,请下载最新的浏览器,推荐下载 Chrome浏览器
欢迎光临。交流群:166852192

如何自定义Orchard CMS电子邮件而不创建自己的模块


Orchard CMS is a great CMS for two important reasons: extending and increasing its core functionality. 
Even as the lead developer (and owner) of a .NET development agency in Boston, I had a hard time understanding how all the quote-unqoute magic happens in Orchard CMS, with template overriding and shape objects. But it’s part of my role here at Cloud Construct, heading up what I think is one of the best web development teams in Boston, to understand the in’s and out’s of the tools we use or recommend to our web development clients.
Orchard CMS是一个伟大的CMS,有两个重要的原因:扩展和增加其核心功能。


即使作为波士顿的.NET开发机构的主要开发者(和所有者),我也很难理解在Orchard CMS中所有的引用异常是如何发生的,模板覆盖和形状对象。 但它是我在Cloud Construct的角色的一部分,指导我认为是波士顿最好的网络开发团队之一,了解我们使用或推荐给我们的网络开发客户的工具。
I discovered the source of the magic. The creators coded Orchard CMS to override templates and shape objects. More so, template overriding is basically the same thing you do when you want to pass a View template to a controller, except the Orchard core checks for alternates and similarly named templates in your theme first.
Okay, so it’s not really magic. Just great architecture. So great, I showed my team of .NET and azure developers, User Experience, and even Information Architecture designers. As I dug deeper, I realized shape objects were simply dynamic objects that were getting resolved at runtime. The Drivers build them, and we (meaning my web development team) can modify them as needed along the Orchard pipeline. I also discovered how to add more features into the core functionality.
我发现了魔法的源泉。 创建者将Orchard CMS编码以覆盖模板和形状对象。 更重要的是,当你想传递View模板到控制器时,模板覆盖基本上是一样的,除了Orchard核心首先检查你的主题中的备用项和类似命名的模板。


好吧,所以它不是真的魔术。 只是伟大的建筑。 那么伟大的,我展示了我的团队的.NET和天蓝色的开发人员,用户体验,甚至信息架构设计师。 当我挖得更深,我意识到形状对象只是动态对象,在运行时得到解决。 驱动程序构建它们,我们(意思是我的Web开发团队)可以根据需要在Orchard管道中修改它们。 我还发现如何在核心功能中添加更多功能。
Typically, you add new functionality to the CMS by building out a custom module and utilizing that module for whatever you need to do. But there are some instances where what you want to do is so minor that you don’t want to build a whole module for it. Rather, it’s more efficient not to build a whole module for one simple functionality task. At least, that’s my viewpoint: How can we make web development tools more efficient? Not only for us but also for our Cloud Construct clients who look to us as the experts.
For instance, you might just want to add a new parameter to an object in a core module and do something with that value during its normal processing. Case in point: Adding an option for a “reply-to” field to be added to emails coming from custom forms.
But the Orchard Email Activity, EmailMessage class, and SmtpMessageChannel class do not support this. Obviously, I could just modify those and call it a day; but if the Orchard.Email module ever gets updated, I would lose my changes.
通常,通过构建自定义模块并将该模块用于您需要执行的任何操作,向CMS添加新功能。但有些情况下,你想做的是那么轻微,你不想为它构建一个整体模块。相反,为一个简单的功能任务构建整个模块效率更高。至少,这是我的观点:我们如何使Web开发工具更有效率?不仅对我们,而且对于我们的Cloud Construct客户,我们的专家。


例如,您可能只想向核心模块中的对象添加新参数,并在其正常处理过程中使用该值进行操作。示例:为“添加到”自定义表单的电子邮件中添加“回复”字段的选项。


但是Orchard Email Activity,EmailMessage类和SmtpMessageChannel类不支持这个。显然,我可以只是修改它们,并称之为一天;但如果Orchard.Email模块有更新,我会失去我的更改。
Orchard.Email Module :

NOTE: I could also update the Orchard.Email module and then push my changes up to the public repo, so all can take advantage of this change. But in most cases, I am working disconnected from that repo and using standalone release code bases for our web development, .NET development or website design clients. Again, it came down to being efficient ... for the long-term.
注意:我也可以更新Orchard.Email模块,然后将我的更改推送到公共仓库,所以所有人都可以利用此更改。 但在大多数情况下,我的工作与该repo断开,并使用独立的版本代码库用于我们的Web开发,.NET开发或网站设计客户端。 再次,它归结为高效...长期。

The No-Module Solution

In Orchard, a theme is just like a module, for the most part. It is an MVC area and is checked by the Core before rendering for templates that might override any module. The secret is that its also checks any classes as well. This is pretty powerful. This lets you override classes that are present in the core and in modules by simply adding them to your theme. What did I do?
无模块解决方案
在Orchard中,主题就像一个模块,大部分。 它是一个MVC区域,并且在渲染之前由Core检查,可能覆盖任何模块的模板。 秘密是,它也检查任何类。 这是相当强大。 这允许您通过简单地将它们添加到您的主题来覆盖存在于核心和模块中的类。 我做了什么?
I added four new files in specific folders to my theme:
我在特定文件夹中添加了四个新文件到我的主题:

  1. MyEmailActivity.cs is a direct copy of the EmailActivity.cs file in the Orchard.Email module. I then changed the namespace and class name and modified the Execute method to support my new "From" parameter.
1、MyEmailActivity.cs是Orchard.Email模块中EmailActivity.cs文件的直接副本。 然后我更改了命名空间和类名,并修改了Execute方法以支持我的新“From”参数。


As you can see from above, we are now passing a From parameter into the messageservice. Step 2 details how to get that parameter into the Activity. You will also need to swap out the Form and Name category properties of the class as well. This will make sure your activity is unique and it gets bound to the form you're going to create in the next step.
2. The next file to add is MyEmailForm.cs. This is also an exact copy from EmailForm.cs in the Orchard.Email module. I then changed the namespace and class name and modified the Describe method to support adding the new "From" parameter to the Action form. Actions are associated with a form in order to provide additional information for your action to process. 
从上面可以看到,我们现在将一个From参数传递给messageservice。 步骤2详细说明如何获取该参数到活动。 您还需要替换类的表单和名称类别属性。 这将确保您的活动是唯一的,它绑定到您将在下一步中创建的表单。


2、下一个要添加的文件是MyEmailForm.cs。 这也是Orchard.Email模块中EmailForm.cs的精确副本。 然后我更改了命名空间和类名,并修改了Describe方法以支持将新的“From”参数添加到Action表单。 操作与表单相关联,以便为您的操作提供其他信息进行处理。

TIP: Make sure you also set the Id of the form to MyEmailActivity, which corresponds to the activity you created in Step 1.
3. Next, copy the EmailMessage.cs class from Orchard.Email and also add it to your theme like it is shown above. Update the namespace. Add an additional property called From which is a string field. This object will be used to build the new MailMessage in the Process method of the next file.
4. Create the last file called SmtpMessageChannel.cs and also copy this from Orchard.Email. You can update the namespace but leave the class name the same. This will allow for Orchard to resolve this as the SmtpMessageChannel class to use when sending the email. Orchard will use this class over the one defined in the Orchard.Email module. Next in this class, add some code to abstract out the new From parameter.
提示:确保您还将表单的Id设置为MyEmailActivity,这与您在步骤1中创建的活动相对应。

3.接下来,从Orchard.Email复制EmailMessage.cs类,并将其添加到您的主题,如上所示。 更新命名空间。 添加一个名为From的附加属性,它是一个字符串字段。 此对象将用于在下一个文件的Process方法中构建新的MailMessage。

4.创建名为SmtpMessageChannel.cs的最后一个文件,并从Orchard.Email中复制此文件。 您可以更新命名空间,但保留类名称相同。 这将允许Orchard将此解析为SmtpMessageChannel类,以在发送电子邮件时使用。 Orchard将使用这个类比在Orchard.Email模块中定义的类。 接下来在这个类中,添加一些代码以抽象出新的From参数。


Next, assign this parameter to the MailMessage being passed to the SmtpClient.
接下来,将此参数分配给要传递给SmtpClient的MailMessage。

This will then allow for you to provide a "reply-to" for your email. If you're familiar with the Form that is shown for the email activity, you know you can just paste in a hardcoded value or use a token. In my example, I am having users submit a contact form and then alerting the admin of the system through my new "MyEmailActivity." In the form for the activity, I use a token so that the recipient of the form action can easily respond to the person submitting the contact form. Below our new field, we added being seeded with a token from the contact form being submitted.
这将允许您为您的电子邮件提供“回复”。 如果您熟悉针对电子邮件活动显示的表单,那么您可以只粘贴硬编码值或使用令牌。 在我的示例中,我让用户提交联系表单,然后通过我的新“MyEmailActivity”提醒系统的管理员。 在活动的表单中,我使用令牌,以便表单操作的收件人可以轻松地响应提交联系表单的人。 在我们的新字段下面,我们添加了来自所提交的联系表单的令牌。

Here is the workflow definition:

5. For the last part, include the appropriate view files and stylesheets for the new Action so it is displayed in the Admin. Because we did this in the theme and not a module, we added two files to the Admin theme so they are found when the Workflow editor is brought up.
5.对于最后一部分,包括新动作的相应视图文件和样式表,以便它显示在管理中。 因为我们在主题而不是模块中执行此操作,所以我们向管理主题添加了两个文件,以便在工作流编辑器启动时找到它们。

Finally, add the css file shown in the picture above and also the Activity view file so that the Activity toolbox properly loads. I copied the ones from Orchard.Email.

That's it!

Let me know if you tried this too, or if you encountered any errors in my description. Some of the inner workings of the Orchard CMS are still magic to me.
最后,添加上图所示的css文件,以及Activity视图文件,以便正确加载Activity工具箱。 我复制了来自Orchard.Email的。


就是这样!
让我知道,如果你也试过,或者如果你在我的描述中遇到任何错误。 Orchard CMS的一些内部工作对我来说仍然是魔法。
Arra Derderian serves as the President and Lead Technical Architect for Cloud Construct, a top Boston web development firm, and founder of the Boston Orchard CMS User Group. He is a member of the Windows Azure Insiders group. Cloud Construct is a digital agency with capabilities that span web development, information architecture, UX, and design.
原文:https://www.cloudconstruct.com/blog/customize-orchard-emails-without-creating-your-own-module 

作者原创内容不容易,如果觉得内容不错,请点击右侧“打赏”,赏俩给作者花花,也算是对作者付出的肯定,也可以鼓励作者原创更多更好内容。
更多详情欢迎到QQ群 166852192 交流。