Thanks. Used your code but the message is not in email.
My code:
@{Page.FormVariables=newFormVariables(Parameter,Model);Page.Errors=newList<string>();Page.Model=Model;}<divclass="form-box">@(HttpContext.Current.Request["form-posted"]!="1"?RenderContactForm():TrySendForm())</div>@helper RenderContactForm(){<aid="cultivcontactform"></a><formaction="#cultivcontactform"method="post"class="contact-form"><fieldset><inputtype="hidden"name="form-posted"value="1"/>@RenderTextField(Page.FormVariables.Name,Page.FormVariables.NameFieldName,Page.FormVariables.NameFieldError,"text")@RenderTextField(Page.FormVariables.Phone,Page.FormVariables.PhoneFieldName,Page.FormVariables.PhoneFieldError,"text")@RenderTextField(Page.FormVariables.Email,Page.FormVariables.EmailFieldName,Page.FormVariables.EmailFieldError,"text")@RenderTextareaField(Page.FormVariables.Message,Page.FormVariables.MessageFieldName,Page.FormVariables.MessageFieldError,"textarea")@RenderCheckboxField(Page.FormVariables.SendCopy,Page.FormVariables.SendCopyFieldName,"","checkbox")<divclass="row"><inputtype="submit"value="Send"id="cultivcontactformsubmit"/>@if(Page.Errors.Count>0&&Page.Errors.Contains("Other")==false){<divclass="note">@Page.FormVariables.ErrorsInForm</div>}@if(Page.Errors.Count>0&&Page.Errors.Contains("Other")){<divclass="note">@Page.FormVariables.ErrorSendingForm</div>}</div></fieldset></form>}@helper RenderTextField(stringfieldLabel,stringfieldName,stringerrorText,stringclassName){<divclass="row">@if(Page.Errors.Contains(fieldName)){<spanclass="error">@errorText</span>}<labelfor="@fieldName">@fieldLabel</label><br/><inputtype="text"id="@fieldName"name="@fieldName"class="default @className@(Page.Errors.Contains(fieldName)?"error":"")"value="@HttpContext.Current.Request[fieldName]"/></div>}@helper RenderTextareaField(stringfieldLabel,stringfieldName,stringerrorText,stringclassName){<divclass="row">@if(Page.Errors.Contains(fieldName)){<spanclass="error">@errorText</span>}<labelfor="@fieldName">@fieldLabel</label><br/><textareaid="@fieldName"name="@fieldName"class="textarea default @className@(Page.Errors.Contains(fieldName)?"error":"")"cols="30"rows="10">@HttpContext.Current.Request[fieldName]</textarea></div>}@helper RenderCheckboxField(stringfieldLabel,stringfieldName,stringerrorText,stringclassName){<divclass="row sendToMe">@if(Page.Errors.Contains(fieldName)){<spanclass="error">@errorText</span>}<inputtype="checkbox"id="@fieldName"name="@fieldName"class="default @className@(Page.Errors.Contains(fieldName)?"error":"")"@(HttpContext.Current.Request[Page.FormVariables.SendCopyFieldName]=="on"?"checked=checked":"")/><labelfor="@fieldName">@fieldLabel</label></div>}@helper TrySendForm(){if(Request.Url.AbsoluteUri.Contains("form-posted")==false){Page.Errors=GetFormErrors();if(Page.Errors.Count==0){Page.Errors.AddRange(TrySendMail(Page,GetMailVariables(Page)));varsendCopy=HttpContext.Current.Request[Page.FormVariables.SendCopyFieldName]=="on";if(sendCopy){Page.Errors.AddRange(TrySendMail(Page,GetMailVariablesCopyToSender(Page)));}if(Page.Errors.Count==0){Response.Redirect(Page.FormVariables.RedirectUrl);}}else{@RenderContactForm()}}else{<pclass="sent-confirmation">@Page.FormVariables.FormSentConfirmation</p>}}@functions {privatestaticList<string>GetFormErrors(){varerrorFields=newList<string>();foreach(stringkeyinHttpContext.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);}}returnerrorFields;}privatestaticMailVariablesGetMailVariables(dynamicpage){varmailVariables=newMailVariables{Content=GetMailContent(page,page.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 configurationFromName=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};returnmailVariables;}privatestaticMailVariablesGetMailVariablesCopyToSender(dynamicpage){varmailVariables=newMailVariables{Content=GetMailContent(page,page.FormVariables.MailIntroTextCopyToSender),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 configurationFromName=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};returnmailVariables;}privatestaticstringGetMailContent(dynamicpage,stringmailIntroText){varphone=HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.PhoneFieldName]);varname=HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.NameFieldName]);varemail=HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.EmailFieldName]);varmessage=HttpUtility.HtmlEncode(HttpContext.Current.Request[page.FormVariables.MessageFieldName]).Replace("\n","<br />");varmailContent=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)){varmailTemplateProperty=page.Model.MailTemplate;varmailTemplate=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;}}returnmailContent;}privatestaticstringSurroundWithArial(stringinput){returnstring.Format("<font face=\"Arial\" size=\"3\">{0}</font>",input);}privatestaticIEnumerable<string>TrySendMail(dynamicpage,MailVariablesmailVariables){varmailSent=SendMail(mailVariables);if(mailSent==false){page.Errors.Add("Other");}returnpage.Errors;}publicstaticboolSendMail(MailVariablesmailVariables){try{varmailMsg=newSystem.Net.Mail.MailMessage{From=newSystem.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From),HttpUtility.HtmlEncode(mailVariables.FromName)),Subject=mailVariables.Subject,Body=mailVariables.Content,IsBodyHtml=true};mailMsg.To.Add(newSystem.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.To),HttpUtility.HtmlEncode(mailVariables.ToName)));mailMsg.Bcc.Add(newSystem.Net.Mail.MailAddress(HttpUtility.HtmlEncode(mailVariables.From)));mailMsg.ReplyToList.Add(newSystem.Net.Mail.MailAddress(mailVariables.ReplyTo));varsmtpClient=newSystem.Net.Mail.SmtpClient{EnableSsl=mailVariables.EnableSsl};smtpClient.Send(mailMsg);returntrue;}catch(Exceptionex){umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,-1,string.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));}returnfalse;}publicclassFormVariables{publicFormVariables(dynamicparameter,dynamicmodel){varmodelMailFrom=model.MailFrom.GetType()!=typeof(umbraco.MacroEngines.DynamicNull)?model.MailFrom:"";varmodelMailFromName=model.MailFromName.GetType()!=typeof(umbraco.MacroEngines.DynamicNull)?model.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";boolenableSsl;bool.TryParse(parameter.EnableSsl,outenableSsl);EnableSsl=enableSsl;NameFieldName="name-req";NameFieldError=GetParamValue(parameter.NameFieldError)??"Please enter your name";PhoneFieldName="phone-req";PhoneFieldError=GetParamValue(parameter.PhoneFieldError)??"Please enter your phone no.";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";}publicstringMailFrom{get;set;}publicstringMailFromName{get;set;}publicstringName{get;set;}publicstringPhone{get;set;}publicstringEmail{get;set;}publicstringMessage{get;set;}publicstringSendCopy{get;set;}publicstringErrorsInForm{get;set;}publicstringErrorsendingForm{get;set;}publicstringMailSubject{get;set;}publicstringMailIntroText{get;set;}publicstringMailSubjectCopyToSender{get;set;}publicstringMailIntroTextCopyToSender{get;set;}publicstringFormSentConfirmation{get;set;}publicstringRedirectUrl{get;set;}publicboolEnableSsl{get;set;}publicstringNameFieldName{get;set;}publicstringNameFieldError{get;set;}publicstringPhoneFieldName{get;set;}publicstringPhoneFieldError{get;set;}publicstringEmailFieldName{get;set;}publicstringEmailFieldError{get;set;}publicstringMessageFieldName{get;set;}publicstringMessageFieldError{get;set;}publicstringSendCopyFieldName{get;set;}publicstaticstringGetParamValue(dynamicinput){if(input!=null&&input.StartsWith("[!")&&input.EndsWith("]")){vardictKey=input.Substring(2,input.LastIndexOf("]")-2);input=library.GetDictionaryItem(dictKey);}returninput;}}publicclassMailVariables{publicstringContent{get;set;}publicstringSubject{get;set;}publicstringTo{get;set;}publicstringToName{get;set;}publicstringFrom{get;set;}publicstringFromName{get;set;}publicstringReplyTo{get;set;}publicboolEnableSsl{get;set;}}}
Adding a Phone field
Hi there
I've tried to add a phone field, by comparing the current fields and their values.
So i've gotten to this, which gives me an error:
Error loading MacroEngine script (file: ~/macroscripts/cultivcontactform.cshtml)
Code:
Any help is much appreciated!
Ah DOH!
I forgot to correct the second set of NameFieldName to PhoneFieldName and NameFieldError to PhoneFieldError.
My bad!
Thanks. Used your code but the message is not in email.
My code:
WUPS!!!
Messeage just nedded in mailContent.....
Again thanks for your code.
is working on a reply...