public static void SendAlertEmails(List <SavedSearch> searches, List <WhatChanged> whatChanged = null) { Classes.Media352_MembershipProvider.User userEntity = Classes.Media352_MembershipProvider.User.GetByID(searches.FirstOrDefault().UserID, (new[] { "UserInfo" }).ToList()); string toAddress = userEntity.Email; string propertyLinks = string.Empty, searchLinks = string.Empty; string basePropertyHtml = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearchProperty, null, false); string baseFilterHtml = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearchFilter, null, false); string baseFilterPropertyHtml = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearchFilterProperty, null, false); foreach (SavedSearch search in searches) { string msLink = search.ShowcaseID == (int)MeybohmShowcases.AikenExistingHomes || search.ShowcaseID == (int)MeybohmShowcases.AikenRentalHomes ? "aiken/" : "augusta/"; string searchType = search.ShowcaseID == (int)MeybohmShowcases.AikenRentalHomes || search.ShowcaseID == (int)MeybohmShowcases.AugustaRentalHomes ? "rentals" : "search"; if (!string.IsNullOrEmpty(search.FilterString)) { string propertiesText = string.Empty; WhatChanged whatChangedEntity = whatChanged.Find(f => f.SavedSearch.SavedSearchID == search.SavedSearchID); if (whatChangedEntity != null) { foreach (ShowcaseItemForJSON property in whatChangedEntity.PropertiesThatChanged) { string whatChangedText = string.Empty; List <PropertyChangeLog> propertyChanges = whatChangedEntity.ChangeLog.Where(s => s.ShowcaseItemID == property.ShowcaseItemID && !m_ChangeLogAttributesToIgnore.Any(a => a == s.Attribute)).ToList(); if (propertyChanges.Any()) { if (propertyChanges.Any(c => c.Attribute == "Home Added")) { whatChangedText += "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" /> Home Added"; } else { foreach (PropertyChangeLog change in propertyChanges) { whatChangedText += "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" /> " + change.Attribute + " Updated,"; } } } else { whatChangedText = "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" /> Home Added"; } propertiesText += baseFilterPropertyHtml.Replace("[[Address]]", property.Address + "<br />" + property.City + ", " + property.State + " " + property.Zipcode) .Replace("[[Title]]", property.Title.Replace(" Bedrooms:", "<br />Bedrooms:")) .Replace("[[Image]]", !String.IsNullOrWhiteSpace(property.Image) && !property.Image.ToLower().StartsWith("http") ? Helpers.RootPath + Globals.Settings.UploadFolder + "images/" + property.Image : property.Image) .Replace("[[WhatChanged]]", whatChangedText.TrimEnd(',').Replace(",", "<br />")); } } searchLinks += baseFilterHtml.Replace("[[Url]]", Helpers.RootPath + msLink + searchType + "?" + search.FilterString) .Replace("[[Name]]", search.Name) .Replace("[[Properties]]", propertiesText); } if (search.ShowcaseItemID > 0) { ShowcaseItem property = ShowcaseItem.GetByID((int)search.ShowcaseItemID, new string[] { "Address", "Address.State" }); if (!property.Active) { continue; } propertyLinks = string.IsNullOrEmpty(propertyLinks) ? string.Format(@"<tr style=""background-color: #f5f5f5; padding: 10px 0; width: 100%;""> <td style=""text-align: center; padding: 10px 0; font-family: Arial, sans-serif; font-size: 16px; color: #999; line-height: 1.4; vertical-align: top;"" colspan=""2""> Updated Properties: {0} </td> </tr> <tr> <td colspan=""2""> </td> </tr>", string.Join(", ", searches.Where(s => s.ShowcaseItemID.HasValue).Select(s => s.Name))) : propertyLinks; propertyLinks += basePropertyHtml.Replace("[[Title]]", property.Title.Replace(" Bedrooms:", "<br />Bedrooms:")) .Replace("[[Image]]", !String.IsNullOrWhiteSpace(property.Image) && !property.Image.ToLower().StartsWith("http") ? Helpers.RootPath + Globals.Settings.UploadFolder + "images/" + property.Image : property.Image) .Replace("[[Address]]", property.Address.Address1 + "<br />" + property.Address.City + ", " + property.Address.State.Abb + " " + property.Address.Zip) .Replace("[[DateListed]]", property.DateListed.HasValue ? property.DateListed.Value.ToShortDateString() : string.Empty) .Replace("[[NumberOfPhotos]]", Media.GetNumberOfPhotos(property.ShowcaseItemID).ToString()) .Replace("[[WhatChanged]]", "<img alt=\"Listing Updated\" src=\"[[ROOT]]img/exclamation.png\" /> " + PropertyChangeLog.PropertyChangeLogPage(0, 1, "", "DateStamp", false, new PropertyChangeLog.Filters { FilterPropertyChangeLogShowcaseItemID = search.ShowcaseItemID.ToString() }).FirstOrDefault().Attribute.Replace("PhotoURL", "Photos") + " Updated") .Replace("[[PropertyLink]]", Helpers.RootPath + msLink + searchType + "?id=" + property.ShowcaseItemID); } } UserInfo userInfo = userEntity.UserInfo.FirstOrDefault(); if (string.IsNullOrWhiteSpace(toAddress) || userInfo == null || (string.IsNullOrEmpty(propertyLinks) && string.IsNullOrEmpty(searchLinks))) { return; } MailMessage email = new MailMessage { IsBodyHtml = true, From = new MailAddress(Globals.Settings.FromEmail) }; email.To.Add(toAddress); email.Body = EmailTemplateService.HtmlMessageBody(EmailTemplates.SavedSearch, new { FirstAndLastName = userInfo.FirstAndLast, PropertyLinks = propertyLinks, SearchLinks = searchLinks }); email.Subject = Globals.Settings.SiteTitle + " - Saved Search"; SmtpClient smtp = new SmtpClient(); smtp.Send(email); }
public static void SendAlertEmails(SavedSearch savedSearch, WhatChanged whatChanged = null) { SendAlertEmails(new List <SavedSearch>(new[] { savedSearch }), new List <WhatChanged>(new[] { whatChanged })); }