How To Send Email In Orchard CMS

This article explains how to send email in Orchard CMS using MessageService.

Introduction

In this article, you will learn how to send an email in Orchard CMS using its out of the box services

Steps to send email in Orchard CMS

  • Create Email Wrapper template
  • Create Email Template
  • Inject required Orchard services in the Controller or Custom Service
  • Specify email template to use and pass the data
  • Send email using MessageService

To demonstrate the scenario, let us take the example of sending a challenge email to verify the user's email address. In the Orchard.Users module, you will find all this code to send an email using Orchard services.

 

How To Send Email In Orchard CMS

 

Steps 1 - Create Email Wrapper template

It applies default body alteration for SMTP Channel which creates the body of the HTML template. Create a new file “Template.User.Wrapper.cshtml” under Views folder.

  1. @* Override this template to alter the email messages sent by the Orchard.Users module *@  
  2. @Display.PlaceChildContent(Source: Model)  

Steps 2 - Create Email Template

Now, create the content of the email. It applies default body alteration for SMTP Channel which creates the body of HTML template. Create a new file “Template.User.Validated.cshtml” under Views folder. It is the same Razor View page where we can pass the data and place on the place holders. For example, Model.ContactEmail is the address of the contact person of the website.

  1. @T("Thank you for registering with {0}.<br /><br /><br /><b>Final Step</b><br />To verify that you own this e-mail address, please click the following link:<br /><a href=\"{1}\">{1}</a><br /><br /><b>Troubleshooting:</b><br />If clicking on the link above does not work, try the following:<br /><br />Select and copy the entire link.<br />Open a browser window and paste the link in the address bar.<br />Click <b>Go</b> or, on your keyboard, press <b>Enter</b> or <b>Return</b>.", Model.RegisteredWebsite, Model.ChallengeUrl)  
  2. @if (!String.IsNullOrWhiteSpace(Model.ContactEmail)) {  
  3.     @T("<br /><br />If you continue to have access problems or want to report other issues, please <a href=\"mailto:{0}\">Contact Us</a>.",Model.ContactEmail)  
  4. }  

Steps 3 - Inject required Orchard services in the Controller or Custom Service

To render the email template, we need to use a few Orchard services. In Orchard CMS, every View Object is a shape so we need to create a shape using our created template views. We need to inject IShapeService and IShapeFactory to create the shape for the email content.

IMessageService is required to send the email through the SMTP channel.
  1. namespace Orchard.Users.Services {  
  2.     public class UserService : IUserService {  
  3.         private readonly IMessageService _messageService;  
  4.         private readonly IShapeFactory _shapeFactory;  
  5.         private readonly IShapeDisplay _shapeDisplay;  
  6.         public UserService(  
  7.         IMessageService messageService,   
  8.         IShapeFactory shapeFactory,  
  9.         IShapeDisplay shapeDisplay) {  
  10.   
  11.             _messageService = messageService;  
  12.             _shapeFactory = shapeFactory;  
  13.             _shapeDisplay = shapeDisplay;  
  14.         }  
  15.         ...  

Steps 4 - Specify email template to use and pass data

At this step, we specify the email and wrapper template to create the shape of the HTML content. In the below lines of code, the ShapeFactory.Create method takes the first parameter for the email template. Then, we add an email wrapper template in the template metadata. Along with this, we pass the model data using the anonymous object.

  1. var template = _shapeFactory.Create("Template_User_Validated", Arguments.From(new {  
  2.     RegisteredWebsite = site.As<RegistrationSettingsPart>().ValidateEmailRegisteredWebsite,  
  3.     ContactEmail = site.As<RegistrationSettingsPart>().ValidateEmailContactEMail,  
  4.     ChallengeUrl = url  
  5. }));  
  6. template.Metadata.Wrappers.Add("Template_User_Wrapper");  

Steps 5 - Sending email using MessageService

Now, let us specify the mail message Subject, Body, and Recipients. You can also specify the attachments here. Just add the “Attachments” key and list as value to the parameters dictionary. Attachment should be the file path to attach with the email message.

  1. var parameters = new Dictionary<stringobject> {  
  2.                     {"Subject", T("Verification E-Mail").Text},  
  3.                     {"Body", _shapeDisplay.Display(template)},  
  4.                     {"Recipients", user.Email}  
  5.                 };  
  6.   
  7.         _messageService.Send("Email", parameters);  

Below is the complete code for method which sends the challenge email.

  1. public void SendChallengeEmail(IUser user, Func<stringstring> createUrl) {  
  2.     string nonce = CreateNonce(user, DelayToValidate);  
  3.     string url = createUrl(nonce);  
  4.   
  5.     if (user != null) {  
  6.         var site = _siteService.GetSiteSettings();  
  7.   
  8.         var template = _shapeFactory.Create("Template_User_Validated", Arguments.From(new {  
  9.             RegisteredWebsite = site.As<RegistrationSettingsPart>().ValidateEmailRegisteredWebsite,  
  10.             ContactEmail = site.As<RegistrationSettingsPart>().ValidateEmailContactEMail,  
  11.             ChallengeUrl = url  
  12.         }));  
  13.         template.Metadata.Wrappers.Add("Template_User_Wrapper");  
  14.           
  15.         var parameters = new Dictionary<stringobject> {  
  16.                     {"Subject", T("Verification E-Mail").Text},  
  17.                     {"Body", _shapeDisplay.Display(template)},  
  18.                     {"Recipients", user.Email}  
  19.                 };  
  20.   
  21.         _messageService.Send("Email", parameters);  
  22.     }  
  23. }  

Conclusion

I guess that’s all you need to know to send an email in Orchard CMS.