예제 #1
0
        public ActionResult ContactUs(ContactFormViewModel model)
        {
            try
            {
                // This code will create a Document of type Contact Submission and add it to the Contact Us Submissions list in the Umbraco Back end
                // This is done in case there are any issues with the emailer.
                // Get Submissions List Node ID
                var submissionsNodeId = Umbraco.TypedContentAtRoot().First(_ => _.Name == "Contact Us Submissions").Id;
                // Create a new ContactSubmission document and title it the users name
                IContent doc = ApplicationContext.Services.ContentService.CreateContent(model.FirstName + " " + model.LastName, submissionsNodeId, "ContactSubmission");
                // Populate all the data
                doc.Properties["messageTopic"].Value = model.MessageType;
                doc.Properties["firstName"].Value    = model.FirstName;
                doc.Properties["lastName"].Value     = model.LastName;
                doc.Properties["email"].Value        = model.Email;
                doc.Properties["phoneNumber"].Value  = model.PhoneNumber;
                doc.Properties["message"].Value      = model.Message;
                // If this is a logged in user
                if (User.Identity.IsAuthenticated)
                {
                    doc.Properties["memberId"].Value = model.MemberId;
                }
                // Save (but do not publish) the contact submision
                ApplicationContext.Services.ContentService.Save(doc);

                string mailData = CurrentPage.GetPropertyValue <string>("categoriesAndEmails");
                IDictionary <string, IEnumerable <string> > categoriesAndEmails = ContactFormViewModel.GetCategoriesAndEmails(mailData);
                IEnumerable <string> emails = categoriesAndEmails[model.MessageType];

                const string na = "N/A";
                // Build a dictionary for all the dynamic text in the email template
                var dynamicText = new Dictionary <string, string>
                {
                    { "<%MemberId%>", model.MemberId ?? na },
                    { "<%FirstName%>", model.FirstName },
                    { "<%LastName%>", model.LastName },
                    { "<%YNumber%>", model.YNumber ?? na },
                    { "<%Username%>", model.Username ?? na },
                    { "<%Email%>", model.Email },
                    { "<%Phone%>", model.PhoneNumber },
                    { "<%MessageBody%>", model.Message }
                };
                // Get the Umbraco root node to access dynamic information (phone numbers, emails, ect)
                IPublishedContent root = Umbraco.TypedContentAtRoot().First();

                //Get the Contact Us Email Template ID
                object emailTemplateId = null;
                string smtpEmail       = null;
                string template        = CurrentPage.GetPropertyValue <string>("emailTemplate");
                if (template == "Member")
                {
                    emailTemplateId = root.GetProperty("memberContactUsTemplate").Value;
                    smtpEmail       = root.GetProperty("smtpEmailAddress").Value.ToString();
                }
                if (template == "Provider")
                {
                    emailTemplateId = root.GetProperty("providerContactUsTemplate").Value;
                    smtpEmail       = root.GetProperty("providerSmtpEmailAddress").Value.ToString();
                }
                if (template == "Broker")
                {
                    emailTemplateId = root.GetProperty("brokerContactUsTemplate").Value;
                    smtpEmail       = root.GetProperty("brokerSmtpEmailAddress").Value.ToString();
                }

                foreach (string email in emails)
                {
                    try
                    {
                        SendEmail(email, model.MessageType, BuildEmail((int)emailTemplateId, dynamicText), smtpEmail);
                    }
                    catch (Exception ex)
                    {
                        // Create an error message with sufficient info to contact the user
                        string additionalInfo = model.FirstName + " " + model.LastName + " could not send a contact us email request. Please contact at " + model.Email;
                        // Add the error message to the log4net output
                        log4net.GlobalContext.Properties["additionalInfo"] = additionalInfo;
                        // Log the error
                        logger.Error("Unable to complete Contact Us submission due to SMTP error", ex);
                        logger.Error("Template: " + (template != null ? template : "error") + " - " + "templateID: " + (emailTemplateId != null ? emailTemplateId.ToString() : "error") + " : " + (smtpEmail != null ? smtpEmail : "error"));
                        // Set the sucess flag to true and post back to the same page
                        TempData["IsSuccessful"] = false;
                        return(RedirectToCurrentUmbracoPage());
                    }
                }

                // Set the sucess flag to true and post back to the same page
                TempData["IsSuccessful"] = true;
                return(RedirectToCurrentUmbracoPage());
            }
            catch (Exception ex) // If the message failed to send
            {
                // Create an error message with sufficient info to contact the user
                string additionalInfo = model.FirstName + " " + model.LastName + " could not send a contact us email request. Please contact at " + model.Email;
                // Add the error message to the log4net output
                log4net.GlobalContext.Properties["additionalInfo"] = additionalInfo;
                // Log the error
                logger.Error("Unable to complete Contact Us submission", ex);
                // Set the success flag to false and post back to the same page
                TempData["IsSuccessful"] = false;
                return(RedirectToCurrentUmbracoPage());
            }
        }