Bevin
Bevin

Reputation: 173

Custom Tokens In Sitecore for Email Body

I need to send customized email's while the user requests the password and when the user has been enabled. the body of the email should b loaded dynamically from sitecore, I guess custom tokens should be used to access the name and password details while entering the body text in sitecore. Could anyone suggest me what could be done to achieve this...??

Im a newbie so pardon me if the question is silly...!!

Upvotes: 0

Views: 1071

Answers (2)

pavan
pavan

Reputation: 1

Setup your Sitecore Item that contains content for your email with custom tokens in it, Apply some presentation to your email item. Then pass your email item to GetEmailBody method , which renders your sitecore Item and returns the rendered HTML.

In the foreach loop of sendEmail method, you will be replacing your custom tokens with actual token values. The dictionary that you passed to send email method should contain your custom token as key and replacement string as value.

public static bool SendEmail(Item emailItem, string toAddress, Dictionary<string, string> tokens)
    {
    //get rendered Email body from your email item
        emailBody = GetEmailBody(emailItem);

        //here we go through our dictionary of tokens and properly replace each token in our emailBody
        string encodedTokenKey = string.Empty;
        foreach (KeyValuePair<string, string> token in tokens)
        {
            emailBody = emailBody.Replace(token.Key, token.Value);
            encodedTokenKey = System.Web.HttpUtility.UrlEncode(token.Key);
            if (!string.IsNullOrEmpty(encodedTokenKey)) {
                emailBody = Regex.Replace(emailBody, encodedTokenKey, token.Value, RegexOptions.IgnoreCase);
            }
        }
    }

    public static string GetEmailBody(Item emailItem)
    {
        System.Net.WebRequest request;
        string text = string.Empty;
        request = (System.Net.HttpWebRequest.Create(string.Format("http://{0}{1}",
            System.Web.HttpContext.Current.Request.Url.Host,emailItem.GetFriendlyUrl())));

        try
        {
            using (System.Net.WebResponse response = request.GetResponse())
            {
                using (System.IO.StreamReader reader =
                    new System.IO.StreamReader(response.GetResponseStream()))
                {
                    text = reader.ReadToEnd();
                }
            }
        }
        catch (Exception ex) { }

        if (string.IsNullOrEmpty(text)) 
        { text = emailItem[SitecoreFields.Email_Message_Body]; }

        return text;
    }

Upvotes: 0

Matthew Dresser
Matthew Dresser

Reputation: 11442

You're basically along the right lines with this (assuming you're not using ECM as jammykam mentioned). I have done the same thing and there are a number of steps (assuming your tokens are already in place within your email content item):

  1. Retrieve your email item in Sitecore
  2. Create a System.Net.Mail.MailMessage object
  3. Call FieldRenderer.Render for rendering out the body text of your email from the Sitecore item
  4. After calling FieldRenderer.Render, perform your token replacement (could be done using string.Replace)
  5. Send email using SmtpClient.Send

If you wanted the ability to insert the tokens into a rich text field, you would need to customise the rich text editor, which can be reasonably complicated - that would be a separate question.

Upvotes: 2

Related Questions