Copied to clipboard

Flag this post as spam?

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


  • Chester Campbell 98 posts 209 karma points
    Apr 13, 2016 @ 19:13
    Chester Campbell
    0

    WorkflowService.ExecuteWorkflows() will not send file upload attachment in email

    Hi,

    Umbraco 7.4.2 Umbraco Forms 4.2.1

    I have a surface controller form that is programmatically submitting data into an Umbraco Forms form. The fields include three "file upload" fields. The UF form has the "send email" workflow attached and the "send attachments" checkbox is checked.

    When I submit my surface controller form I am able to insert my form model's data into the UF form successfully. When I look in the backoffice I can see the records including the uploaded files.

    But the email that is sent by the workflow is not multi-part and does not have any attachments.

    There are no errors in the Umbraco Log.

    Any ideas why this isn't working? Is it a problem with ExecuteWorkflows() or perhaps could it be how I'm setting the value for the file upload fields?

    Here's how I'm setting the UF field values. My fields are either textfield, textarea, or file upload. I use this same routine for all three:

            private static void AddToRecord(Umbraco.Forms.Core.Record record, Umbraco.Forms.Core.Field field, object propertyValue)
        {
            Guid key = Guid.NewGuid();
            Umbraco.Forms.Core.RecordField recordField = new RecordField
            {
                Alias = field.Alias,
                FieldId = field.Id,
                Field = field,
                Key = key,
                Record = record.Id,
                Values = new List<object>() { propertyValue }
            };
    
            Umbraco.Forms.Data.Storage.RecordFieldStorage storage = new RecordFieldStorage();
            storage.InsertRecordField(recordField);
            record.RecordFields.Add(key, recordField);
        } 
    
  • Chris Wilson 100 posts 377 karma points
    Apr 18, 2016 @ 14:01
    Chris Wilson
    0

    Umbraco Forms first stores file uploads in Media/Forms, then stores the URL to the media item as the form value for that field.

    Are you replicating this procedure or attempting to store the file as a blob?

    /Chris.

  • Chester Campbell 98 posts 209 karma points
    Apr 18, 2016 @ 14:37
    Chester Campbell
    0

    @Chris ... I am saving my file uploads to another directory(/images/profiles/uploads), not /Media/Forms. Then I'm storing the site-relative path to the file as the value of the field in the Umbraco Form.

    My expectation is that the "Send an Email" workflow would use the value in the form field to retrieve the file and attach it to the email.

  • Chris Wilson 100 posts 377 karma points
    Apr 18, 2016 @ 15:04
    Chris Wilson
    0

    From what I understand the Send Email workflow will only send files it can find via the MediaService, you may need to create your own workflow for that. Thankfully that's relatively easy as you can essentially just create a Umbraco.Forms.CoreWorkflowType wrapper for System.Net.Mail.MailMessage.

    Something like this:

    public class SendPlainEmailWorkflow : WorkflowType
    {
        [Setting("Sender address",
                 description = "The address that emails will appear to have originated from. If unset, we use the environment default.",
                 view = "textfield")]
        public string SenderAddress { get; set; }
    
        [Setting("Recipient address",
                 description = "The address that emails will be sent to.",
                 view = "textfield")]
        public string RecipientAddress { get; set; }
    
        [Setting("Message subject",
                 description = "Enter the subject line of the email",
                 view = "textfield")]
        public string MessageSubject { get; set; }
    
        [Setting("Message body",
                 description = "The main body of the message",
                 view = "textarea")]
        public string MessageBody { get; set; }
    
        public SendPlainEmailWorkflow()
        {
            Id = Guid.Parse("8f15e8ed-433d-47fd-8631-b929c07bce39");
            Name = "Send custom email";
            Description = "Sends an email to the provided address. Does not include the submitted form data.";
        }
    
        public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
                // Get the sender address either from workflow config, or application config
                var senderAddress = record.TryPopulateReferences(SenderAddress);
                senderAddress = string.IsNullOrWhiteSpace(senderAddress)
                    ? UmbracoConfig.For.UmbracoSettings().Content.NotificationEmailAddress
                    : senderAddress;
    
                // Check the recipient address
                var recipientAddress = record.TryPopulateReferences(RecipientAddress);
    
                // Check the message subject
                var subject = record.TryPopulateReferences(MessageSubject);
    
                // Check the message body
                var message = record.TryPopulateReferences(MessageBody);
    
    
                // --- GET YOUR IMAGES HERE --- //
                var YOUR_ATTACHMENTS = ...
    
                // Construct the message
                var mailMsg = new System.Net.Mail.MailMessage
                {
                    From = new System.Net.Mail.MailAddress(senderAddress, senderAddress),
                    Subject = subject,
                    Body = message,
                    IsBodyHtml = true
                };
                mailMsg.To.Add(new System.Net.Mail.MailAddress(recipientAddress, recipientAddress));
    
                foreach(var photo in YOUR_ATTACHMENTS){
                    mailMsg.Attachments.Add(new System.Net.Mail.Attachment(photo.InputStream, photo.FileName));
                }
    
                // Send the message
                var smtpClient = new System.Net.Mail.SmtpClient { EnableSsl = false };
                smtpClient.Send(mailMsg);
    
                return WorkflowExecutionStatus.Completed;
        }
    
        public override List<Exception> ValidateSettings()
        {
            var errors = new List<Exception>();
    
            if (string.IsNullOrWhiteSpace(MessageSubject)) errors.Add(new Exception("Subject line is required. You may use bracket syntax."));
            if (string.IsNullOrWhiteSpace(RecipientAddress)) errors.Add(new Exception("Recipient address is required. You may use bracket syntax."));
    
            return errors;
        }
    }
    

    TryPopulateReferences is an extension method I have just to wrap the getting of a value from a Record. I use GUIDs but you can easily use aliases.

    The bracket syntax for form properties is handled by Umbraco Forms and hence will continue to work without any further effort on your part.

    /Chris

  • Chester Campbell 98 posts 209 karma points
    Apr 18, 2016 @ 15:21
    Chester Campbell
    0

    Thanks that helps a lot. A custom workflow sounds like the way to go.

  • Chris Wilson 100 posts 377 karma points
    Apr 18, 2016 @ 15:34
    Chris Wilson
    0

    Any time, good luck!

Please Sign in or register to post replies

Write your reply to:

Draft