Skip to content

Instantly share code, notes, and snippets.

@tiagoduarte
Last active September 24, 2016 13:05
Show Gist options
  • Select an option

  • Save tiagoduarte/67ffcc849f020afb7c869de8fdd0b166 to your computer and use it in GitHub Desktop.

Select an option

Save tiagoduarte/67ffcc849f020afb7c869de8fdd0b166 to your computer and use it in GitHub Desktop.
umbraco contact form part 2
using System.Configuration;
using System.Net.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using MailChimp;
using MailChimp.Lists;
namespace ContactForm.Controllers
{
public class ContactController : SurfaceController
{
[ChildActionOnly]
public ActionResult ContactForm()
{
// In case you need it...
var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());
var model = new Models.ContactModel();
//return View("umbContactUs", model);
return CurrentUmbracoPage();
}
/// <summary>
/// this method sends an email using web.config settings
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
private void SendEmail(string subject, string body)
{
//web.config
//<network host="smtp.gmail.com" enableSsl="true" port="587" userName="you@gmail.com" password="" />
//may need to activate less secure apps: https://www.google.com/settings/security/lesssecureapps
//get data from web.config
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string userName = smtpSection.Network.UserName;
string password = smtpSection.Network.Password;
string host = smtpSection.Network.Host;
bool ssl = smtpSection.Network.EnableSsl;
int port = smtpSection.Network.Port;
MailMessage message = new MailMessage();
//enforce to and from
message.From = new MailAddress(userName);
message.To.Add(new MailAddress(userName));
//message.Sender = new MailAddress(userName);
message.Body = body;
message.Subject = subject;
message.IsBodyHtml = true;
//send email
SmtpClient smtp = new SmtpClient(host, port);
smtp.Credentials = new System.Net.NetworkCredential(userName, password);
//smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.EnableSsl = ssl;
smtp.Send(message);
}
[HttpPost]
public ActionResult ContactForm(Models.ContactModel model)
{
//Check if the dat posted is valid (All required's & email set in email field)
if (!ModelState.IsValid)
{
//Not valid - so lets return the user back to the view with the data they entered still prepopulated
return CurrentUmbracoPage();
}
try
{
//compose email
var sb = new StringBuilder();
sb.AppendFormat("<p>Fist Name: {0}</p>", model.FistName);
sb.AppendFormat("<p>Last Name: {0}</p>", model.LastName);
sb.AppendFormat("<p>Company: {0}</p>", model.Company);
sb.AppendFormat("<p>Position: {0}</p>", model.Position);
sb.AppendFormat("<p>Telephone: {0}</p>", model.Telephone);
sb.AppendFormat("<p>Email: {0}</p>", model.Email);
sb.AppendFormat("<p>Enquire: {0}</p>", model.Enquire);
//send email
SendEmail("Contact Form (" + umbraco.library.NiceUrlWithDomain(CurrentPage.Id) + ")", sb.ToString());
}
catch (SmtpException smtpEx)
{
ModelState.AddModelError("Error", smtpEx.Message);
return CurrentUmbracoPage();
}
catch (System.Exception ex)
{
ModelState.AddModelError("Error", ex.Message);
return CurrentUmbracoPage();
}
ViewBag.Message = model.SuccessMessage;
return CurrentUmbracoPage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment