Copied to clipboard

Flag this post as spam?

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


  • Matt Marceau 5 posts 25 karma points
    Mar 18, 2013 @ 17:22
    Matt Marceau
    0

    Default Contact Form error

    I've googled the below error, however most of the results are actually pages where it is happening, not pages with answers on how to fix it.  I'm trying to get the default contact form to send an email before i spend time modifying it to operate as needed.  Is there anything that needs to be changed for a default installation?

    Error loading MacroEngine script (file: ContactForm.cshtml)

    Below is ContactForm.cshtml

    @using System.Collections.Specialized
    @using System.Text
    @using System.Threading
    @using Bootstrap.Logic.Utils
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
    var form = Context.Request.Form;

    if (IsPost)
    {
    if (!Context.IsValidAntiForgery()) { ModelState.AddFormError(@Dictionary.InvalidPost); }
    if (form["name"].IsNullOrWhiteSpace()) { ModelState.AddError("name", @Dictionary.FormNameValidation); }
    if (!form["email"].IsMatch(MainHelper.EmailRegex)) { ModelState.AddError("email", @Dictionary.FormEmailValidation); }
    if (form["enquiry"].IsNullOrWhiteSpace()) { ModelState.AddError("enquiry", @Dictionary.FormCommentValidation); }
    }

    if (!IsPost || !ModelState.IsValid)
    {
    @Html.Raw(library.RemoveFirstParagraphTag(Model.FormText.ToString()))
    <form method="post" action="@Model.Url" class="form-horizontal validate">
    <fieldset>
    <legend>@Dictionary.FormLegend</legend>

    @*Name field*@
    <div class="control-group @{@(!ModelState.IsValidField("name") ? "error" : "")}">
    @Html.Label(Dictionary.FormName, "name", new Dictionary<string, object> { { "class", "control-label" } })
    <div class="controls">
    @Html.TextBox("name", form["name"], new Dictionary<string, object>
    {
    {"class", !ModelState.IsValidField("name") ? "input-xlarge error" : "input-xlarge"},
    {"data-val", "true"},
    {"data-val-required", Dictionary.FormNameValidation}
    })
    @if (!IsPost && ModelState.IsValid)
    {
    <span class="field-validation-valid help-inline" data-valmsg-for="name" data-valmsg-replace="true"></span>
    }
    else
    {
    @Html.ValidationMessage("name", new Dictionary<string, object>
    {
    { "class", "help-inline" },
    { "data-valmsg-for", "name" },
    { "data-valmsg-replace", "true" }
    })
    }
    </div>
    </div>

    @*Address one*@
    <div class="control-group">
    @Html.Label(Dictionary.AddressName, "address1", new Dictionary<string, object> { { "class", "control-label" } })
    <div class="controls">
    @Html.TextBox("address1", form["address1"], new Dictionary<string, object> { {"class", "input-xlarge"} })
    </div>
    </div>

    @*Adress two*@
    <div class="control-group">
    @Html.Label(Dictionary.AddressName, "address2", new Dictionary<string, object> { { "class", "hide" } })
    <div class="controls">
    @Html.TextBox("address2", form["address2"], new Dictionary<string, object> { { "class", "input-xlarge" } })
    </div>
    </div>

    @*Email field*@
    <div class="control-group @{@(!ModelState.IsValidField("email") ? "error" : "")}">
    @Html.Label(Dictionary.Email, "email", new Dictionary<string, object> { { "class", "control-label" } })
    <div class="controls">
    @Html.TextBox("email", form["email"], new Dictionary<string, object>
    {
    {"class", !ModelState.IsValidField("email") ? "input-xlarge error" : "input-xlarge"},
    {"data-val", "true"},
    {"data-val-regex", Dictionary.FormEmailValidation},
    {"data-val-regex-pattern", MainHelper.EmailRegex},
    {"data-val-required", Dictionary.FormEmailValidation}
    })
    @if (!IsPost && ModelState.IsValid)
    {
    <span class="field-validation-valid help-inline" data-valmsg-for="email" data-valmsg-replace="true"></span>
    }
    else
    {
    @Html.ValidationMessage("email", new Dictionary<string, object>
    {
    { "class", "help-inline" },
    { "data-valmsg-for", "email" },
    { "data-valmsg-replace", "true" }
    })
    }
    </div>
    </div>

    @*Enquiry field*@
    <div class="control-group @{@(!ModelState.IsValidField("enquiry") ? "error" : "")}">
    @Html.Label(Dictionary.FormComment, "enquiry", new Dictionary<string, object> { { "class", "control-label" } })
    <div class="controls">
    @Html.TextArea("enquiry", form["enquiry"], new Dictionary<string, object>
    {
    {"rows", "5"},
    {"cols", "25"},
    {"class", !ModelState.IsValidField("enquiry") ? "input-xlarge error" : "input-xlarge"},
    {"data-val", "true"},
    {"data-val-required", Dictionary.FormCommentValidation}
    })
    @if (!IsPost && ModelState.IsValid)
    {
    <span class="field-validation-valid help-inline" data-valmsg-for="enquiry" data-valmsg-replace="true"></span>
    }
    else
    {
    @Html.ValidationMessage("enquiry", new Dictionary<string, object>
    {
    { "class", "help-inline" },
    { "data-valmsg-for", "enquiry" },
    { "data-valmsg-replace", "true" }
    })
    }
    </div>
    </div>

    <div class="form-actions">
    <button type="submit" class="btn btn-primary textPw">@Dictionary.Send</button>
    </div>
    @*Anti forgery key*@
    @Context.GetAntiForgeryHtml()
    </fieldset>
    </form>
    }
    else
    {
    var ok = SendForm(form);
    if (!ok)
    {
    <div id="errorMailSettings">
    @Model.ErrorMessage
    </div>
    }
    else
    {
    // Set Thankyou text from our contact node
    <div id="thankYou">
    <h2>@Model.ThankYouHeaderText</h2>
    @Model.ThankYouMessageText
    </div>
    }
    }
    }

    @functions
    {
    public bool SendForm(NameValueCollection form)
    {
    // Get the variables from the form and set them in strings
    string strName = Library.StripHtml(form["name"]).ToString();
    string strAddressLine1 = Library.StripHtml(form["address1"]).ToString();
    string strAddressLine2 = Library.StripHtml(form["address2"]).ToString();
    string strEmailFrom = Library.StripHtml(form["email"]).ToString();
    string strMessage = Library.StripHtml(form["enquiry"]).ToString();

    // Lets set the values passed in from the Macro
    string strEmailTo = Model.EmailTo.ToString();
    string strEmailSubject = Model.EmailSubject.ToString();
    var now = DateTime.Now;
    var strTime = String.Format("{0:HH:mm:ss}", now);
    var strDate = String.Format("{0:dd/MM/yyyy}", now);

    // Let's Replace the placeholders in the email message body
    var strEmailBody = new StringBuilder(Model.EmailBody.ToString());
    strEmailBody.Replace("[Name]", strName); // Find and Replace [Name]
    strEmailBody.Replace("[AddressLine1]", strAddressLine1); // Find and Replace [AddressLine1]
    strEmailBody.Replace("[AddressLine2]", strAddressLine2); // Find and Replace [AddressLine2]
    strEmailBody.Replace("[Email]", strEmailFrom); // Find and Replace [Email]
    strEmailBody.Replace("[Message]", strMessage); // Find and Replace [Message]
    strEmailBody.Replace("[Time]", strTime); // Find and Replace [Time]
    strEmailBody.Replace("[Date]", strDate); // Find and Replace [Date]

    // Now the email is sent out to the owner, lets send out an email
    // to let the user know we have recieved their email & will respond shortly
    string strEmailReplySubject = Model.EmailReplySubject.ToString();
    var strEmailReplyBody = new StringBuilder(Model.EmailReplyBody.ToString());
    strEmailReplyBody.Replace("[Name]", strName); // Find and Replace [Name]

    return MainHelper.TrySendMail(strEmailTo, strEmailSubject, strEmailBody.ToString(), Thread.CurrentThread.CurrentUICulture, Model.Id)
    && MainHelper.TrySendMail(strEmailFrom, strEmailReplySubject, strEmailReplyBody.ToString(), Thread.CurrentThread.CurrentUICulture, Model.Id);
    }
    }

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 18, 2013 @ 18:06
    Jan Skovgaard
    0

    Hi Matt and welcome to our :)

    What version of Umbraco are you using? Perhaps some of the Razor implementation has been changed a bit since the above macro was created.

    What do you get in the trace log if you add ?umbdebugshowtrace=1 to the url where you use the macro?

    Once we have this form working you should also make sure that the smtp settings in your web.config file has been configured properly otherwise you will not receive any e-mail.

    Looking forward to hearing from you.

    /Jan

  • Matt Marceau 5 posts 25 karma points
    Mar 18, 2013 @ 18:32
    Matt Marceau
    0

    Ok I'm very new to using Umbraco.  We are on version 4.11.1.  I added the showtrace part to the url but i didn't get any trace results.  Below are screen shots of the form and after pressing submit.

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 23, 2013 @ 14:33
    Jan Skovgaard
    0

    Hi Matt

    Hmm, try checking if the umbdebug has been disabled by checking the "umbdebug" key in the web.config file - I believe you should be able to see something in the stack trace so just want to make sure it's not disabled.

    And be aware that "umdebug" is from the top of my mind. Look in the ad keys section and look for something that contains "debug" at least and if it's set to false set it to true.

    Hope this makes sense (A bit hungover today).

    /Jan

  • Matt Marceau 5 posts 25 karma points
    Mar 25, 2013 @ 14:11
    Matt Marceau
    0

    It was set to false, so I changed it to true.  however I still don't get a stack trace or any usable error message when testing the contact form.  just a bunch of blue outlined boxes.

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 29, 2013 @ 12:03
    Jan Skovgaard
    0

    Hi Matt

    Sorry for the late reply.

    Don't you have an option to scroll further down where the actual trace should appear?

    /Jan

  • Matt Marceau 5 posts 25 karma points
    Mar 29, 2013 @ 13:55
    Matt Marceau
    0

    yup, now i see the info down there.  what should i be looking for in it?  the error appears after i submit the form, but the trace disappears after submit.

Please Sign in or register to post replies

Write your reply to:

Draft