Skip to content

Commit

Permalink
Merge pull request #86 from Jike11/master
Browse files Browse the repository at this point in the history
feat: Add web push hook functionality and configuration options
  • Loading branch information
Jinnrry authored Feb 27, 2024
2 parents 483bfa1 + 1b2b3a3 commit 9bf01a9
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 18 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ beautiful and cute Logo for this project!
## Features

* Single file operation and easy deployment.

* The binary file is only 15MB and takes up less than 10M of memory during the run.

* Support dkim, spf checksum, [Email Test](https://www.mail-tester.com/) score 10 points if correctly configured.

* Implementing the ACME protocol, the program will automatically obtain and update Let's Encrypt certificates.

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

* At present, only the core function of sending and receiving emails has been completed. Basically, it can only be used
by a single person, and does not deal with issues related to permission management in the process of multiple users.

* The UI is ugly

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

* [Click Here](https://github.com/Jinnrry/PMail/releases) Download a program file that matches you.

* Or use Docker `docker pull ghcr.io/jinnrry/pmail:latest`

## 2、Run

`./pmail`
`./pmail`

Or
Or

`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`

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

## 6、Telegram Message Push

Create bot and get token from [BotFather](https://t.me/BotFather)
Open the `config/config.json` file in the run directory, edit a few configuration items at the beginning of `tg`and restart the service.

## 7、WebHook Push

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.

# Configuration file format description

```json
Expand All @@ -108,6 +108,8 @@ Open the `config/config.json` file in the run directory, edit a few configuratio
"weChatPushUserId": "", // weChat UserId
"tgChatId": "", // telegram chatid
"tgBotToken": "", // telegram token
"webPushUrl": "", // webhook 推送地址
"webPushToken": "", // webhook 推送 token
"isInit": true // If false, it will enter the bootstrap process.
}
```
Expand Down
20 changes: 12 additions & 8 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ PMail是一个追求极简部署流程、极致资源占用的个人域名邮箱

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

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

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

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

* [点击这里](https://github.com/Jinnrry/PMail/releases)下载一个与你匹配的程序文件。

* 或者使用Docker运行 `docker pull ghcr.io/jinnrry/pmail:latest`

## 2、运行

`./pmail`
`./pmail`

或者

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

## 6、Telegram推送

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

## 7、WebHook推送

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

# 配置文件说明

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

1、前端: vue3+element-plus

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

2、后端: golang + mysql

后端代码进入`server`文件夹,运行`main.go`文件
后端代码进入 `server`文件夹,运行 `main.go`文件

## 后端接口文档

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

## 插件开发

参考微信推送插件`server/hooks/wechat_push/wechat_push.go`

参考微信推送插件 `server/hooks/wechat_push/wechat_push.go`
2 changes: 2 additions & 0 deletions server/config/config.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"weChatPushUserId": "",
"tgChatId": "",
"tgBotToken": "",
"webPushUrl": "",
"webPushToken": "",
"isInit": true,
"httpsEnabled": 2
}
2 changes: 2 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Config struct {
TgBotToken string `json:"tgBotToken"`
TgChatId string `json:"tgChatId"`
IsInit bool `json:"isInit"`
WebPushUrl string `json:"webPushUrl"`
WebPushToken string `json:"webPushToken"`
Tables map[string]string `json:"-"`
TablesInitData map[string]string `json:"-"`
}
Expand Down
2 changes: 2 additions & 0 deletions server/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"weChatPushUserId": "",
"tgChatId": "",
"tgBotToken": "",
"webPushUrl": "",
"webPushToken": "",
"isInit": true,
"httpsEnabled": 1
}
2 changes: 2 additions & 0 deletions server/config/config_mysql.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"weChatPushUserId": "",
"tgChatId": "",
"tgBotToken": "",
"webPushUrl": "",
"webPushToken": "",
"isInit": true,
"httpsEnabled": 2
}
2 changes: 2 additions & 0 deletions server/hooks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hooks
import (
"pmail/dto/parsemail"
"pmail/hooks/telegram_push"
"pmail/hooks/web_push"
"pmail/hooks/wechat_push"
"pmail/utils/context"
)
Expand All @@ -26,5 +27,6 @@ func Init() {
HookList = []EmailHook{
wechat_push.NewWechatPushHook(),
telegram_push.NewTelegramPushHook(),
web_push.NewWebPushHook(),
}
}
88 changes: 88 additions & 0 deletions server/hooks/web_push/web_push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package web_push

import (
"bytes"
"encoding/json"
"net/http"
"pmail/config"
"pmail/dto/parsemail"
"pmail/utils/context"

log "github.com/sirupsen/logrus"
)

type WebPushHook struct {
url string
token string
}

// EmailData 用于存储解析后的邮件数据
type EmailData struct {
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Body string `json:"body"`
Token string `json:"token"`
}

func (w *WebPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {

}

func (w *WebPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {

}

func (w *WebPushHook) ReceiveParseBefore(email []byte) {

}

func (w *WebPushHook) ReceiveParseAfter(email *parsemail.Email) {
if w.url == "" {
return
}

content := string(email.Text)

if content == "" {
content = email.Subject
}

webhookURL := w.url // 替换为您的 Webhook URL

to := make([]string, len(email.To))
for i, user := range email.To {
to[i] = user.EmailAddress
}

data := EmailData{
From: email.From.EmailAddress,
To: to,
Subject: email.Subject,
Body: content,
Token: w.token,
}

var ctx *context.Context = nil
jsonData, err := json.Marshal(data)

if err != nil {
log.WithContext(ctx).Errorf("web push error %+v", err)
}

resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.WithContext(ctx).Errorf("web push error %+v", err)
}
defer resp.Body.Close()
}

func NewWebPushHook() *WebPushHook {

ret := &WebPushHook{
url: config.Instance.WebPushUrl,
token: config.Instance.WebPushToken,
}
return ret

}
19 changes: 19 additions & 0 deletions server/hooks/web_push/wechat_push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package web_push

import (
"pmail/config"
"pmail/dto/parsemail"
"testing"
)

func testInit() {

config.Init()

}
func TestWebPushHook_ReceiveParseAfter(t *testing.T) {
testInit()

w := NewWebPushHook()
w.ReceiveParseAfter(&parsemail.Email{Subject: "标题", Text: []byte("文本内容")})
}
5 changes: 3 additions & 2 deletions server/http_server/https_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"embed"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"io/fs"
olog "log"
"net/http"
Expand All @@ -19,6 +17,9 @@ import (
"pmail/utils/context"
"pmail/utils/id"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)

//go:embed dist/*
Expand Down

0 comments on commit 9bf01a9

Please sign in to comment.