Posted on 15/09/2016 11:24:54
Hi Jens
I don't think it was a big job. I created a website settings itemtype to hold settings like FromEmail, ToEmail, Subject and FromEmailName used when sending the email notification. The code is here:
namespace Notifications
{
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using Dynamicweb;
using Dynamicweb.Content.Commenting;
using Dynamicweb.Content.Items;
using Dynamicweb.Notifications;
using Dynamicweb.Rendering;
[Dynamicweb.Extensibility.Subscribe(Commenting.OnAfterComment)]
public class CommentingOnAfterComment : Dynamicweb.Extensibility.NotificationSubscriber
{
public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
{
if (HttpContext.Current.Request.UrlReferrer == null)
{
return;
}
var ws = ItemManager.Storage.Open("WebsiteSettings").SelectAll().FirstOrDefault();
if (ws == null)
{
return;
}
var commentArgs = args as Commenting.CommentArgs;
var fromEmail = ws.ContainsKey("FromEmail") ? ws["FromEmail"].ToString() : "";
var toEmail = ws.ContainsKey("ToEmail") ? ws["ToEmail"].ToString() : "";
var subject = ws.ContainsKey("Subject") ? ws["Subject"].ToString() : "";
var fromEmailName = ws.ContainsKey("FromEmailName") ? ws["FromEmailName"].ToString() : "";
var template = new Template(Input.Request("Comment.NotifyTemplate"));
if (commentArgs != null)
{
Renderer.RenderComment(template, commentArgs.Comment);
}
template.SetTag("Comment.Referrer", HttpContext.Current.Request.UrlReferrer.AbsoluteUri);
EmailHandler.Send(new MailMessage
{
Subject = subject,
From = new MailAddress(fromEmail, fromEmailName),
Sender = new MailAddress(fromEmail, fromEmailName),
To =
{
toEmail
},
IsBodyHtml = true,
Body = template.Output(),
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8,
HeadersEncoding = Encoding.UTF8
});
}
}
}