Metadata-Version: 2.1
Name: winmail
Version: 2023.11.21
Summary: Winmail API
Home-page: http://winmail.cn
Author: Sway
Author-email: sway_wang@foxmail.com
License: UNKNOWN
Platform: UNKNOWN
Requires-Python: >=3.6.0
Description-Content-Type: text/markdown

# Winmail

## 介绍

[Winmail](http://winmail.cn) Server 工具类

接口版本： 1.1、1.2

【建议使用】Winmail OpenAPI接口。
【不建议使用】获取Winmail信息和COM接口，Python需运行在Winmail所在机器。

## 安装

```bash
pip install winmail
```

## 更新历史

2023.11.20

- 删除包里多余文件

2023.11.17

- 版本号使用日期，更改到2023.11.17。
- 增加OpenApi1.2接口。



## 使用说明

### 服务启停

```python
from winmail import start_winmail_service, stop_winmail_service

start_winmail_service()
stop_winmail_service()
```



### OpenApi12说明

接口按官方OpenApi1.2版本完成，提供基础的参数提示，方便使用。方法名使用Winmail接口中的方法名命名，有点分隔符的替换为下划线。比如方法domain.added -> domain_added。

```python
from winmail import OpenApi12

api = OpenApi12('localhost', 6080, 'bt6aWe18d5b', 'btt387e78f6871aea2016aR3916eb65e299b7affHb')

login_result = api.login('admin', 'adminpassword', manage_path='admin')
print(login_result)
result = api.user_edited(name='usera', password='newpassword',  domain='test.com', changedpwd=1, authtype=0, status=0)
print(result)
```

1.2版本接口中未包含的可以参考以下调用OpenApi通用类。

### OpenApi说明

可以使用HTTP、HTTPS请求，不检查证书。具体OpenApi的接口完整手册请查看Winmail官方文档。

```python

from winmail import OpenApi as WinmailApi

server = '192.168.1.195'
port = 6080
apikey = 'bt6aWe18d5b'
apisecret = 'btt387e78f6871aea2016aR3916eb65e299b7affHb'

# 管理员接口示例
manage_path = 'admin'
user = 'admin'
pwd = 'adminpassword'    
api = WinmailApi(server, port, apikey, apisecret, use_ssl=False)

login_result = api.login(user, pwd, manage_path)

if login_result['result'] == 'ok':
    # 更新session id防止会话过期，默认为30分钟过期。
    if api.update_session():
        print('update ok')
    else:
        print('update failed')

    # 取域名列表，按API接口说明的参数字典
    method_params = {
        "method": "domain"
    }

    method_result = api.get_api(**method_params)
    
    if method_result['result'] == 'ok':
        print(method_result)
    else:
        print(method_result)

else:
    print(login_result)


# 邮箱用户接口示例
user = 'usera'
pwd = 'mypassword'
# 邮件用户的参考：sessid包含的webmail风格。如果是手机可以使用6，普通PC用0
tid = 6
api = WinmailApi(server, port, apikey, apisecret, use_ssl=False)

login_result = api.login(user, pwd, tid=tid)
if login_result['result'] == 'ok':
    # print(api.url)
    # print(api.sessid)

    if api.update_session():
        print('update ok')
    else:
        print('update failed')

    # 写邮件操作示例
    # 上传附件操作，如果有多个附件可以一次请求也可以多次请求。每次请求的附件都会保留在邮件上除非使用reset清除。建议上传前清一下附件缓存
    method_params = {
        "method": "newmsg.reset",
    }
    method_result = api.get_api(**method_params)
    if method_result['result'] != 'ok':
        print(method_result)

    # 第一次上传多个附件
    method_params = {
        "method": "upload.upload",
        "attachfile": ['E://logs//webmail.log',
                'E://logs//system.log']
    }
    method_result = api.get_api(**method_params)
    if method_result['result'] != 'ok':
        print(method_result)
    # 第二次上传单个附件
    method_params = {
        "method": "upload.upload",
        "attachfile": 'E://logs//admin.log'
    }
    method_result = api.get_api(**method_params)
    if method_result['result'] != 'ok':
        print(method_result)

    # 写邮件信体
    method_params = {
        "method": "newmsg.send",
        "from": "AA<a@195.com>",
        "to": "B用户<b@195.com>",
        "subject": 'OpenApi写信测试',
        "msgbody": "<font color=red>RED content</font>",
        "ishtml": '1',
        "savetosent": '1'
    }
    method_result = api.get_api(**method_params)

    if method_result['result'] == 'ok':
        print(method_result)
    else:
        print(method_result)

else:
    print(login_result)
```

### Winmail模块说明

:warning:Python必须运行在Winmail服务器上。不建议使用。

Winmail模板主要提供Winmail相关的参数路径获取。比如安装目录，域的邮件、网盘存储目录，用户邮件、网盘存储目录等。

也可以获取域名，用户，组列表。因为是直接读取配置文件和数据库，Openapi有接口的，建议使用OpenAPI接口。

#### 初始化和数据库连接说明

已经支持的数据库为Sqlite、mysql、postgresql。

##### 导入Winmail
```python
from winmail import Winmail
wm = Winmail()
print(wm.get_mailarchive_path())
print(wm.get_user_alias_list())
# 按XML路径获取system.cfg中的配置项
print(wm.get_sys_config("./dboption/dbtype"))
```
属性包含：
```txt
winmail_path,
winmail_sys_conf_path,
winmail_domain_conf_path,
winmail_mailgroup_conf_path,
winmail_mailuser_stat_conf_path,
winmail_mailuser_conf_path,
db_type
```

方法包含：

```txt
connect_db,
get_sys_config,
get_db_type,
check_winmail_path,
get_user_mailstore_path,
get_user_netstore_path,
get_domain_mailstore_path,
get_domain_netstore_path,
get_mailbackup_path,
get_mailarchive_path,
get_pri_domain,
get_domain_list,
get_all_domain_list,	
get_domain_tuple_list,
get_user_list,
get_user_alias_list,
get_group_list,
check_pri_domain
```



Winmail中如果使用了Mysql数据库，请在实例化时初始化数据库密码。如：
```python
from winmail import Winmail
wm = Winmail('mysql-password')
wm.connect_db()
```
如果Winmail中使用了Sqlite数据库，实例化时不需要密码。但在使用connect_db时，必须指定Sqlite操作的数据库文件路径，如：
```python
from winmail import Winmail
wm = Winmail()
wm.connect_db(wm.winmail_mailuser_conf_path)
```

### Com接口说明

:warning:Python必须运行在Winmail服务器上。只支持Windows平台，暂未全部完成Com接口，不建议使用。

请参考Winmail的Com接口文档。

```python
from winmail import ComApi as WinmailCom

wc = WinmailCom()
winmail_db_path = wc.get_db_path()
add_domain = wc.add_domain('test.com')
print(add_domain)
```


