前几天发过一个文章很简单很简单的介绍了一下CommentToMail这个评论邮件通知插件(博主懒),今天决定填一下坑。
下载插件
先下载这款插件,地址由作者提供:下载链接
下载后解压上传至typecho的 /usr/plugins/
目录下,重命名为CommentToMail
修改主题模板
博主所使用handsome主题最新版已经直接集成了对插件的支持,因此不用修改,使用同款主题的朋友们,可以直接跳过这一步!
打开主题的comments.php
文件(主题文件夹在/usr/themes/主题/
下),在评论form表单的适当位置添加name为receiveMail的选择框(checkbox),请注意:下方两种代码,你只能选择一个添加到主题模板文件,一般建议你选择默认接收邮件。
正常显示选择框:
`<input type="checkbox" name="receiveMail" id="receiveMail" value="yes" checked /> <label for="receiveMail" style="padding-left:8px;">当有人回复时接收邮件提醒</label>`
隐藏选择框(默认接受邮件):
`<input type="hidden" name="receiveMail" id="receiveMail" value="yes" />`
我们以typecho的默认主题为例:
因为邮件通知,我们肯定需要知道邮箱,所以我们索引把代码扔到邮箱输入框那边。
后台启用并设置插件
后台启用插件后,在后台头部控制台
下拉菜单中会出现邮件评论提醒
,进入后我们先去插件设置
以博主的设置为例:
博主使用的是腾讯企业邮箱,配置中的smtp地址和smtp端口请在你的邮件服务商那边获取,设置中的key请不要随意泄露。
到这里,插件基本上已经设置完毕,你可以点击控制台
-->邮件评论提醒
-->邮件发送测试
,来发送一封测试邮件,如果成功收到,则设置没有问题。但因为这一款插件使用网址监控运触发发件,因此要实现自动发件还需要设置监控。
设置监控
我们可以按照作者的方法,使用第三方的网址监控商来实现,如阿里/360网址监控。这里我不在赘述,如有需要请移步作者教程
今天我使用的是服务器定时任务的方式来实现的,简单快捷。
- 如果你使用的是宝塔面板,在计划任务中就可以直接添加一个
访问url
的定时任务,其中执行周期会影响发件延迟,我直接暴力设置为1分钟,url可以在刚刚的插件设置那边看到,以我的为例:https://www.wefinger.club/action/comment-to-mail?do=deliverMail&key=你设置的key
- crontab定时脚本方式,首先确认你的服务器支持curl和crontab,不支持请先安装
- centos
`yum install crontabs`
`yum install curl`
- debian/ubuntu
`apt-get install cron`
`apt-get install curl`
- 接着使用
crontab -e
打开crontab文件编写,第一次进入编写crontab文件的界面,系统会提示选择相应的编辑器,一般我们选择vi编辑器就可以了,如果非第一次进入,但是自己的编辑器又不是vi编辑器,直接再shell命令行输入: select-editor 就可以进行重新选择 进入编辑器后,按下 “ i ” 键,进入编辑模式,在编辑模式下,我们写上我们这次需要访问url的脚本:
`*/1 * * * * curl "你的url"`
这里是每分钟执行一次,具体请百度相关的语法;后面的url链接更换为你的url。url一定要用""括起来。。不然emmm
输入完成后,点击“ Esc ”键,再点击“ : ”键,再输入 wq ,即可保存退出
- 保存完后需要重启crontab服务:
sudo service cron restart
- 查看建立成功的监控任务:
crontab -l
到这里,插件基本上就可用了。尽情的玩耍去吧~
1.Key是邮件任务执行密码,防止他人恶意执行任务消耗资源
2.下方任务执行地址就是说当你访问这个网址时,邮件任务才会执行,为了达到自动发送的效果,我们设置cron或者网址监控,每隔一段时间让远程服务器代替你访问任务执行网址
3.执行验证是用来调试和应对特殊环境,一般不要勾选!
4.可以清理邮件发送信息
5.QQ邮箱smtp密码需要在邮箱网页端获取,具体配置信息可以参考度娘
6.测试普通QQ邮箱可以正常发送,但可能由于腾讯反垃圾邮件逻辑,用户不能正常接受邮件,建议大家使用QQ域名邮箱,如果你没有域名邮箱,可以通过邮件联系我,或者直接在下方留言
7.本插件仅支持typecho1.0及之后版本
8.如果出现 邮件发送失败:SMTP connect() failed. (PHP>=5.6)可以参考这篇博文 https://9sb.org/45
转载一波DengSir的魔改邮件模板
首先,插件目录下有owner.html还有guest.html这两个文件,其中owner是自己看的,guest是通知评论者的
效果如图:
我们先来改owenr.html
代码:
<div style="border-top: 2px solid #12ADDB;border-radius: 5px;box-shadow: 0 5px 100px #AAA;padding-bottom: 15px;width: 300px;margin: 50px auto;font-family: 'Century Gothic', 'Trebuchet MS', 'Hiragino Sans GB', 微软雅黑, 'Microsoft Yahei', Tahoma, Helvetica, Arial, 'SimSun', sans-serif;text-align: center;">
<div style="position: relative;margin-bottom: 20px;padding: 5px 5px 0;">
<div style="box-shadow: 0 5px 10px #5a5151;border-radius: 5px;overflow: hidden;"><img src="https://www.96sir.com/images/mail_me.jpg" style="width: 100%;"></div>
<div style="width: 70px;height: 30px;line-height: 30px;background-color: #49badc;color: #fff;position: absolute;bottom: -15px;left: 40px;box-shadow: 5px 5px 10px #5a5151;border-radius: 5px;">Mail</div>
</div>
<h2 style="border-bottom: 1px solid #12ADDB;font-size: 14px;font-weight: 700;color: #8078e6;padding: 5px 10px;">您发表的文章<a style="text-decoration: none;color: #12ADDB;" href="{permalink}" target="_blank"> {title} </a>有了新的评论</h2>
<div style="padding: 0 20px;margin-top: 20px;text-align: left;color: #555555;font-size: 12px;">
<p style="font-size: 14px;font-weight: 700;">时间:<span style="border-bottom: 1px dashed #ccc;" t="5" times=" 20:42"> {time} </span></p>
<p style="background-color: #f1f1f1;border: 1px dashed #DDD;padding: 10px 15px;margin: 18px auto">{text}</p>
<p style="font-size: 14px;font-weight: 700;">评论者"{author}"邮箱为:</p>
<p style="background-color: #f1f1f1;border: 1px dashed #DDD;padding: 10px 15px;margin: 18px auto"><a style="text-decoration: none;color: #12ADDB;" href="mailto:{mail}" target="_blank">{mail}</a></p>
<p style="font-size: 14px;font-weight: 700;">评论者"{author}"其他信息:</p>
<p style="background-color: #f1f1f1;border: 1px dashed #DDD;padding: 10px 15px;margin: 18px auto">IP:{ip},状态:{status},[<a style="text-decoration: none;color: #12ADDB;" href='{manage}' target='_blank'>管理评论</a>]</p>
<p>您可以点击 <a style="text-decoration: none;color: #12ADDB;" href="{permalink}" target="_blank">查看回复的完整內容 </a>欢迎再次光临 <a style="text-decoration: none; color: #12addb" href="https://www.96sir.com" target="_blank">{siteTitle}</a></p>
</div>
</div>
然后来改guest.html
代码:
<div style="border-top: 2px solid #12ADDB;border-radius: 5px;box-shadow: 0 5px 100px #AAA;padding-bottom: 15px;width: 300px;margin: 50px auto;font-family: 'Century Gothic', 'Trebuchet MS', 'Hiragino Sans GB', 微软雅黑, 'Microsoft Yahei', Tahoma, Helvetica, Arial, 'SimSun', sans-serif;text-align: center;">
<div style="position: relative;margin-bottom: 20px;padding: 5px 5px 0;">
<div style="box-shadow: 0 5px 10px #5a5151;border-radius: 5px;overflow: hidden;"><img src="https://www.96sir.com/images/mail_me.jpg" style="width: 100%;"></div>
<div style="width: 70px;height: 30px;line-height: 30px;background-color: #49badc;color: #fff;position: absolute;bottom: -15px;left: 40px;box-shadow: 5px 5px 10px #5a5151;border-radius: 5px;">Mail</div>
</div>
<h2 style="border-bottom: 1px solid #12ADDB;font-size: 14px;font-weight: 700;color: #8078e6;padding: 5px 10px;">您({author_p})在<a style="text-decoration: none;color: #12ADDB;" href="{permalink}" target="_blank"> {title} </a>的评论有了新的回复</h2>
<div style="padding: 0 20px;margin-top: 20px;text-align: left;color: #555555;font-size: 12px;">
<p style="font-size: 14px;font-weight: 700;">时间:<span style="border-bottom: 1px dashed #ccc;" t="5" times=" 20:42"> {time} </span></p>
<p style="font-size: 14px;font-weight: 700;">你的评论:</p>
<p style="background-color: #f1f1f1;border: 1px dashed #DDD;padding: 10px 15px;margin: 18px auto">{text_p}</p>
<p style="font-size: 14px;font-weight: 700;"><strong>{author}</strong> 回复说:</p>
<p style="background-color: #f1f1f1;border: 1px dashed #DDD;padding: 10px 15px;margin: 18px auto">{text}</p>
<p>您可以点击 <a style="text-decoration: none;color: #12ADDB;" href="{permalink}" target="_blank">查看评论的完整內容 </a>本邮件为自动发送,如有疑问,联系我 <a style="text-decoration: none; color: #12addb" href="mailto:me@96sir.com" target="_blank">me@96sir.com </a>欢迎再次光临 <a style="text-decoration: none; color: #12addb" href="https://www.96sir.com" target="_blank">{siteTitle}</a></p>
</div>
</div>
https://www.96sir.com/resources/44.html
<p align="center">本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。</p>
感谢博主,我搞了一晚上没搞定,看了你的博文才知道是curl 后面的网址没加“”,把我给坑死了
博主可知道邮件发送测试的时候不行是什么原因,一直转圈圈,几分钟后报502错误
我也在用这插件,这是个好插件
是挺好的,用cron监控很稳定。就是自带邮件模板不好看,准备换掉。。
emmmm