Question, How to attach a file to an email in Umbraco Forms by code?. I have a piece of code on jquery that create a string so I need to save that string to a file and attach it to an email. But the user should not do it manually.
any idea is really appreciated. I am running against time.
Thank you so much for your answer but that is not what I mean.
In your answer, the user has to pick the file to be uploaded.
What I need is to attach a file to the email by code when the user click submit.
Let me try to explain this better. There is a google map in my form that when the user draw a polygon it generates a string containig the info of the polygon drawed. So I need to;
1-. Create a file with that string.
2-. Attach this file to the email.
These two points have to be done by code, not by the user.
These two points are my issue.
And finally I made a custom workflowtype that satisfies my need. Here is the code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Forms.Core.Enums;
using System.Text.RegularExpressions;
using Umbraco.Forms.Data.Storage;
using System.Xml;
using System.Xml.XPath;
using System.Web;
using System.Net.Mail;
using Umbraco.Core.Configuration;
using System.IO;
using System.Net.Mime;
namespace Umbraco.Forms.Core.Providers.WorkflowTypes
{
public class SendEmailKML : WorkflowType
{
[Attributes.Setting("Email", description = "Enter a list of receivers email separated by a semicolon", view = "TextField")]
public string Email { get; set; }
[Attributes.Setting("SenderEmail", description = "Enter the sender email (if blank it will use the settings from /config/umbracosettings.config)", view = "TextField")]
public string SenderEmail { get; set; }
[Attributes.Setting("Subject", description = "Enter the subject", view = "TextField")]
public string Subject { get; set; }
[Attributes.Setting("Attachment", description = "Attach file uploads to email", view = "Checkbox")]
public string Attachment { get; set; }
public SendEmailKML()
{
this.Id = new Guid("A7309169-E91A-484C-BFFA-934CAF733864");
this.Name = "Request a quote email";
this.Description = "Send the info on the \"Request a quote\" form to a list of emails and attach a KML file generated from the map";
}
public override List<Exception> ValidateSettings()
{
List<Exception> l = new List<Exception>();
if (string.IsNullOrEmpty(Email))
l.Add(new Exception("'Email' setting has not been set"));
if (string.IsNullOrEmpty(Subject))
l.Add(new Exception("'Subject' setting has not been set'"));
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(string.IsNullOrEmpty(SenderEmail) ? UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress : SenderEmail);
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);
}
XmlNode xml = record.ToXml(new System.Xml.XmlDocument());
XPathNavigator navigator = xml.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("//fields/child::*");
selectExpression.AddSort("@pageindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
selectExpression.AddSort("@fieldsetindex", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
selectExpression.AddSort("@sortorder", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Number);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
var fileKML = "";
string list = "<p></p> <ul> ";
while (nodeIterator.MoveNext())
{
var caption = Umbraco.Forms.Data.DictionaryHelper.GetText(nodeIterator.Current.SelectSingleNode("caption").Value);
if ((caption != "Attach files") && (caption != "kmlString"))
{
list += "<li><strong>" + caption + ": </strong>";
}
XPathNodeIterator values = nodeIterator.Current.Select(".//value");
while (values.MoveNext())
{
var val = values.Current.Value.Trim();
if ((caption != "Attach files") && (caption != "kmlString"))
{
list += Umbraco.Forms.Data.DictionaryHelper.GetText(val).Replace("\n", "<br/>") + "<br/><br/>";
}
if ((this.Attachment == true.ToString()) && (val.Contains("/forms/upload")))
{
// add attachment
string filelocation = HttpContext.Current.Server.MapPath(val);
m.Attachments.Add(new Attachment(filelocation));
}
if ((caption == "kmlString"))
{
fileKML = val.Replace(""", "\"");
}
}
list += "</li>";
caption = "";
}
list += "</ul>";
m.Body = list;
var stream = new MemoryStream();
var fileBytes = Encoding.UTF8.GetBytes(fileKML);
stream.Write(fileBytes, 0, fileBytes.Length);
stream.Seek(0, SeekOrigin.Begin);
var contentType = new ContentType
{
MediaType = MediaTypeNames.Application.Octet,
Name = "Map_Coordinates.kml"
};
var attachment = new Attachment(stream, contentType);
m.Attachments.Add(attachment);
System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient();
s.Send(m);
return WorkflowExecutionStatus.Completed;
}
}
Attach a file to email by code
Hello
Question, How to attach a file to an email in Umbraco Forms by code?. I have a piece of code on jquery that create a string so I need to save that string to a file and attach it to an email. But the user should not do it manually.
any idea is really appreciated. I am running against time.
Hi Damaso, and welcome to Our 😊
Try to see my comment in this thread
https://our.umbraco.org/forum/umbraco-pro/contour/64180-Umbraco-Forms-email-workflow-Full-path-of-uploaded-files-or-files-as-attachments#comment-216813
Hope this can help you to archive what you are trying to do
/Dennis
Hello Dennis
Thank you so much for your answer but that is not what I mean. In your answer, the user has to pick the file to be uploaded. What I need is to attach a file to the email by code when the user click submit. Let me try to explain this better. There is a google map in my form that when the user draw a polygon it generates a string containig the info of the polygon drawed. So I need to; 1-. Create a file with that string. 2-. Attach this file to the email. These two points have to be done by code, not by the user. These two points are my issue.
Again, thank you.
Here is what finally I did.
I create a custom WorkflowType.
In this link, I found a Forms default SendEmail workflowtype;
https://gist.github.com/TimGeyssens/6ff5392c4d9bbf89bdfc
From there and using the idea from this other link;
https://our.umbraco.org/forum/umbraco-pro/contour/44586-Working-with-save-as-file-workflow
And finally I made a custom workflowtype that satisfies my need. Here is the code;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Umbraco.Forms.Core.Enums; using System.Text.RegularExpressions; using Umbraco.Forms.Data.Storage; using System.Xml; using System.Xml.XPath; using System.Web; using System.Net.Mail; using Umbraco.Core.Configuration; using System.IO; using System.Net.Mime;
namespace Umbraco.Forms.Core.Providers.WorkflowTypes { public class SendEmailKML : WorkflowType { [Attributes.Setting("Email", description = "Enter a list of receivers email separated by a semicolon", view = "TextField")] public string Email { get; set; }
}
I want to send a huge thanks to;
Jesper Hauge, Tim Geyssens, Dennis Aaen
is working on a reply...