Copied to clipboard

Flag this post as spam?

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


  • Sam 75 posts 391 karma points
    Nov 16, 2016 @ 18:03
    Sam
    0

    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();
        }
    }
    }
    
  • Dennis Adolfi 1071 posts 6352 karma points MVP 2x c-trib
    Nov 16, 2016 @ 19:09
    Dennis Adolfi
    100

    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!

  • Sam 75 posts 391 karma points
    Nov 16, 2016 @ 20:11
    Sam
    1

    Thank you Dennis, That did the trick, but the reference needed was:

    using Umbraco.Web;

  • Dennis Adolfi 1071 posts 6352 karma points MVP 2x c-trib
    Nov 17, 2016 @ 07:14
    Dennis Adolfi
    0

    Awesome Sam, glad it worked out for you!

    Thanks for the feedback, I always mix those two up. :)

  • Janus Kamp Hansen 11 posts 147 karma points
    Nov 16, 2016 @ 19:14
    Janus Kamp Hansen
    0

    Hi Sam,

    I have done it like this:

    if (HttpContext.Current.Request.Url.DnsSafeHost.ToString().EndsWith("vorestest.dk") || HttpContext.Current.Request.Url.DnsSafeHost == "localhost") {
    }
    

    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 ;)

  • Nik 1267 posts 5300 karma points MVP 3x c-trib
    Nov 16, 2016 @ 19:40
    Nik
    1

    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:

    var siteHomeNode = Umbraco.AssignedContentItem.AnsestorOrSelf("Home");
    if(siteHomeNode == null || string.IsNullOrWhiteSpace(siteHomeNode.GetPropertyValue<string>(contactEmail))){
         //get email from config
    }else{
        email = siteHomeNode.GetPropertyValue<string>(contactEmail);
    }
    

    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 :-)

Please Sign in or register to post replies

Write your reply to:

Draft