Skip to content

Commit 1b2b3a3

Browse files
committed
feat: Add web push hook functionality and configuration options
1 parent 483bfa1 commit 1b2b3a3

File tree

10 files changed

+142
-18
lines changed

10 files changed

+142
-18
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ beautiful and cute Logo for this project!
2121
## Features
2222

2323
* Single file operation and easy deployment.
24-
2524
* The binary file is only 15MB and takes up less than 10M of memory during the run.
26-
2725
* Support dkim, spf checksum, [Email Test](https://www.mail-tester.com/) score 10 points if correctly configured.
28-
2926
* Implementing the ACME protocol, the program will automatically obtain and update Let's Encrypt certificates.
3027

3128
> By default, a ssl certificate is generated for the web service, allowing pages to use the https protocol.
3229
> If you have your own gateway or don't need https, set `httpsEnabled` to `2` in the configuration file so that the web
3330
> service will not use https.
34-
(Note: Even if you don't need https, please make sure the path to the ssl certificate file is correct, although the web
31+
> (Note: Even if you don't need https, please make sure the path to the ssl certificate file is correct, although the web
3532
> service doesn't use the certificate anymore, the smtp protocol still needs the certificate)
3633
3734
* Support pop3, smtp protocol, you can use any mail client you like.
@@ -40,7 +37,6 @@ beautiful and cute Logo for this project!
4037

4138
* At present, only the core function of sending and receiving emails has been completed. Basically, it can only be used
4239
by a single person, and does not deal with issues related to permission management in the process of multiple users.
43-
4440
* The UI is ugly
4541

4642
# How to run
@@ -52,14 +48,13 @@ First go to [spamhaus](https://check.spamhaus.org/) and check your domain name a
5248
## 1、Download
5349

5450
* [Click Here](https://github.com/Jinnrry/PMail/releases) Download a program file that matches you.
55-
5651
* Or use Docker `docker pull ghcr.io/jinnrry/pmail:latest`
5752

5853
## 2、Run
5954

60-
`./pmail`
55+
`./pmail`
6156

62-
Or
57+
Or
6358

6459
`docker run -p 25:25 -p 80:80 -p 443:443 -p 110:110 -p 465:465 -p 995:995 -v $(pwd)/config:/work/config ghcr.io/jinnrry/pmail:latest`
6560

@@ -82,9 +77,14 @@ Open the `config/config.json` file in the run directory, edit a few configuratio
8277
and restart the service.
8378

8479
## 6、Telegram Message Push
80+
8581
Create bot and get token from [BotFather](https://t.me/BotFather)
8682
Open the `config/config.json` file in the run directory, edit a few configuration items at the beginning of `tg`and restart the service.
8783

84+
## 7、WebHook Push
85+
86+
Open the `config/config.json` file in the running directory, edit the webPushUrl and webPushToken (optional). After receiving an email, the email information will be posted to the hook address, and the token will also be placed in the body for easy verification. After configuring, restart the service.
87+
8888
# Configuration file format description
8989

9090
```json
@@ -108,6 +108,8 @@ Open the `config/config.json` file in the run directory, edit a few configuratio
108108
"weChatPushUserId": "", // weChat UserId
109109
"tgChatId": "", // telegram chatid
110110
"tgBotToken": "", // telegram token
111+
"webPushUrl": "", // webhook 推送地址
112+
"webPushToken": "", // webhook 推送 token
111113
"isInit": true // If false, it will enter the bootstrap process.
112114
}
113115
```

README_CN.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ PMail是一个追求极简部署流程、极致资源占用的个人域名邮箱
3232

3333
实现了ACME协议,程序将自动获取并更新Let’s Encrypt证书。
3434

35-
默认情况下,会为web后台也生成ssl证书,让后台使用https访问,如果你有自己的网关层,不需要https的话,在配置文件中将`httpsEnabled`
36-
设置为`2`,这样管理后台就不会使用https协议。( 注意:即使你不需要https,也请保证ssl证书文件路径正确,http协议虽然不使用证书了,但是smtp协议还需要证书)
35+
默认情况下,会为web后台也生成ssl证书,让后台使用https访问,如果你有自己的网关层,不需要https的话,在配置文件中将 `httpsEnabled`
36+
设置为 `2`,这样管理后台就不会使用https协议。( 注意:即使你不需要https,也请保证ssl证书文件路径正确,http协议虽然不使用证书了,但是smtp协议还需要证书)
3737

3838
### 5、邮件客户端支持
3939

@@ -56,12 +56,11 @@ PMail是一个追求极简部署流程、极致资源占用的个人域名邮箱
5656
## 1、下载文件
5757

5858
* [点击这里](https://github.com/Jinnrry/PMail/releases)下载一个与你匹配的程序文件。
59-
6059
* 或者使用Docker运行 `docker pull ghcr.io/jinnrry/pmail:latest`
6160

6261
## 2、运行
6362

64-
`./pmail`
63+
`./pmail`
6564

6665
或者
6766

@@ -83,8 +82,12 @@ PMail是一个追求极简部署流程、极致资源占用的个人域名邮箱
8382
打开运行目录下的 `config/config.json`文件,编辑 `weChatPush` 开头的几个配置项,重启服务即可。
8483

8584
## 6、Telegram推送
85+
8686
[BotFather](https://t.me/BotFather) 创建并获取令牌机器人。 打开运行目录下的 config/config.json 文件,编辑 `tg` 开头的几个配置项,重启服务即可。
8787

88+
## 7、WebHook推送
89+
90+
打开运行目录下的 `config/config.json`文件,编辑 webPushUrl 跟webPushToken (可选),接收到邮件后会往hook地址post发送邮件信息,token也会放在body中,方便需要的进行校验,配置完重启服务即可。
8891

8992
# 配置文件说明
9093

@@ -109,6 +112,8 @@ PMail是一个追求极简部署流程、极致资源占用的个人域名邮箱
109112
"weChatPushUserId": "", // 微信推送用户id
110113
"tgChatId": "", // telegram 推送chatid
111114
"tgBotToken": "", // telegram 推送 token
115+
"webPushUrl": "", // webhook 推送地址
116+
"webPushToken": "", // webhook 推送 token
112117
"isInit": true // 为false的时候会进入安装引导流程
113118
}
114119
```
@@ -129,17 +134,16 @@ SMTP端口: 25/465(SSL)
129134

130135
1、前端: vue3+element-plus
131136

132-
前端代码位于`fe`目录中,运行参考`fe`目录中的README文件
137+
前端代码位于 `fe`目录中,运行参考 `fe`目录中的README文件
133138

134139
2、后端: golang + mysql
135140

136-
后端代码进入`server`文件夹,运行`main.go`文件
141+
后端代码进入 `server`文件夹,运行 `main.go`文件
137142

138143
## 后端接口文档
139144

140145
[参见Wiki](https://github.com/Jinnrry/PMail/wiki)
141146

142147
## 插件开发
143148

144-
参考微信推送插件`server/hooks/wechat_push/wechat_push.go`
145-
149+
参考微信推送插件 `server/hooks/wechat_push/wechat_push.go`

server/config/config.dev.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"weChatPushUserId": "",
1818
"tgChatId": "",
1919
"tgBotToken": "",
20+
"webPushUrl": "",
21+
"webPushToken": "",
2022
"isInit": true,
2123
"httpsEnabled": 2
2224
}

server/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Config struct {
3232
TgBotToken string `json:"tgBotToken"`
3333
TgChatId string `json:"tgChatId"`
3434
IsInit bool `json:"isInit"`
35+
WebPushUrl string `json:"webPushUrl"`
36+
WebPushToken string `json:"webPushToken"`
3537
Tables map[string]string `json:"-"`
3638
TablesInitData map[string]string `json:"-"`
3739
}

server/config/config.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"weChatPushUserId": "",
1818
"tgChatId": "",
1919
"tgBotToken": "",
20+
"webPushUrl": "",
21+
"webPushToken": "",
2022
"isInit": true,
2123
"httpsEnabled": 1
2224
}

server/config/config_mysql.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"weChatPushUserId": "",
1818
"tgChatId": "",
1919
"tgBotToken": "",
20+
"webPushUrl": "",
21+
"webPushToken": "",
2022
"isInit": true,
2123
"httpsEnabled": 2
2224
}

server/hooks/base.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hooks
33
import (
44
"pmail/dto/parsemail"
55
"pmail/hooks/telegram_push"
6+
"pmail/hooks/web_push"
67
"pmail/hooks/wechat_push"
78
"pmail/utils/context"
89
)
@@ -26,5 +27,6 @@ func Init() {
2627
HookList = []EmailHook{
2728
wechat_push.NewWechatPushHook(),
2829
telegram_push.NewTelegramPushHook(),
30+
web_push.NewWebPushHook(),
2931
}
3032
}

server/hooks/web_push/web_push.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package web_push
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
"pmail/config"
8+
"pmail/dto/parsemail"
9+
"pmail/utils/context"
10+
11+
log "github.com/sirupsen/logrus"
12+
)
13+
14+
type WebPushHook struct {
15+
url string
16+
token string
17+
}
18+
19+
// EmailData 用于存储解析后的邮件数据
20+
type EmailData struct {
21+
From string `json:"from"`
22+
To []string `json:"to"`
23+
Subject string `json:"subject"`
24+
Body string `json:"body"`
25+
Token string `json:"token"`
26+
}
27+
28+
func (w *WebPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {
29+
30+
}
31+
32+
func (w *WebPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
33+
34+
}
35+
36+
func (w *WebPushHook) ReceiveParseBefore(email []byte) {
37+
38+
}
39+
40+
func (w *WebPushHook) ReceiveParseAfter(email *parsemail.Email) {
41+
if w.url == "" {
42+
return
43+
}
44+
45+
content := string(email.Text)
46+
47+
if content == "" {
48+
content = email.Subject
49+
}
50+
51+
webhookURL := w.url // 替换为您的 Webhook URL
52+
53+
to := make([]string, len(email.To))
54+
for i, user := range email.To {
55+
to[i] = user.EmailAddress
56+
}
57+
58+
data := EmailData{
59+
From: email.From.EmailAddress,
60+
To: to,
61+
Subject: email.Subject,
62+
Body: content,
63+
Token: w.token,
64+
}
65+
66+
var ctx *context.Context = nil
67+
jsonData, err := json.Marshal(data)
68+
69+
if err != nil {
70+
log.WithContext(ctx).Errorf("web push error %+v", err)
71+
}
72+
73+
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
74+
if err != nil {
75+
log.WithContext(ctx).Errorf("web push error %+v", err)
76+
}
77+
defer resp.Body.Close()
78+
}
79+
80+
func NewWebPushHook() *WebPushHook {
81+
82+
ret := &WebPushHook{
83+
url: config.Instance.WebPushUrl,
84+
token: config.Instance.WebPushToken,
85+
}
86+
return ret
87+
88+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package web_push
2+
3+
import (
4+
"pmail/config"
5+
"pmail/dto/parsemail"
6+
"testing"
7+
)
8+
9+
func testInit() {
10+
11+
config.Init()
12+
13+
}
14+
func TestWebPushHook_ReceiveParseAfter(t *testing.T) {
15+
testInit()
16+
17+
w := NewWebPushHook()
18+
w.ReceiveParseAfter(&parsemail.Email{Subject: "标题", Text: []byte("文本内容")})
19+
}

server/http_server/https_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"embed"
55
"encoding/json"
66
"fmt"
7-
log "github.com/sirupsen/logrus"
8-
"github.com/spf13/cast"
97
"io/fs"
108
olog "log"
119
"net/http"
@@ -19,6 +17,9 @@ import (
1917
"pmail/utils/context"
2018
"pmail/utils/id"
2119
"time"
20+
21+
log "github.com/sirupsen/logrus"
22+
"github.com/spf13/cast"
2223
)
2324

2425
//go:embed dist/*

0 commit comments

Comments
 (0)