#region using using System; using System.Web; using BlogEngine.Core.Web.Controls; using BlogEngine.Core; using System.Net.Mail; using System.Threading; #endregion /// /// Sends an e-mail to the blog owner whenever a comment is added. /// [Extension("Sends an e-mail to the blog owner whenever a comment is added", "1.3", "BlogEngine.NET")] public class SendCommentMail { /// /// Hooks up an event handler to the Post.CommentAdded event. /// static SendCommentMail() { Post.CommentAdded += new EventHandler(Post_CommentAdded); } private static void Post_CommentAdded(object sender, EventArgs e) { Post post = (Post)((Comment)sender).Parent; if (post != null && BlogSettings.Instance.SendMailOnComment && !Thread.CurrentPrincipal.Identity.IsAuthenticated) { Comment comment = post.Comments[post.Comments.Count - 1]; // Trackback and pingback comments don't have a '@' symbol in the e-mail field. string replyTo = comment.Email.Contains("@") ? comment.Email : BlogSettings.Instance.Email; string subject = " comment on "; if (comment.Email == "trackback") subject = " trackback on "; else if (comment.Email == "pingback") subject = " pingback on "; System.Globalization.CultureInfo defaultCulture = Utils.GetDefaultCulture(); ServingEventArgs args = new ServingEventArgs(comment.Content, ServingLocation.Email); Comment.OnServing(comment, args); string body = args.Body; MailMessage mail = new MailMessage(); mail.From = new MailAddress(BlogSettings.Instance.Email); mail.To.Add(BlogSettings.Instance.Email); mail.ReplyTo = new MailAddress(replyTo, HttpUtility.HtmlDecode(comment.Author)); mail.Subject = BlogSettings.Instance.EmailSubjectPrefix + subject + post.Title; mail.Body = "
"; mail.Body += body.Replace(Environment.NewLine, "
") + "

"; mail.Body += string.Format("{0}: {2}

", Utils.Translate("post", null, defaultCulture), post.PermaLink + "#id_" + comment.Id, post.Title); string deleteLink = post.AbsoluteLink + "?deletecomment=" + comment.Id; mail.Body += string.Format("{1}", deleteLink, Utils.Translate("delete", null, defaultCulture)); if (BlogSettings.Instance.EnableCommentsModeration) { string approveLink = post.AbsoluteLink + "?approvecomment=" + comment.Id; mail.Body += string.Format(" | {1}", approveLink, Utils.Translate("approve", null, defaultCulture)); } mail.Body += "
_______________________________________________________________________________
"; mail.Body += "

Author information

"; mail.Body += "
"; mail.Body += "Name: " + comment.Author + "
"; mail.Body += "E-mail: " + comment.Email + "
"; mail.Body += string.Format("Website: {0}
", comment.Website); if (comment.Country != null) mail.Body += "Country code: " + comment.Country.ToUpperInvariant() + "
"; if (HttpContext.Current != null) { mail.Body += "IP address: " + HttpContext.Current.Request.UserHostAddress + "
"; mail.Body += "User-agent: " + HttpContext.Current.Request.UserAgent; } mail.Body += "
"; mail.Body += "
"; Utils.SendMailMessageAsync(mail); } } }