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);
<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.
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.
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
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?
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:
And a sample xslt file to generate the body html:
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
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.
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
Here is an amended sample XSLT file which includes XSLT for:
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)>"
Tom, check out the answer here:
http://our.umbraco.org/forum/umbraco-pro/contour/8937-Email-to-a-friend?p=0#comment32617
Cheers...
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.
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?
is working on a reply...