iredmail - 注册页 - register - 简单制作

需求:有一个简单的 web 表单,让未登录的访客 注册 邮箱
思路:iredadmin 里,有 Create 的类,照它样子,复制出一个 Register 类来;同样,模仿着添加好 web 访问的路径 和 html 表单

环境:debian 12 里安装好的 iredmail - 1.7.1 SQL 版本

请注意,我不会 Python ,这是我在 Poe GPT 帮助下,简单制作的,仅用于测试。

步骤:

文件内容:iRedAdmin-2.6/controllers/sql/urls.py 中,添加 访问路径 :

'/register', 'controllers.sql.basic.Register',

文件内容:iRedAdmin-2.6/controllers/sql/basic.py 中,添加 Register 的类 :

from libs import iredpwd
from libs.sqllib import general as sql_lib_general
class Register:
    def GET(self):
        available_domains = ['iredmail.demo.anqun.org', 'imap.demo.2xinxian.top', 'example.net']  # 示例域名列表
        form = web.input()
        return web.render(
            'register.html',
            available_domains=available_domains,
            msg=form.get('msg'),
        )

    def POST(self):
        form = web.input()

        domain = form.get('domainName', '').strip().lower()
        username = form.get('username', '').strip().lower()
        newpw = form.get('newpw', '').strip()
        confirmpw = form.get('confirmpw', '').strip()
        cn = form.get('cn', '').strip()  # 获取可选的显示名称

        if not username or not newpw or not confirmpw or not domain:
            return web.seeother(f'/register?msg=INVALID_INPUT')

        if newpw != confirmpw:
            return web.seeother(f'/register?msg=PASSWORDS_DO_NOT_MATCH')

        # 生成密码哈希
        pwscheme = 'SSHA512'
        passwd_hash = iredpwd.generate_password_hash(newpw, pwscheme=pwscheme)

        result = self.add_user(domain, username, passwd_hash, cn)  # 将 cn 传递给 add_user

        if result[0]:
            return web.seeother(f'/register?msg=CREATED')
        else:
            return web.seeother(f'/register?msg={web.urlquote(result[1])}')

    def add_user(self, domain, username, password, cn):
        mail = f"{username}@{domain}"

        if sql_lib_general.is_email_exists(mail):
            return (False, 'ALREADY_EXISTS')

        record = {
            'domain': domain,
            'username': mail,
            'password': password,
            'active': 1,
            'name': cn
        }

        try:
            _wrap = SQLWrap()
            conn = _wrap.conn
            conn.insert('mailbox', **record)
            return (True, )
        except Exception as e:
            return (False, repr(e))

创建模板文件 iRedAdmin-2.6/templates/default/register.html

{% extends "layout.html" %}

{% block title %}{{ _('Add mail user') }}{% endblock title %}
{% block navlinks_create %}class="active"{% endblock %}

{% block main %}
{# Show system message #}
{% if msg %}
    {% if msg.startswith('PW_') %}
        {% set _pw_errors = msg.split(',') %}
        {% for _err in _pw_errors %}
            {{ user_msg_handler(_err) }}
        {% endfor %}
    {% else %}
    {#        {{ user_msg_handler(msg) }} #}
        <p>{{ msg }}</p>
    {% endif %}
{% endif %}

    <div class="content-box">
        <div class="box-body">
            <div class="box-header clear">
                <ul class="tabs clear">
                    <li class="active"><a href="#user_add"><i class="fa fa-plus"></i> {{ _('User') }}</a>
                </ul>

                <h2>{{ _('Add mail user') }}</h2>
            </div>

            <div id="user_add" class="box-wrap clear">
                <form name="form_add_user" method="post" action="{{ctx.homepath}}/register">
                    <div class="form-field clear">
                        <h4 class="size-250 fl-space">{{ _('Mail Domain') }} <span class="required">*</span></h4>
                        <span class="clean-padding">
                            <select name="domainName" id="domainSelect" required>
                                {% for domain in available_domains %}
                                    <option value="{{ domain }}">{{ domain }}</option>
                                {% endfor %}
                            </select>
                        </span>
                    </div>
                
                    <div class="form-field clear">
                        <h4 class="size-250 fl-space">{{ _('Mail Address') }} <span class="required">*</span></h4>
                        <span class="clean-padding">
                            <input type="text" size="35" name="username" value="" autocomplete="off" class="text fl-space" required />@
                        </span>
                    </div>
                
                    <div class="form-field clear">
                        <h4 class="size-250 fl-space">{{ _('Password') }} <span class="required">*</span></h4>
                        <span class="clean-padding">
                            <input type="password" name="newpw" required class="text fl-space" />
                        </span>
                    </div>
                    
                    <div class="form-field clear">
                        <h4 class="size-250 fl-space">{{ _('Confirm Password') }} <span class="required">*</span></h4>
                        <span class="clean-padding">
                            <input type="password" name="confirmpw" required class="text fl-space" />
                        </span>
                    </div>

                    <div class="form-field clear">
                        <h4 class="size-250 fl-space">{{ _('Display Name (Optional)') }}</h4>
                        <span class="clean-padding">
                            <input type="text" name="cn" value="" autocomplete="off" class="text fl-space" />
                        </span>
                    </div>                    
                
                    <div class="form-field clear">
                        <span>
                            <input type="submit" name="submit_add_user" value="{{ _('Add') }}" class="button green"/>
                        </span>
                    </div>
                </form>
            </div>{# -- End box-wrap -- #}
        </div>{# -- End content-box -- #}
    </div>{# -- End box-body -- #}
{% endblock main %}

iredmail 自定义注册页 表单

iredmail 自助注册邮箱成功

iredmail 自助注册的邮箱,成功登录到 Webmail

参考:

标签: iredmail, register

添加新评论