Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Per Ploug Hansen 208 posts 129 karma points
    Jan 04, 2010 @ 14:07
    Per Ploug Hansen
    6

    Sending custom emails with contour - howto

    This is a simple custom workflow which uses a xslt file to modify the record xml, and turn it into a html email.

    This means that you have complete control over the email output using xslt, and can send out pretty much any kind of email content.

    The complete workflow class:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Xml;
    using System.Xml.XPath;

    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Enums;
    using Umbraco.Forms.Data.Storage;


    namespace XsltEmailWorkFlow
    {
    public class SendEmail : Umbraco.Forms.Core.WorkflowType
    {
    [Umbraco.Forms.Core.Attributes.Setting("Email", description = "Enter the receiver email", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
    public string Email { get; set; }

    [Umbraco.Forms.Core.Attributes.Setting("Subject", description = "Enter the subject", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
    public string Subject { get; set; }

    [Umbraco.Forms.Core.Attributes.Setting("XsltFile", description = "Transform the xml before posting it,<a target='_blank' href='xslt/postAsXmlSample.xslt'>(Sample file)</a>", prevalues = "XsltEmail", control = "Umbraco.Forms.Core.FieldSetting.File")]
    public string XsltFile { get; set; }


    public SendEmail()
    {
    this.Id = new Guid("34bca580-f928-11de-8a39-0800200c9a66");
    this.Name = "Send xslt transformed email";
    this.Description = "Send the result of the form to an email addresse";
    }

    public override List<Exception> ValidateSettings()
    {
    List<Exception> l = new List<Exception>();
    if (string.IsNullOrEmpty(Email))
    l.Add(new Exception("'Email' setting not filled out'"));

    if (string.IsNullOrEmpty(Subject))
    l.Add(new Exception("'Subject' setting not filled out'"));

    return l;
    }


    public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
    {
    System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
    m.From = new System.Net.Mail.MailAddress(umbraco.UmbracoSettings.NotificationEmailSender);
    m.Subject = Subject;
    m.IsBodyHtml = true;

    if (Email.Contains(";"))
    {
    string[] emails = Email.Split(';');
    foreach (string email in emails)
    {
    m.To.Add(email.Trim());
    }
    }
    else
    {
    m.To.Add(Email);
    }

    RecordsViewer viewer = new RecordsViewer();
    XmlNode xml = viewer.GetSingleXmlRecord(record, new System.Xml.XmlDocument());

    //we will by default set the body to the record xml so if no xslt file is
    //present we atleast get the raw data.
    string result = xml.OuterXml;
    if (!string.IsNullOrEmpty(XsltFile))
    {
    result = Umbraco.Forms.Data.XsltHelper.TransformXML(xml, XsltFile, null);
    }

    m.Body = result;

    System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient();
    s.Send(m);

    return WorkflowExecutionStatus.Completed;
    }
    }
    }

    And a sample xslt file to generate the body html:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts"
    exclude-result-prefixes="xsl msxsl user">

    <xsl:output method="html" media-type="text/html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="DTD/xhtml1-strict.dtd"
    cdata-section-elements="script style"
    indent="yes"
    encoding="utf-8"/>

    <xsl:param name="records" />

    <xsl:template match="/">

    <h3>Intro</h3>
    <p>
    Hello, this is a sample email using xslt to convert a record into a custom email
    </p>

    <h3>the fields</h3>
    <ul>
    <xsl:for-each select="$records//fields/child::*">
    <li>
    <h4>Caption: <xsl:value-of select="./caption"/></h4>
    <p>
    <xsl:value-of select=".//value"/>
    </p>
    </li>
    </xsl:for-each>
    </ul>

    <h3>The actual xml</h3>
    <xsl:copy-of select="$records"/>

    </xsl:template>



    </xsl:stylesheet>


     

    When you compile the code and add it to the /bin a new workflow will turn up in the list. When you select it, you can input receiver email, subject and upload an xslt file. The uploaded xslt file transform the record xml.

    And you're done

  • David Peck 690 posts 1896 karma points c-trib
    Jan 06, 2010 @ 10:49
    David Peck
    0

    Thanks PPH! This is really frickin' useful. I actually just wanted to know how to send an alert email without the contents of the post. While this is different to your solution, you've done all the hard work for me. Ta.

  • Dan Evans 631 posts 1018 karma points
    Jan 07, 2010 @ 11:39
    Dan Evans
    0

    This is great - exactly what we need. Hopefully this will become part of the core?

    The only issue i have is when i upload an XSLT it gets locked by a process so if i try and upload a new XSLT or amend the existing XSLT on the server I can't save any changes without restarting IIS

    Thanks

    Dan

  • Dan Evans 631 posts 1018 karma points
    Jan 07, 2010 @ 13:15
    Dan Evans
    2

    Here is an amended sample XSLT file which includes XSLT for:

    1. Selecting the value of a single node based on the node-name
    2. Getting the value of a server variable
    3. Excluding a node when looping through all the form fields based on the node-name (in this case the CAPTCHA)
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
    exclude-result-prefixes="xsl msxsl user umbraco.library Exslt.ExsltStrings">

    <xsl:output method="html" media-type="text/html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="DTD/xhtml1-strict.dtd"
    cdata-section-elements="script style"
    indent="yes"
    encoding="utf-8"/>

    <xsl:param name="records" />

    <xsl:template match="/">

    <h3>Intro</h3>
    <p>
    This is a sample email using xslt to convert a record into a custom email
    </p>
    <p>
    Name: <xsl:value-of select="$records//fields/child::* [name() = 'name']/.//value"/><br />
    Website: <xsl:value-of select="umbraco.library:RequestServerVariables('HTTP_HOST')"/>
    </p>
    <h3>the fields</h3>
    <table>
    <xsl:for-each select="$records//fields/child::*[name() != 'typeboththelettersyoucanseeinthebox']">
    <tr>
    <td>
    <strong><xsl:value-of select="./caption"/></strong>
    </td>
    <td>
    <xsl:value-of select=".//value"/>
    </td>
    </tr>
    </xsl:for-each>
    </table>

    <h3>The actual xml</h3>
    <xsl:copy-of select="$records"/>
    </xsl:template>
    </xsl:stylesheet>

  • Tom 713 posts 954 karma points
    May 06, 2010 @ 04:14
    Tom
    0

    How would i refer to a value from the querystring here? I wanted to pass a referring pageID in to my email and use the xslt to generate the page url..

    ie: send-email.aspx?referring-page-id=1111

     

    then in the xslt

    sorry im just going to phrase it not actually xslt it

    "somone wanted you to see: <umbraco.libary:NiceUrl(referring-page-id)>"

  • Nik Wahlberg 639 posts 1237 karma points MVP
    May 06, 2010 @ 04:40
  • David Park 32 posts 52 karma points
    May 08, 2010 @ 19:51
    David Park
    0

    I have encountered a problem with this very generously donated solution - Greek characters do not display properly. That is, they are replaced, mostly by question marks.

    I thought UTF-8 covered that? Seemingly there is a problem somewhere. I suspect the same will happen with some Asian characters. Does anyone know where I or the code is going wrong?

    Thanks in advance.

  • David Park 32 posts 52 karma points
    May 08, 2010 @ 19:56
    David Park
    0

    Sorry - scratch some of what I just said. It is only the captured information that does not display correctly.

    Example:-

    Ονοματεπνυμο: ???µatep???µ?
    Διεθυνση ηλεκτρονικο ταχυδρομεου: [email protected]
    νομα Εταιρεας: ???µa ?ta??e?a?
    Διεθυνση: ??e????s?
    Πλη: ???
    Χρα: ???

    Is it a problem with the workflow class possibly? Or a wider Contour quirk? UTF-8 is not being defined somewhere perhaps?

Please Sign in or register to post replies

Write your reply to:

Draft