/// <summary> /// Method that creates a list of attachments from the given files /// </summary> /// <param name="uploadedFiles"></param> /// <returns>Return a list of attachments</returns> public static List <Attachment> CreateFormHandlerMailAttachments(Telerik.Web.UI.UploadedFileCollection uploadedFiles) { List <Attachment> attachments = new List <Attachment>(); if (uploadedFiles.Count > 0) { foreach (Telerik.Web.UI.UploadedFile uploadedFile in uploadedFiles) { Attachment attachment = new Attachment(uploadedFile.InputStream, uploadedFile.FileName); attachments.Add(attachment); } } return(attachments); }
/// <summary> /// Method that builds and sends confirmation messages to the administrator and, /// if <see cref="SendClientConfirmation"/> is true, the client too. /// </summary> private void SendSubmittedFormDataEmail() { StringBuilder formDataBuilder = new StringBuilder(); StringBuilder formDataHTMLBuilder = new StringBuilder(); List <Attachment> attachments = new List <Attachment>(); foreach (var fieldControl in this.FieldControls) { if (fieldControl is FormCheckboxes) { StringBuilder choicesString = new StringBuilder(); var choices = ((List <String>)((FormCheckboxes)fieldControl).Value).ToList(); for (int i = 0; i < choices.Count(); i++) { choicesString.Append(choices[i]); if (i != choices.Count() - 1) { choicesString.Append(", "); } } formDataBuilder.Append(String.Format("{0}: {1}\r\n", ((FormCheckboxes)fieldControl).Title, choicesString.ToString())); formDataHTMLBuilder.Append(String.Format("{0}: {1}<br/>", ((FormCheckboxes)fieldControl).Title, choicesString.ToString())); } else if (fieldControl is FieldControl) { var control = fieldControl as FieldControl; if (MailUploadedFilesAsAttachments && control.Value.GetType().Equals(typeof(Telerik.Web.UI.UploadedFileCollection))) { Telerik.Web.UI.UploadedFileCollection uploadedFileCollection = (Telerik.Web.UI.UploadedFileCollection)control.Value; attachments.AddRange(FormHandlerMail.CreateFormHandlerMailAttachments(uploadedFileCollection)); } else if (fieldControl.GetType() != typeof(FormCaptcha)) { formDataBuilder.Append(String.Format("{0}: {1}\r\n", control.Title, control.Value)); formDataHTMLBuilder.Append(String.Format("{0}: {1}<br/>", control.Title, control.Value)); } } } // Try get client e-mailaddress if (!String.IsNullOrWhiteSpace(this.ClientConfirmationEmailAddressFormControlName)) { FormTextBox emailFormControl = this.FieldControls.OfType <FormTextBox>().Where(f => f.MetaField.FieldName == this.ClientConfirmationEmailAddressFormControlName).FirstOrDefault(); if (emailFormControl != null) { clientEmailAddress = emailFormControl.Value.ToString(); } } // Try get client ClientConfirmationControl if (!String.IsNullOrWhiteSpace(this.ClientConfirmationControlName) && !String.IsNullOrWhiteSpace(this.clientConfirmationControlSendOptionValue) && SendClientConfirmation) { FieldControl clientConfirmationControlName = this.FieldControls.OfType <FieldControl>().Where(f => f.DataFieldName == this.ClientConfirmationControlName).FirstOrDefault(); if (clientConfirmationControlName != null) { SendClientConfirmation = false; if (clientConfirmationControlName is FormCheckboxes) { var choices = ((List <String>)((FormCheckboxes)clientConfirmationControlName).Value).ToList(); foreach (var selectedChoices in choices) { if (selectedChoices.ToUpper().Equals(clientConfirmationControlSendOptionValue.ToUpper())) { SendClientConfirmation = true; break; } } } else if (clientConfirmationControlName is FormMultipleChoice) { if (clientConfirmationControlSendOptionValue.ToUpper().Equals(clientConfirmationControlName.Value.ToString().ToUpper())) { SendClientConfirmation = true; } } } } #region Administrator mail string bodyText = string.Empty; if (!string.IsNullOrWhiteSpace(emailBody)) { bodyText = emailBody.Replace(SUBMITTED_FORM_DATA_STRING_TAG, formDataBuilder.ToString()); } string bodyHTMLText = string.Empty; if (!string.IsNullOrWhiteSpace(emailBodyHTML)) { bodyHTMLText = emailBodyHTML.Replace(SUBMITTED_FORM_DATA_STRING_TAG, formDataHTMLBuilder.ToString()); } string actualReferrerUrlSessionKey = !String.IsNullOrWhiteSpace(ReferrerUrlSessionKey) ? ReferrerUrlSessionKey : "REFERRER_URL"; string referrerUrl = String.Empty; if (HttpContext.Current.Session[actualReferrerUrlSessionKey] != null) { referrerUrl = ((Uri)HttpContext.Current.Session[actualReferrerUrlSessionKey]).ToString(); } bodyHTMLText = bodyHTMLText.Replace(REFERRER_URL_TAG, referrerUrl); string fromEmailAddressToUse = !String.IsNullOrWhiteSpace(clientEmailAddress) && IsClientEmailAddressUsedAsFromEmailAddress ? clientEmailAddress : fromEmailAddress; FormHandlerMail.SendFormHandlerEmailMessage(fromEmailAddressToUse, ToEmailAddresses, ccEmailAddresses, emailSubject, bodyText, bodyHTMLText, attachments, this.FormName); #endregion #region Client confirmation if (SendClientConfirmation && !String.IsNullOrWhiteSpace(clientEmailAddress)) { string clientBodyText = string.Empty; if (!string.IsNullOrWhiteSpace(clientEmailBody)) { clientBodyText = clientEmailBody.Replace(SUBMITTED_FORM_DATA_STRING_TAG, formDataBuilder.ToString()); } string clientBodyHTMLText = string.Empty; if (!string.IsNullOrWhiteSpace(clientEmailBodyHTML)) { clientBodyHTMLText = clientEmailBodyHTML.Replace(SUBMITTED_FORM_DATA_STRING_TAG, formDataHTMLBuilder.ToString()); } FormHandlerMail.SendFormHandlerEmailMessage(clientFromEmailAddress, clientEmailAddress, clientCcEmailAddresses, clientEmailSubject, clientBodyText, clientBodyHTMLText, null, this.FormName); } #endregion }