Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Feb 20, 2012 @ 17:09
    Martin
    1

    Hi, Im new to Razor, so this might a learners question.

    I've installed the Razor Contact Form and have edited the script to include a phone field.

    All this works fine. I'm getting all the fields within my contact page.

    My problem is that when the form is emailed to me, the message field is missing.

    Is there a setting that I haven't altered to accommodate an extra field?

     

    Thanks Martin


    @
        Page.FormVariables new FormVariables(ParameterModel);
        Page.Errors new List<string>();
        Page.Model Model;
    }
    <div class="form-box">
        @(HttpContext.Current.Request["form-posted"!= "1" RenderContactForm(TrySendForm())
    </div>
    @helper RenderContactForm()
    {
        <id="cultivcontactform"></a>
        <form action="#cultivcontactform" method="post" class="contact-form">
            <fieldset>
                <input type="hidden" name="form-posted" value="1" />
                @RenderTextField(Page.FormVariables.NamePage.FormVariables.NameFieldNamePage.FormVariables.NameFieldError"text")
                @RenderTextField(Page.FormVariables.PhonePage.FormVariables.PhoneFieldNamePage.FormVariables.PhoneFieldError"text")
                @RenderTextField(Page.FormVariables.EmailPage.FormVariables.EmailFieldNamePage.FormVariables.EmailFieldError"text")
                @RenderTextareaField(Page.FormVariables.MessagePage.FormVariables.MessageFieldNamePage.FormVariables.MessageFieldError"textarea")
                @RenderCheckboxField(Page.FormVariables.SendCopyPage.FormVariables.SendCopyFieldName"""checkbox")
                <div class="row">
                    <input type="submit" id="cultivcontactformsubmit" />
                    @if (Page.Errors.Count &Page.Errors.Contains("Other"== false)
                    {
                        <div class="note">@Page.FormVariables.ErrorsInForm</div>
                    }
                    @if (Page.Errors.Count &Page.Errors.Contains("Other"))
                    {
                        <div class="note">@Page.FormVariables.ErrorSendingForm</div>
                    }
                </div>
            </fieldset>
        </form>
    }
    @helper RenderTextField(string fieldLabelstring fieldNamestring errorTextstring className)
    {
        <div class="row">
            <label for="@fieldName">@fieldLabel</label>
            <input type="text" id="@fieldName" name="@fieldName" class="default @className @(Page.Errors.Contains(fieldName) ? "error" : "")" value="@HttpContext.Current.Request[fieldName]" />
            @if (Page.Errors.Contains(fieldName))
            {
                <span class="error">@errorText</span>
            }
        </div>
    }

    @helper RenderTextareaField(string fieldLabelstring fieldNamestring errorTextstring className)
    {
        <div class="row">
            <label for="@fieldName">@fieldLabel</label>
            <textarea id="@fieldName" name="@fieldName" class="textarea default @className @(Page.Errors.Contains(fieldName) ? "error" : "")" cols="30" rows="10">@HttpContext.Current.Request[fieldName]</textarea>
            @if (Page.Errors.Contains(fieldName))
            {
                <span class="error">@errorText</span>
            }
        </div>
    }
    @helper RenderCheckboxField(string fieldLabelstring fieldNamestring errorTextstring className)
     {
        <div class="row">
            <input type="checkbox" id="@fieldName" name="@fieldName" class="default @className @(Page.Errors.Contains(fieldName) ? "error" : "")" @(HttpContext.Current.Request[Page.FormVariables.SendCopyFieldName== "on" "checked=checked" ""/>
            <label for="@fieldName">@fieldLabel</label>
            @if (Page.Errors.Contains(fieldName))
            {
                <span class="error">@errorText</span>
            }
        </div>
    }
        
    @helper TrySendForm()
    {
        if (Request.Url.AbsoluteUri.Contains("form-posted"== false)
        {
            Page.Errors GetFormErrors();
            if (Page.Errors.Count == 0)
            {
                Page.Errors.AddRange(TrySendMail(PageGetMailVariables(Page)));
                var sendCopy HttpContext.Current.Request[Page.FormVariables.SendCopyFieldName== "on";
                if (sendCopy)
                {
                    Page.Errors.AddRange(TrySendMail(PageGetMailVariablesCopyToSender(Page)));
                }
                if (Page.Errors.Count == 0)
                {
                    Response.Redirect(Page.FormVariables.RedirectUrl);
                }
            }
            else
            {
                @RenderContactForm()
            }
        }
        else
        {
            <class="sent-confirmation">@Page.FormVariables.FormSentConfirmation</p>
        
    }
    @functions 
    {
        private static List<stringGetFormErrors()
        {
            var errorFields new List<string>();
            foreach (string key in HttpContext.Current.Request.Form)
            {
                if (key.EndsWith("-req"&HttpContext.Current.Request.Form[key== "")
                {
                    errorFields.Add(key);
                }
                if (key.EndsWith("-req-mail"&(HttpContext.Current.Request.Form[key== "" |System.Text.RegularExpressions.Regex.IsMatch(HttpContext.Current.Request.Form[key]@"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?"== false))
                {
                    errorFields.Add(key);
                }
            }
            return errorFields;
        }
        
        private static MailVariables GetMailVariables(dynamic page)
        {
            var mailVariables new MailVariables
                                    {
                                        Content GetMailContent(pagepage.FormVariables.MailIntroText),
                                        From page.FormVariables.MailFrom,
                                        // To make sure your mail isn't marked as spam the from address
                                        // is always the configured "from" address, make sure the domain 
                                        // corresponds with your SMTP server configuration
                                        
                                        FromName = HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.NameFieldName]),
                                        ReplyTo = HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.EmailFieldName]),
                                        Subject = page.FormVariables.MailSubject,
                                        To = page.FormVariables.MailFrom,
                                        ToName = page.FormVariables.MailFromName,
                                        EnableSsl = page.FormVariables.EnableSsl
                                    };
            return mailVariables;
        }
        
        private static MailVariables GetMailVariablesCopyToSender(dynamic page)
        {
            var mailVariables = new MailVariables
                                    {
                                        Content = GetMailContent(page, page.FormVariables.MailIntroTextCopyToSender),
                                        From = page.FormVariables.MailFrom,
                                        // To make sure your mail isn'marked as spam the from address
                                        // is always the configured "from" addressmake sure the domain 
                                        // corresponds with your SMTP server configuration
                                        
                                        FromName page.FormVariables.MailFromName,
                                        ReplyTo page.FormVariables.MailFrom,
                                        Subject page.FormVariables.MailSubjectCopyToSender,
                                        To HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.EmailFieldName]),
                                        ToName HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.NameFieldName]),
                                        EnableSsl page.FormVariables.EnableSsl
                                    };
            return mailVariables;
        }
        
        private static string GetMailContent(dynamic pagestring mailIntroText)
        {
            var name HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.NameFieldName]);
            var phone HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.PhoneFieldName]);//
            var email HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.EmailFieldName]);
            var message HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.MessageFieldName]).Replace("\n""<br />");
            var mailContent string.Format(
                                     "{0}<br /><br /><table><tr><td valign=\"top\"><p><strong>{1}</strong></p></td><td valign=\"top\"><p>{2}</p></td></tr><tr><td valign=\"top\"><p><strong>{3}</strong></p></td><td valign=\"top\"><p>{4}</p></td></tr><tr><td valign=\"top\"><p><strong>{5}</strong></p></td><td valign=\"top\"><p>{6}</p></td></tr></table>",
                                     SurroundWithArial(mailIntroText),
                                     SurroundWithArial(page.FormVariables.Name),
                                     SurroundWithArial(name),
                                     SurroundWithArial(page.FormVariables.Phone),
                                     SurroundWithArial(phone),                                        
                                     SurroundWithArial(page.FormVariables.Email),
                                     SurroundWithArial(email),
                                     SurroundWithArial(page.FormVariables.Message),
                                     SurroundWithArial(message)
                        );
            // This if and the mailTemplateProperty are here to make the code 4.7.0 compatible
            // As of 4.7.1 you only have to do
            // if(page.Model.MailTemplate.ToString(!= ""var mailTemplate page.Model.MailTemplate.ToString()//start replacements }
            if (page.Model.MailTemplate.GetType(!= typeof(umbraco.MacroEngines.DynamicNull))
            {
                var mailTemplateProperty page.Model.MailTemplate;
                var mailTemplate mailTemplateProperty.GetType(== typeof(umbraco.MacroEngines.DynamicXml)
                                       mailTemplateProperty.ToXml().ToString().Trim()
                                       mailTemplateProperty.ToString();
                if (mailTemplate != "")
                {
                    mailTemplate mailTemplate.Replace("%%INTROTEXT%%"mailIntroText);
                    mailTemplate mailTemplate.Replace("%%NAMELABEL%%"page.FormVariables.Name);
                    mailTemplate mailTemplate.Replace("%%NAME%%"name);
                    mailTemplate mailTemplate.Replace("%%PHONELABEL%%"page.FormVariables.Phone);
                    mailTemplate mailTemplate.Replace("%%PHONE%%"phone);
                    mailTemplate mailTemplate.Replace("%%EMAILLABEL%%"page.FormVariables.Email);
                    mailTemplate mailTemplate.Replace("%%EMAIL%%"email);
                    mailTemplate mailTemplate.Replace("%%MESSAGELABEL%%"page.FormVariables.Message);
                    mailTemplate mailTemplate.Replace("%%MESSAGE%%"message);
                    mailContent mailTemplate;
                }
            }
            return mailContent;
        }
        
        private static string SurroundWithArial(string input)
        {
            return string.Format("<font face=\"Arial\" size=\"3\">{0}</font>"input);
        }
        
        private static IEnumerable<stringTrySendMail(dynamic pageMailVariables mailVariables)
        {
            var mailSent SendMail(mailVariables);
            if (mailSent == false)
            {
                page.Errors.Add("Other");
            }
            return page.Errors;
        }
        public static bool SendMail(MailVariables mailVariables)
        {
            try
            {
                var mailMsg new System.Net.Mail.MailMessage
                {
                    From new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From)HttpUtility.HtmlEncode(mailVariables.FromName)),
                    Subject mailVariables.Subject,
                    Body mailVariables.Content,
                    IsBodyHtml true
                };
                mailMsg.To.Add(new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.To)HttpUtility.HtmlEncode(mailVariables.ToName)));
                mailMsg.Bcc.Add(new System.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From)));
                mailMsg.ReplyToList.Add(new System.Net.Mail.MailAddress(mailVariables.ReplyTo));
                var smtpClient new System.Net.Mail.SmtpClient EnableSsl mailVariables.EnableSsl };
                smtpClient.Send(mailMsg);
                return true;
            }
            catch (Exception ex)
            {
                umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error-1string.Format("Error creating or sending contact mail, check if there is a mailFrom property on your document and that it has a value, or specify a MailFrom parameter on the macro call exception: {0}"ex.InnerException));
            }
            return false;
        }
        public class FormVariables
        {
            public FormVariables(dynamic parameterdynamic model)
            {
                var modelMailFrom model.MailFrom.GetType(!= typeof(umbraco.MacroEngines.DynamicNullmodel.MailFrom "";
                var modelMailFromName model.MailFromName.GetType(!= typeof(umbraco.MacroEngines.DynamicNullmodel.MailFromName "";
                MailFrom GetParamValue(parameter.MailFrom?GetParamValue(modelMailFrom);
                MailFromName GetParamValue(parameter.MailFromName?GetParamValue(modelMailFromName);
                Name GetParamValue(parameter.FormLabelName?"Name*";
                Phone GetParamValue(parameter.FormLabelPhone?"Phone*";
                Email GetParamValue(parameter.FormLabelEmail?"E-mail*";
                Message GetParamValue(parameter.FormLabelMessage?"Message*";
                SendCopy GetParamValue(parameter.FormLabelSendCopy?"Send me a copy of this mail";
                ErrorsInForm GetParamValue(parameter.FormValidationError?"Please check the highlighted fields for errors";
                ErrorsendingForm GetParamValue(parameter.FormGenericError?"There was a technical error sending the form, please try again or contact us in an alternative way.";
                MailSubject GetParamValue(parameter.MailSubject?"Contact mail";
                MailIntroText GetParamValue(parameter.MailIntroText?"A contact mail has been submitted with the following details:";
                MailSubjectCopyToSender GetParamValue(parameter.MailSubjectCopyToSender?"Copy of contact mail";
                MailIntroTextCopyToSender GetParamValue(parameter.MailIntroTextCopyToSender?"You have contacted us with the information below, we will get back to you soon.";
                FormSentConfirmation GetParamValue(parameter.FormSentConfirmation?"Thank you, we will get back to you soon.";
                RedirectUrl GetParamValue(parameter.RedirectUrl?model.Url "?form-posted=1";
                bool enableSsl;
                bool.TryParse(parameter.EnableSslout enableSsl);
                EnableSsl enableSsl;
                NameFieldName "name-req";
                NameFieldError GetParamValue(parameter.NameFieldError?"Please enter your name";
                PhoneFieldName "phone-req";
                PhoneFieldError GetParamValue(parameter.PhoneFieldError?"Please enter your Phone Number";         
                EmailFieldName "email-req-mail";
                EmailFieldError GetParamValue(parameter.EmailFieldError?"Please enter a valid e-mailddress";
                MessageFieldName "message-req";
                MessageFieldError GetParamValue(parameter.MessageFieldError?"Please enter a message";
                SendCopyFieldName "sendcopy";
            }
            public string MailFrom getset}
            public string MailFromName getset}
            public string Name getset}
            public string Phone getset}
            public string Email getset}
            public string Message getset}
            public string SendCopy getset}
            public string ErrorsInForm getset}
            public string ErrorsendingForm getset}
            public string MailSubject getset}
            public string MailIntroText getset}
            public string MailSubjectCopyToSender getset}
            public string MailIntroTextCopyToSender getset}
            public string FormSentConfirmation getset}
            public string RedirectUrl getset}
            public bool EnableSsl getset}
            public string NameFieldName getset}
            public string NameFieldError getset}
            public string PhoneFieldName getset}
            public string PhoneFieldError getset}
            public string EmailFieldName getset}
            public string EmailFieldError getset}
            public string MessageFieldName getset}
            public string MessageFieldError getset}
            public string SendCopyFieldName getset}
            public static string GetParamValue(dynamic input)
            {
                if (input != null &input.StartsWith("[!"&input.EndsWith("]"))
                {
                    var dictKey input.Substring(2input.LastIndexOf("]"2);
                    input library.GetDictionaryItem(dictKey);
                }
                return input;
            }
        }
        public class MailVariables
        {
            public string Content getset}
            public string Subject getset}
            public string To getset}
            public string ToName getset}
            public string From getset}
            public string FromName getset}
            public string ReplyTo getset}
            public bool EnableSsl getset}
        }
    }

     

     

  • Martin 278 posts 662 karma points
    Feb 23, 2012 @ 15:45
    Martin
    1

    Solved it.

    I had to add the <td> to be outputted.

  • Proxicode 127 posts 323 karma points
    Mar 31, 2015 @ 23:18
    Proxicode
    0

    Thank you so much @Martin!! I am embarrassed to say how much time I wasted on this...wish I would have done a search a long time ago. High Five for you!!

Please Sign in or register to post replies

Write your reply to:

Draft