func (p *EmailPlugin) Form() string {
	return `<h4 class="title">
        SMTP 邮箱提醒设置
    </h4>
    <p class="item">
        <label for="smtp-addr">SMTP服务器</label>
        <input class="ipt" id="smtp-addr" type="text" name="smtp_host" placeholder="SMTP服务器地址，如 smtp.gmail.com:465" value="` + p.settings["smtp_host"] + `"/>
    </p>
    <p class="item">
        <label for="smtp-email">SMTP邮箱</label>
        <input class="ipt" id="smtp-email" type="email" name="smtp_email_user" placeholder="使用SMTP的邮箱" value="` + p.settings["smtp_email_user"] + `"/>
    </p>
    <p class="item">
        <label for="smtp-password">邮箱密码</label>
        <input class="ipt" id="smtp-password" type="text" name="smtp_email_password" placeholder="邮箱密码" value="` + p.settings["smtp_email_password"] + `"/>
    </p>
    <p class="submit item">
        <label>&nbsp;</label>
        <button class="btn btn-blue">保存设置</button>
        <span class="tip">暂不支持验证，请自行保证正确</span>
    </p>`
}
Form returns the form template string for email plugin.
package plugin
import (
	"bytes"
	"encoding/base64"
	"fmt"
	"github.com/fuxiaohei/GoBlog/app/model"
	"github.com/fuxiaohei/GoInk"
	"html/template"
	"net/mail"
	"net/smtp"
	"path"
	"strings"
)
var (
	emailReplyTemplate = `<table style="width: 99.8%;height:99.8% "><tbody><tr><td style="background:#FAFAFA">
    <div style="background-color:white;border-top:2px solid #0079BC;box-shadow:0 1px 3px #AAAAAA;line-height:180%;padding:0 15px 12px;width:500px;margin:50px auto;color:#555555;font-family:'Century Gothic','Trebuchet MS','Hiragino Sans GB',微软雅黑,'Microsoft Yahei',Tahoma,Helvetica,Arial,'SimSun',sans-serif;font-size:12px;">
        <h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;"><span style="color: #0079bc;font-weight: bold;">&gt; </span>您在<a style="text-decoration:none;color: #e64346;" href="{{.link}}"> {{.site}} </a>上的留言有回复啦！</h2>
        <div style="padding:0 12px 0 12px;margin-top:18px">
            <p><strong>{{.author_p}}</strong> 同学，您曾在文章《{{.title}}》上发表评论:</p>
            <p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0">{{.text_p}}</p>
            <p><strong>{{.author}}</strong> 给您的回复如下:</p>
            <p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0">{{.text}}</p>
            <p>您可以点击 <a style="text-decoration:none; color:#0079BC" href="{{.permalink}}">查看回复的完整內容 </a>，欢迎再次光临 <a style="text-decoration:none; color:#0079bc" href="{{.link}}">{{.site}} </a>。</p>
        </div>
    </div>
</td></tr></tbody></table>`
	emailCreatedTemplate = `<table style="width: 99.8%;height:99.8% "><tbody><tr><td style="background:#FAFAFA">
    <div style="background-color:white;border-top:2px solid #0079BC;box-shadow:0 1px 3px #AAAAAA;line-height:180%;padding:0 15px 12px;width:500px;margin:50px auto;color:#555555;font-family:'Century Gothic','Trebuchet MS','Hiragino Sans GB',微软雅黑,'Microsoft Yahei',Tahoma,Helvetica,Arial,'SimSun',sans-serif;font-size:12px;">
        <h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;"><span style="color: #0079bc;font-weight: bold;">&gt; </span>{{.author}} 在<a style="text-decoration:none;color: #e64346;" href="{{.link}}"> {{.site}} </a>上的文章 《{{.title}}》 发表了评论！</h2>
        <div style="padding:0 12px 0 12px;margin-top:18px">
            <p><strong>{{.author}}</strong> 同学，在文章《{{.title}}》上发表评论:</p>
            <p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0">{{.text}}</p>
            <p>您可以点击 <a style="text-decoration:none; color:#0079BC" href="{{.permalink}}">查看完整內容 </a>，欢迎再次光临 <a style="text-decoration:none; color:#0079bc" href="{{.link}}">{{.site}} </a>。</p>
        </div>
    </div>
</td></tr></tbody></table>`
)
type EmailPlugin struct {
	isActive            bool
	isHandlerRegistered bool
	settings            map[string]string
	templates           map[string]*template.Template
}
func init() {
	EmailPlugin := new(EmailPlugin)
	EmailPlugin.isActive = false
	EmailPlugin.isHandlerRegistered = false
	EmailPlugin.settings = make(map[string]string)
	EmailPlugin.templates = make(map[string]*template.Template)
	t1, e := template.New("reply").Parse(emailReplyTemplate)
	if e != nil {
		panic(e)
	}
	EmailPlugin.templates["reply"] = t1
	t2, e2 := template.New("created").Parse(emailCreatedTemplate)
	if e2 != nil {
		panic(e)
	}
	EmailPlugin.templates["created"] = t2
	register(EmailPlugin)
}
func (p *EmailPlugin) Name() string {
	return "邮件提醒"
}
func (p *EmailPlugin) Key() string {
	return "email_notify_plugin"
}
func (p *EmailPlugin) Desc() string {
	return "评论及回复等邮件提醒"
}
func (p *EmailPlugin) ToStorage() map[string]interface{} {
	m := make(map[string]interface{})
	m["name"] = p.Name()
	m["description"] = p.Desc()
	m["is_activate"] = p.isActive
	return m
}
func (p *EmailPlugin) Activate() {
	if p.isHandlerRegistered {
		p.isActive = true
		return
	}
	if model.Storage.Has("plugin/" + p.Key()) {
		model.Storage.Get("plugin/"+p.Key(), &p.settings)
	}
	fn := func(context *GoInk.Context) {
		context.On("comment_created", func(co interface{}) {
			if !p.isActive {
				return
			}
			go func() {
				if len(p.settings["smtp_host"]) < 1 || len(p.settings["smtp_email_user"]) < 1 || len(p.settings["smtp_email_password"]) < 1 {
					println("no setting in smtp email plugin")
					return
				}
				p.sendEmail(co.(*model.Comment), true)
			}()
		})
		context.On("comment_reply", func(co interface{}) {
			if !p.isActive {
				return
			}
			go func() {
				if len(p.settings["smtp_host"]) < 1 || len(p.settings["smtp_email_user"]) < 1 || len(p.settings["smtp_email_password"]) < 1 {
					fmt.Println("no setting in smtp email plugin")
					return
				}
				p.sendEmail(co.(*model.Comment), false)
			}()
		})
	}
	Handler("comment_email_notify", fn, false)
	p.SetSetting(p.settings)
	p.isActive = true
	p.isHandlerRegistered = true
}
func (p *EmailPlugin) Deactivate() {
	p.isActive = false
}
func (p *EmailPlugin) IsActive() bool {
	return p.isActive
}
func (p *EmailPlugin) Version() string {
	return "0.1.5"
}
func (p *EmailPlugin) HasSetting() bool {
	return true
}