Example #1
0
        /// <summary>
        /// Deserializes the configuration for an email handler.
        /// </summary>
        /// <param name="configuration">
        /// The serialized configuration.
        /// </param>
        /// <returns>
        /// The deserialized configuration.
        /// </returns>
        public object DeserializeConfiguration(string configuration)
        {
            // Variables.
            var recipients = new List<string>();
            var config = new EmailConfiguration()
            {
                Recipients = recipients
            };
            var configData = JsonHelper.Deserialize<JObject>(configuration);
            var dynamicConfig = configData as dynamic;
            var properties = configData.Properties().Select(x => x.Name);
            var propertySet = new HashSet<string>(properties);

            // Get recipients.
            if (propertySet.Contains("recipients"))
            {
                foreach (var recipient in dynamicConfig.recipients)
                {
                    recipients.Add(recipient.email.Value as string);
                }
            }

            // Get simple properties.
            if (propertySet.Contains("senderEmail"))
            {
                config.SenderEmail = dynamicConfig.senderEmail.Value;
            }
            if (propertySet.Contains("appendFields"))
            {
                config.AppendFields = dynamicConfig.appendFields.Value;
            }
            if (propertySet.Contains("appendPayload"))
            {
                config.AppendPayload = dynamicConfig.appendPayload.Value;
            }
            if (propertySet.Contains("message"))
            {
                config.Message = dynamicConfig.message.Value;
            }
            if (propertySet.Contains("subject"))
            {
                config.Subject = dynamicConfig.subject.Value;
            }

            // Return the email configuration.
            return config;
        }
Example #2
0
        /// <summary>
        /// Constructs an email message from the form fields.
        /// </summary>
        /// <param name="form">
        /// The form being submitted.
        /// </param>
        /// <param name="data">
        /// The form fields.
        /// </param>
        /// <param name="files">
        /// The form files.
        /// </param>
        /// <param name="payload">
        /// Extra data related to the submission.
        /// </param>
        /// <param name="config">
        /// The email configuration.
        /// </param>
        /// <returns>
        /// The email message.
        /// </returns>
        private string ConstructMessage(Form form, IEnumerable<FieldSubmission> data,
            IEnumerable<FileFieldSubmission> files, IEnumerable<PayloadSubmission> payload,
            EmailConfiguration config)
        {
            // Variables.
            var nl = Environment.NewLine;
            var lines = new List<string>();
            var valuesById = data.GroupBy(x => x.FieldId).Select(x => new
            {
                Id = x.Key,
                Values = x.SelectMany(y => y.FieldValues).ToList()
            }).ToDictionary(x => x.Id, x => x.Values);
            var filesById = files.GroupBy(x => x.FieldId).Select(x => new
            {
                Id = x.Key,
                Filename = x.Select(y => y.FileName).FirstOrDefault()
            }).ToDictionary(x => x.Id, x => x.Filename);
            var fieldsById = form.Fields.ToDictionary(x => x.Id, x => x);

            // Payload items.
            foreach (var item in payload)
            {
                lines.Add($"{item.Name}: {item.Value}");
            }

            // Normal fields.
            foreach (var key in valuesById.Keys)
            {
                var values = valuesById[key];
                var formatted = string.Join(", ", values);
                var field = default(IFormField);
                var fieldName = "Unknown Field";
                if (fieldsById.TryGetValue(key, out field))
                {
                    fieldName = field.Name;
                    formatted = field.FormatValue(values, FieldPresentationFormats.Email);
                }
                var line = string.Format("{0}: {1}", fieldName, formatted);
                lines.Add(line);
            }

            // File fields.
            foreach (var key in filesById.Keys)
            {
                var filename = filesById[key];
                var field = default(IFormField);
                var fieldName = "Unknown Field";
                if (fieldsById.TryGetValue(key, out field))
                {
                    fieldName = field.Name;
                }
                var line = string.Format(@"{0}: See attachment, ""{1}""", fieldName, filename);
                lines.Add(line);
            }

            // Return message.
            return config.Message + nl + string.Join(nl, lines);
        }
Example #3
0
        /// <summary>
        /// Constructs an email message from the form fields.
        /// </summary>
        /// <param name="form">
        /// The form being submitted.
        /// </param>
        /// <param name="data">
        /// The form fields.
        /// </param>
        /// <param name="files">
        /// The form files.
        /// </param>
        /// <param name="payload">
        /// Extra data related to the submission.
        /// </param>
        /// <param name="config">
        /// The email configuration.
        /// </param>
        /// <returns>
        /// The email message.
        /// </returns>
        private string ConstructMessage(Form form, IEnumerable <FieldSubmission> data,
                                        IEnumerable <FileFieldSubmission> files, IEnumerable <PayloadSubmission> payload,
                                        EmailConfiguration config)
        {
            // Variables.
            var nl         = Environment.NewLine;
            var lines      = new List <string>();
            var valuesById = data.GroupBy(x => x.FieldId).Select(x => new
            {
                Id     = x.Key,
                Values = x.SelectMany(y => y.FieldValues).ToList()
            }).ToDictionary(x => x.Id, x => x.Values);
            var filesById = files.GroupBy(x => x.FieldId).Select(x => new
            {
                Id       = x.Key,
                Filename = x.Select(y => y.FileName).FirstOrDefault()
            }).ToDictionary(x => x.Id, x => x.Filename);
            var fieldsById = form.Fields.ToDictionary(x => x.Id, x => x);


            // Payload items.
            foreach (var item in payload)
            {
                lines.Add($"{item.Name}: {item.Value}");
            }


            // Normal fields.
            foreach (var key in valuesById.Keys)
            {
                var values    = valuesById[key];
                var formatted = string.Join(", ", values);
                var field     = default(IFormField);
                var fieldName = "Unknown Field";
                if (fieldsById.TryGetValue(key, out field))
                {
                    fieldName = field.Name;
                    formatted = field.FormatValue(values, FieldPresentationFormats.Email);
                }
                var line = string.Format("{0}: {1}", fieldName, formatted);
                lines.Add(line);
            }


            // File fields.
            foreach (var key in filesById.Keys)
            {
                var filename  = filesById[key];
                var field     = default(IFormField);
                var fieldName = "Unknown Field";
                if (fieldsById.TryGetValue(key, out field))
                {
                    fieldName = field.Name;
                }
                var line = string.Format(@"{0}: See attachment, ""{1}""", fieldName, filename);
                lines.Add(line);
            }


            // Return message.
            return(config.Message + nl + string.Join(nl, lines));
        }