How can I switch the recipient email address in my Contact Surface Controller depending on site data?
Hello,
I have 2 sites running on 1 Umbraco installation, and both sites will be using the same contact form. I need the To address to change depending on the current page's site name or Id.
I cannot seem to define the right references to construct a proper condition statement.
Below is my controller. Thanks for your help
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using Umbraco.Web;
namespace Avani.App_Code
{
public class ContactSurfaceController : SurfaceController
{
// GET: ContactSurface
public ActionResult Index()
{
return PartialView("ContactForm", new ContactModel());
}
[HttpPost]
public ActionResult FormSubmit(ContactModel model)
{
if (ModelState.IsValid)
{
//Send Mail
var sb = new StringBuilder();
sb.AppendFormat("<p>Name: {0}</p>", model.Name);
sb.AppendFormat("<p>Email: {0}</p>", model.Email);
sb.AppendFormat("<p>Phone: {0}</p>", model.Phone);
sb.AppendFormat("<p>Company: {0}</p>", model.Company);
sb.AppendFormat("<p>Message: {0}</p>", model.Message);
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
if (...) {
message.To.Add(new MailAddress(ConfigurationManager.AppSettings["ContactAddress"]));
}else
{
message.To.Add(new MailAddress(ConfigurationManager.AppSettings["ContactAddress2"]));
}
message.Subject = "Message from Website";
message.From = new MailAddress(model.Email);
message.Body = sb.ToString();
message.IsBodyHtml = true;
try
{
smtp.Send(message);
}
catch (SmtpException)
{
TempData["Failed"] = true;
return RedirectToCurrentUmbracoPage();
}
TempData["Success"] = true;
return RedirectToCurrentUmbracoPage();
}
return CurrentUmbracoPage();
}
}
}
How can I switch the recipient email address in my Contact Surface Controller depending on site data?
Hello,
I have 2 sites running on 1 Umbraco installation, and both sites will be using the same contact form. I need the To address to change depending on the current page's site name or Id.
I cannot seem to define the right references to construct a proper condition statement.
Below is my controller. Thanks for your help
Hi Sam. Try the following statement:
if(CurrentPage.AncestorOrSelf(1).Id == site id goes here){ }
Make sure to also add the following in the top of your controller:
using Umbraco.Core;
Best of luck!
Thank you Dennis, That did the trick, but the reference needed was:
using Umbraco.Web;
Awesome Sam, glad it worked out for you!
Thanks for the feedback, I always mix those two up. :)
Hi Sam,
I have done it like this:
I use this around all e-mails being sent, so that our test-sites and localhosts doesn't send out e-mail to customers.
I hope that is what you're looking for ;)
Hi Sam,
As you can see there are alot of options available to you.
One that I quite like to do actually stores the email address on a property of the home node.
Making the assumption that for both your sites within your single instance start with a node who's document type is "Home"
I would use some code that is similar to this:
This means that you can set the site specific email without causing an app pool recycle, and you don't have to keep touching web config.
Hope the ideas here help :-)
is working on a reply...