public EmailAPIResultModel Post([FromBody] EmailAPIModel emailAPIModel) { int mediaId = -1; string errorString = ""; try { if (emailAPIModel == null) { errorString = T("The provided data does not correspond to the required format.").ToString(); } else { ContactFormRecord contactForm = _contactFormService.GetContactForm(emailAPIModel.ContentId); if (contactForm == null) { errorString = T("The content Id has not been provided or does not correspond to a content part of the correct type.").ToString(); } if (errorString == "") { errorString = _contactFormService.ValidateAPIRequest(emailAPIModel.SenderName, emailAPIModel.SendFrom, emailAPIModel.MessageText, contactForm.RequireNameField, (emailAPIModel.Attachment.Length > 0), contactForm.RequireAttachment); } if (errorString == "" && !string.IsNullOrWhiteSpace(emailAPIModel.Attachment) && !string.IsNullOrWhiteSpace(emailAPIModel.AttachmentName)) { if (_contactFormService.FileAllowed(emailAPIModel.AttachmentName)) { mediaId = _contactFormService.UploadFromBase64(emailAPIModel.Attachment, contactForm.PathUpload, emailAPIModel.AttachmentName); } else { errorString = T("The file extension in the filename is not allowed or has not been provided.").ToString(); } } if (errorString == "") { _contactFormService.SendContactEmail(emailAPIModel.SenderName, emailAPIModel.SendFrom, emailAPIModel.SendFrom, emailAPIModel.MessageSubject, emailAPIModel.MessageText, mediaId, contactForm, emailAPIModel.AdditionalData); } } } catch (Exception e) { errorString = e.Message; } return(new EmailAPIResultModel { Error = errorString, Information = "" }); }
/// <summary> /// Sends the contact email. /// </summary> /// <param name="id">The id.</param> /// <param name="returnUrl">The return URL.</param> /// <param name="name">The name.</param> /// <param name="email">The bot false email.</param> /// <param name="confirmEmail">The actual email string</param> /// <param name="subject">The subject.</param> /// <param name="message">The message.</param> public ActionResult SendContactEmail(int id, string returnUrl, string name, string email, string confirmEmail, string subject, string message, int mediaid = -1, int Accept = 0) { var redirectionUrl = returnUrl; try { ContactFormRecord contactForm = _contactFormService.GetContactForm(id); if (contactForm.AcceptPolicy && Accept != 1) { TempData["form"] = Request.Form; return(this.RedirectLocal(Request.UrlReferrer.ToString())); } if (contactForm != null) { // If a static subject message was specified, use that value for the email subject. if (contactForm.UseStaticSubject) { if (contactForm.StaticSubjectMessage != null) { subject = contactForm.StaticSubjectMessage.Replace("{NAME}", name); } if (Request.Url != null) { subject = subject.Replace("{DOMAIN}", Request.Url.Host); } } _contactFormService.SendContactEmail(name, confirmEmail, email, subject, message, mediaid, contactForm, _orchardServices.WorkContext.HttpContext.Request.Form); if (!string.IsNullOrWhiteSpace(contactForm.ThankyouPage)) { redirectionUrl = contactForm.ThankyouPage; } } } catch { // L'eccezione serve solo per la chiamata via APIController, mentre per la chiamata via form è già stata loggata e salvata nel Notifier TempData["form"] = Request.Form; redirectionUrl = returnUrl; } return(this.RedirectLocal(redirectionUrl, "~/")); }
/// <summary> /// Sends the contact email. /// </summary> /// <param name="id">The id.</param> /// <param name="returnUrl">The return URL.</param> /// <param name="name">The name.</param> /// <param name="email">The bot false email.</param> /// <param name="subject">The subject.</param> /// <param name="message">The message.</param> /// <param name="confirmEmail">The actual email string</param> public ActionResult SendContactEmail(int id, string returnUrl, string name, string email, string confirmEmail, string subject, string message) { var contactForm = _contentManager.Get <ContactFormPart>(id); if (contactForm != null) { // If a static subject message was specified, use that value for the email subject. if (contactForm.UseStaticSubject) { if (contactForm.StaticSubjectMessage != null) { subject = contactForm.StaticSubjectMessage.Replace("{NAME}", name); } if (Request.Url != null) { subject = subject.Replace("{DOMAIN}", Request.Url.Host); } } _contactFormService.SendContactEmail(name, confirmEmail, email, subject, message, contactForm.RecipientEmailAddress, contactForm.RequireNameField); } return(this.RedirectLocal(returnUrl, "~/")); }
/// <summary> /// Sends the contact email. /// </summary> /// <param name="id">The id.</param> /// <param name="returnUrl">The return URL.</param> /// <param name="name">The name.</param> /// <param name="email">The bot false email.</param> /// <param name="confirmEmail">The actual email string</param> /// <param name="subject">The subject.</param> /// <param name="message">The message.</param> public ActionResult SendContactEmail(int id, string returnUrl, string name, string email, string confirmEmail, string subject, string message, int mediaId = -1, int accept = 0) { var redirectionUrl = returnUrl; try { // we want to create a new contentItem of the same type as the form we are posting var stubItem = _contentManager.Get <ContactFormPart>(id); // then we will try to launch UPdateEditor to test recaptcha. if (stubItem != null) { _frontEndEditeService.BuildFrontEndShape( _contentManager.UpdateEditor(stubItem, this), OnlyShowReCaptcha, NoFields); if (!ModelState.IsValid) { // consider logging // update of recaptcha failed _orchardServices.TransactionManager.Cancel(); TempData["form"] = Request.Form; return(this.RedirectLocal(redirectionUrl, "~/")); } } ContactFormRecord contactForm = _contactFormService.GetContactForm(id); if (contactForm.AcceptPolicy && accept != 1) { TempData["form"] = Request.Form; return(this.RedirectLocal(Request.UrlReferrer.ToString())); } #region Validation Field bool isValid = true; const string emailAddressRegex = @"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$"; if ((contactForm.RequireNameField && String.IsNullOrEmpty(name)) || (contactForm.RequireAttachment && !(mediaId != -1)) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(message) || string.IsNullOrEmpty(confirmEmail)) { if (string.IsNullOrEmpty(email)) { ModelState.AddModelError("email", T("The email is mandatory.").Text); } if (string.IsNullOrEmpty(confirmEmail)) { ModelState.AddModelError("confirmEmail", T("The confirm email is mandatory.").Text); } if (contactForm.RequireNameField && String.IsNullOrEmpty(name)) { ModelState.AddModelError("name", T("The sender name is mandatory.").Text); } if (contactForm.RequireAttachment && !(mediaId != -1)) { ModelState.AddModelError("mediaId", T("The attachment is mandatory.").Text); } if (string.IsNullOrEmpty(message)) { ModelState.AddModelError("message", T("The message text is mandatory.").Text); } isValid = false; } else { Match emailMatch = Regex.Match(email, emailAddressRegex); Match confirmEmailMatch = Regex.Match(confirmEmail, emailAddressRegex); if (!emailMatch.Success) { ModelState.AddModelError("email", T("Invalid email address.").Text); isValid = false; } if (!confirmEmailMatch.Success) { ModelState.AddModelError("confirmEmail", T("Invalid confirm email address.").Text); isValid = false; } if (isValid && email != confirmEmail) { ModelState.AddModelError("confirmEmail", T("Confirm email must be matching to the email.").Text); isValid = false; } } if (!isValid) { TempData["form"] = Request.Form; _notifier.Add(NotifyType.Error, MessageError(ModelState)); return(this.RedirectLocal(redirectionUrl, "~/")); } #endregion #region TriggerValidation _workflowManager.TriggerEvent("ContactFormValidating", stubItem, () => new Dictionary <string, object> { { "Content", stubItem }, { "ModelState", ModelState }, { "FormData", Request.Form }, { "Updater", this }, { "T", T } }); if (!ModelState.IsValid) { TempData["form"] = Request.Form; _notifier.Add(NotifyType.Error, MessageError(ModelState)); return(this.RedirectLocal(redirectionUrl, "~/")); } #endregion if (contactForm != null) { // If a static subject message was specified, use that value for the email subject. if (contactForm.UseStaticSubject) { if (contactForm.StaticSubjectMessage != null) { subject = contactForm.StaticSubjectMessage.Replace("{NAME}", name); } if (Request.Url != null) { subject = subject.Replace("{DOMAIN}", Request.Url.Host); } } _contactFormService.SendContactEmail(name, confirmEmail, email, subject, message, mediaId, contactForm, _orchardServices.WorkContext.HttpContext.Request.Form); // after sending email it triggers a worflow event in order to execute arbitrary code. _workflowManager.TriggerEvent("ContactFormSubmittedEvent", stubItem, () => new Dictionary <string, object> { { "Content", stubItem } }); if (!string.IsNullOrWhiteSpace(contactForm.ThankyouPage)) { redirectionUrl = contactForm.ThankyouPage; } } } catch { // L'eccezione serve solo per la chiamata via APIController, mentre per la chiamata via form è già stata loggata e salvata nel Notifier TempData["form"] = Request.Form; redirectionUrl = returnUrl; } return(this.RedirectLocal(redirectionUrl, "~/")); }
/// <summary> /// Sends the contact email. /// </summary> /// <param name="id">The id.</param> /// <param name="returnUrl">The return URL.</param> /// <param name="name">The name.</param> /// <param name="email">The bot false email.</param> /// <param name="confirmEmail">The actual email string</param> /// <param name="subject">The subject.</param> /// <param name="message">The message.</param> public ActionResult SendContactEmail(int id, string returnUrl, string name, string email, string confirmEmail, string subject, string message, int mediaId = -1, int accept = 0) { var redirectionUrl = returnUrl; try { // we want to create a new contentItem of the same type as the form we are posting var stubItem = _contentManager.Get <ContactFormPart>(id); // then we will try to launch UPdateEditor to test recaptcha. if (stubItem != null) { _frontEndEditeService.BuildFrontEndShape( _contentManager.UpdateEditor(stubItem, this), OnlyShowReCaptcha, NoFields); if (!ModelState.IsValid) { // consider logging // update of recaptcha failed _orchardServices.TransactionManager.Cancel(); TempData["form"] = Request.Form; return(this.RedirectLocal(redirectionUrl, "~/")); } } ContactFormRecord contactForm = _contactFormService.GetContactForm(id); if (contactForm.AcceptPolicy && accept != 1) { TempData["form"] = Request.Form; return(this.RedirectLocal(Request.UrlReferrer.ToString())); } if (contactForm != null) { // If a static subject message was specified, use that value for the email subject. if (contactForm.UseStaticSubject) { if (contactForm.StaticSubjectMessage != null) { subject = contactForm.StaticSubjectMessage.Replace("{NAME}", name); } if (Request.Url != null) { subject = subject.Replace("{DOMAIN}", Request.Url.Host); } } _contactFormService.SendContactEmail(name, confirmEmail, email, subject, message, mediaId, contactForm, _orchardServices.WorkContext.HttpContext.Request.Form); // after sending email it triggers a worflow event in order to execute arbitrary code. _workflowManager.TriggerEvent("ContactFormSubmittedEvent", stubItem, () => new Dictionary <string, object> { { "Content", stubItem } }); if (!string.IsNullOrWhiteSpace(contactForm.ThankyouPage)) { redirectionUrl = contactForm.ThankyouPage; } } } catch { // L'eccezione serve solo per la chiamata via APIController, mentre per la chiamata via form è già stata loggata e salvata nel Notifier TempData["form"] = Request.Form; redirectionUrl = returnUrl; } return(this.RedirectLocal(redirectionUrl, "~/")); }