Skip to content

Commit 0dfa649

Browse files
committed
send email to subscribers
1 parent 22f3acb commit 0dfa649

File tree

4 files changed

+125
-17
lines changed

4 files changed

+125
-17
lines changed

controllers/mail.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
6+
"strings"
7+
8+
"strconv"
9+
10+
"github.com/gin-gonic/gin"
11+
"github.com/pkg/errors"
12+
"github.com/wangsongyan/wblog/models"
13+
)
14+
15+
func SendMail(c *gin.Context) {
16+
subject := c.PostForm("subject")
17+
content := c.PostForm("content")
18+
userId := c.Query("userId")
19+
20+
var err error
21+
if subject == "" || content == "" || userId == "" {
22+
err = errors.New("error parameter.")
23+
}
24+
if err == nil {
25+
var uid uint64
26+
uid, err = strconv.ParseUint(userId, 10, 64)
27+
if err == nil {
28+
var subscriber *models.Subscriber
29+
subscriber, err = models.GetSubscriberById(uint(uid))
30+
if err == nil {
31+
err = sendMail(subscriber.Email, subject, content)
32+
}
33+
}
34+
}
35+
if err == nil {
36+
c.JSON(http.StatusOK, gin.H{
37+
"succeed": true,
38+
})
39+
} else {
40+
c.JSON(http.StatusOK, gin.H{
41+
"succeed": false,
42+
"msg": err.Error(),
43+
})
44+
}
45+
}
46+
47+
func SendBatchMail(c *gin.Context) {
48+
subject := c.PostForm("subject")
49+
content := c.PostForm("content")
50+
var err error
51+
if subject == "" || content == "" {
52+
err = errors.New("error parameter.")
53+
}
54+
if err == nil {
55+
var subscribers []*models.Subscriber
56+
subscribers, err = models.ListSubscriber(true)
57+
if err == nil {
58+
emails := make([]string, 0)
59+
for _, subscriber := range subscribers {
60+
emails = append(emails, subscriber.Email)
61+
}
62+
err = sendMail(strings.Join(emails, ";"), subject, content)
63+
}
64+
}
65+
if err == nil {
66+
c.JSON(http.StatusOK, gin.H{
67+
"succeed": true,
68+
})
69+
} else {
70+
c.JSON(http.StatusOK, gin.H{
71+
"succeed": false,
72+
"msg": err.Error(),
73+
})
74+
}
75+
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ func main() {
157157
// backup
158158
authorized.POST("/backup", controllers.BackupPost)
159159
authorized.POST("/restore", controllers.RestorePost)
160+
161+
// mail
162+
authorized.POST("/new_mail", controllers.SendMail)
163+
authorized.POST("/new_batchmail", controllers.SendBatchMail)
160164
}
161165

162166
router.Run(system.GetConfiguration().Addr)

models/models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,12 @@ func GetSubscriberBySignature(key string) (*Subscriber, error) {
569569
return &subscriber, err
570570
}
571571

572+
func GetSubscriberById(id uint) (*Subscriber, error) {
573+
var subscriber Subscriber
574+
err := DB.First(&subscriber, id).Error
575+
return &subscriber, err
576+
}
577+
572578
// Link
573579
func (link *Link) Insert() error {
574580
return DB.FirstOrCreate(link, "url = ?", link.Url).Error

views/admin/subscriber.html

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<!-- Content Header (Page header) -->
4444
<section class="content-header">
4545
<h1>
46-
<small>订阅管理<a class="btn btn-primary" href="#"><span class="glyphicon glyphicon-plus"></span>群发</a></small>
46+
<small>订阅管理<a class="btn btn-primary" href="javascript:void(0);" data-href="/admin/new_batchmail" data-toggle="modal" data-target="#confirm-delete"><span class="glyphicon glyphicon-plus"></span>群发</a></small>
4747
</h1>
4848
<ol class="breadcrumb">
4949
<li><a href="/admin/index"><i class="fa fa-dashboard"></i> Home</a></li>
@@ -87,7 +87,7 @@ <h3 class="box-title">Hover Data Table</h3>
8787
<td>
8888
{{if .VerifyState}}
8989
{{if .SubscribeState}}
90-
<a href="#" target="_blank" class="btn btn-primary">发邮件</a>
90+
<a href="javascript:void(0);" class="btn btn-primary btnsend" data-href="/admin/new_mail?userId={{.ID}}" data-toggle="modal" data-target="#confirm-delete">发邮件</a>
9191
{{end}}
9292
{{end}}
9393
</td>
@@ -115,14 +115,27 @@ <h3 class="box-title">Hover Data Table</h3>
115115
<div class="modal-dialog">
116116
<div class="modal-content">
117117
<div class="modal-header">
118-
请确认
118+
发送邮件
119119
</div>
120120
<div class="modal-body">
121-
确认删除该记录吗?
121+
<form id="form_data" role="form" class="form-horizontal">
122+
<div class="form-group">
123+
<label class="col-sm-2 control-label">邮件主题</label>
124+
<div class="col-sm-10">
125+
<input name="subject" class="form-control">
126+
</div>
127+
</div>
128+
<div class="form-group">
129+
<label class="col-sm-2 control-label">邮件内容</label>
130+
<div class="col-sm-10">
131+
<textarea name="content" class="form-control" rows="3"></textarea>
132+
</div>
133+
</div>
134+
</form>
122135
</div>
123136
<div class="modal-footer">
124137
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
125-
<a class="btn btn-danger btn-ok">删除记录</a>
138+
<a class="btn btn-primary btn-ok">发送</a>
126139
</div>
127140
</div>
128141
</div>
@@ -150,23 +163,33 @@ <h3 class="box-title">Hover Data Table</h3>
150163
});
151164
});
152165

153-
function pushlish(id){
154-
$.post("/admin/page/"+id+"/publish",{},function(result){
155-
console.log(result);
156-
if(result.succeed){
157-
window.location.href = window.location.href;
158-
}
159-
},"json")
160-
}
161-
162166
$('#confirm-delete').on('show.bs.modal', function(e) {
167+
$(this).find('.btn-ok').unbind("click");
163168
$(this).find('.btn-ok').click(function(){
164-
$.post($(e.relatedTarget).data('href'),{},function(reuslt){
165-
window.location.href = window.location.href;
166-
},'json');
169+
var subject = $('input[name="subject"]').val();
170+
var content = $('textarea[name="content"]').val();
171+
172+
if(!subject){
173+
alert("请填写主题");
174+
return;
175+
}
176+
if(!content){
177+
alert("请填写内容");
178+
return;
179+
}
167180

181+
$.post($(e.relatedTarget).data('href'),{subject:subject,content:content},function(result){
182+
console.log(result);
183+
if(result.succeed){
184+
alert("发送成功");
185+
}else{
186+
alert(result.msg);
187+
}
188+
//window.location.href = window.location.href;
189+
},'json');
168190
});
169191
});
192+
170193
</script>
171194
</body>
172195
</html>

0 commit comments

Comments
 (0)