你是否也遇到过网站留言几个月才看到,针对于这一问题,我们把原有的DTcms留言插件升级一下,加入邮件通知功能,当有人留言时,系统自动发送封邮件到您常用的邮箱或者发送手机短信。
留言配置管理界面
现在我们开始改造工程吧:
1、首先第一步,添加config目录
2、在config目录下添加一个名为“install.config”的配置文件,添加如下代码
<?xml version="1.0"?> <install xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <bookmsg>2</bookmsg> <booktemplet>wordsmsg</booktemplet> <receive>271877887@qq.com</receive> </install>
3、在model.cs文件中添加配置文件模型。
[Serializable] public partial class install { public install() { } private int _bookmsg = 0; private string _booktemplet = ""; private string _receive = ""; /// <summary> /// 通知 /// </summary> public int bookmsg { set { _bookmsg = value; } get { return _bookmsg; } } /// <summary> /// 通知模板别名 /// </summary> public string booktemplet { set { _booktemplet = value; } get { return _booktemplet; } } /// <summary> /// 接收邮箱或手机 /// </summary> public string receive { set { _receive = value; } get { return _receive; } } }
4、在dal.cs文件中添加配置文件操作方法。
/// <summary> /// 数据访问类:配置文件 /// </summary> public partial class install { private static object lockHelper = new object(); public install() { } #region 扩展设置参数 /// <summary> /// 读取站点配置文件 /// </summary> public Model.install loadConfig(string configFilePath) { return (Model.install)SerializationHelper.Load(typeof(Model.install), configFilePath); } /// <summary> /// 写入站点配置文件 /// </summary> public Model.install saveConifg(Model.install model, string configFilePath) { lock (lockHelper) { SerializationHelper.Save(model, configFilePath); } return model; } #endregion }
4、在bll.cs文件中添加以下方法。
/// <summary> /// 配置文件 /// </summary> public partial class install { private readonly DAL.install dal; public install() { dal = new DAL.install(); } /// <summary> /// 读取配置文件 /// </summary> public Model.install loadConfig(string config_path) { string cacheName = "gs_cache_feedback_config"; Model.install model = CacheHelper.Get<Model.install>(cacheName); if (model == null) { CacheHelper.Insert(cacheName, dal.loadConfig(Utils.GetMapPath(config_path)), Utils.GetMapPath(config_path)); model = CacheHelper.Get<Model.install>(cacheName); } return model; } /// <summary> /// 保存配置文件 /// </summary> public Model.install saveConifg(Model.install model, string config_path) { return dal.saveConifg(model, Utils.GetMapPath(config_path)); } }
5、在admin目录下添加install.aspx文件。用作管理操作界面;
6、修改“ajax.ashx”文件,在“feedback_add”方法中添加发送方法;在修改方法之前要预先读取配置文件
Model.install config = new BLL.install().loadConfig("../config/install.config");
7、修改“ajax.ashx”文件第89行,添加如下方法
//是否开启通知功能 if (config.bookmsg > 0 && config.receive !="") { switch (config.bookmsg) { case 1: DTcms.Model.sms_template smsModel = new DTcms.BLL.sms_template().GetModel(config.booktemplet); //取得短信内容 if (smsModel != null) { //替换模板内容 string smstxt = smsModel.content; smstxt = smstxt.Replace("{webname}", siteConfig.webname); smstxt = smstxt.Replace("{webtel}", siteConfig.webtel); smstxt = smstxt.Replace("{weburl}", siteConfig.weburl); smstxt = smstxt.Replace("{username}", model.user_name); smstxt = smstxt.Replace("{usertel}", model.user_tel); smstxt = smstxt.Replace("{userqq}", model.user_qq); smstxt = smstxt.Replace("{useremail}", model.user_email); smstxt = smstxt.Replace("{usertitle}", model.title); smstxt = smstxt.Replace("{usercontent}", model.content); //发送短信 string tipMsg = string.Empty; bool result = new DTcms.BLL.sms_message().Send(config.receive, smstxt, 1, out tipMsg); if (!result) { //LogHelper.WriteLog("手机信息发送失败!"); } } break; case 2: //获得邮件内容 DTcms.Model.mail_template mailModel = new DTcms.BLL.mail_template().GetModel(config.booktemplet); if (mailModel != null) { //替换模板内容 string titletxt = mailModel.maill_title; string bodytxt = mailModel.content; titletxt = titletxt.Replace("{webname}", siteConfig.webname); titletxt = titletxt.Replace("{username}", model.user_name); bodytxt = bodytxt.Replace("{webname}", siteConfig.webname); bodytxt = bodytxt.Replace("{webtel}", siteConfig.webtel); bodytxt = bodytxt.Replace("{weburl}", siteConfig.weburl); bodytxt = bodytxt.Replace("{username}", model.user_name); bodytxt = bodytxt.Replace("{usertel}", model.user_tel); bodytxt = bodytxt.Replace("{userqq}", model.user_qq); bodytxt = bodytxt.Replace("{useremail}", model.user_email); bodytxt = bodytxt.Replace("{usertitle}", model.title); bodytxt = bodytxt.Replace("{usercontent}", model.content); //循环发送 string[] emailArr = config.receive.Split(','); foreach (string email in emailArr) { if (DTcms.Common.Utils.IsValidEmail(email)) { //发送邮件 try { DTMail.sendMail(siteConfig.emailsmtp, siteConfig.emailssl, siteConfig.emailusername, DESEncrypt.Decrypt(siteConfig.emailpassword, siteConfig.sysencryptstring), siteConfig.emailnickname, siteConfig.emailfrom, email, titletxt, bodytxt); } catch { //LogHelper.WriteLog("邮件发送失败!"); } } } } break; } }
到此我们的网站留言功能就升级完成了。注意,邮件通知、短信通知都必须先配置系统参数才可以正常使用。