# yaml-language-server: $schema=https://schema.zeabur.app/template.json
apiVersion: zeabur.com/v1
kind: Template
metadata:
    name: mail-server-receive
spec:
    description: |
        Deploy the SMTP and IMAP parts of the mail server as separate TCP services.
        This template is meant to unblock real mailbox receive on a dedicated server.
    variables:
        - key: DATABASE_URL
          type: STRING
          name: Database URL
          description: PostgreSQL connection string shared by SMTP and IMAP.
        - key: JWT_SECRET
          type: PASSWORD
          name: JWT secret
          description: JWT signing secret used for mailbox authentication.
        - key: SUPABASE_URL
          type: STRING
          name: Supabase URL
          description: Supabase project URL for attachment storage.
        - key: SUPABASE_SERVICE_ROLE_KEY
          type: PASSWORD
          name: Supabase service role key
          description: Supabase service role key for attachment uploads.
        - key: SUPABASE_ATTACHMENTS_BUCKET
          type: STRING
          name: Attachments bucket
          description: Supabase storage bucket used for attachments.
        - key: SMTP_HOSTNAME
          type: STRING
          name: SMTP hostname
          description: Banner hostname advertised by the SMTP server.
        - key: TLS_KEY_PATH
          type: STRING
          name: TLS key path
          description: Path to the TLS private key inside the container.
        - key: TLS_CERT_PATH
          type: STRING
          name: TLS cert path
          description: Path to the TLS certificate inside the container.
    tags:
        - Mail
        - SMTP
        - IMAP
        - TCP
        - Docker
    readme: |
        # Mail Receive Services

        This template deploys only the SMTP and IMAP services needed for real mailbox receive.
        It reuses the existing mail-server repository and exposes TCP ports for SMTP and IMAP.

        ## After deployment

        1. Check each service's network info.
        2. Point MX to the public SMTP endpoint if port 25 is exposed.
        3. Configure your mail client to use the IMAP endpoint.
    services:
        - name: mail-smtp
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/git/docker/default.svg
          template: GIT
          spec:
            id: mail-smtp
            source:
                source: ARBITRARY_GIT
                gitURL: https://github.com/hotunda365/mail-server.git
                branch: main
                dockerfile: |
                    FROM node:20-alpine AS build
                    WORKDIR /app
                    COPY package*.json ./
                    RUN npm ci --include=dev
                    COPY tsconfig.json ./
                    COPY src ./src
                    RUN npm run build

                    FROM node:20-alpine AS runtime
                    WORKDIR /app
                    ENV NODE_ENV=production
                    COPY package*.json ./
                    RUN npm ci --omit=dev
                    COPY --from=build /app/dist ./dist

                    EXPOSE 25 587
                    CMD ["npm", "run", "start:smtp"]
            ports:
                - id: smtp
                  port: 25
                  type: TCP
                - id: submission
                  port: 587
                  type: TCP
            env:
                DATABASE_URL:
                    default: ${DATABASE_URL}
                JWT_SECRET:
                    default: ${JWT_SECRET}
                LOG_LEVEL:
                    default: info
                NODE_ENV:
                    default: production
                SMTP_HOSTNAME:
                    default: ${SMTP_HOSTNAME}
                SMTP_PORT:
                    default: "25"
                SMTP_SUBMISSION_PORT:
                    default: "587"
                SUPABASE_ATTACHMENTS_BUCKET:
                    default: ${SUPABASE_ATTACHMENTS_BUCKET}
                SUPABASE_SERVICE_ROLE_KEY:
                    default: ${SUPABASE_SERVICE_ROLE_KEY}
                SUPABASE_URL:
                    default: ${SUPABASE_URL}
                TLS_CERT_PATH:
                    default: ${TLS_CERT_PATH}
                TLS_KEY_PATH:
                    default: ${TLS_KEY_PATH}
            healthCheck:
                type: TCP
                port: smtp
            portForwarding:
                enabled: true
        - name: mail-imap
          icon: https://raw.githubusercontent.com/zeabur/service-icons/main/git/docker/default.svg
          template: GIT
          spec:
            id: mail-imap
            source:
                source: ARBITRARY_GIT
                gitURL: https://github.com/hotunda365/mail-server.git
                branch: main
                dockerfile: |
                    FROM node:20-alpine AS build
                    WORKDIR /app
                    COPY package*.json ./
                    RUN npm ci --include=dev
                    COPY tsconfig.json ./
                    COPY src ./src
                    RUN npm run build

                    FROM node:20-alpine AS runtime
                    WORKDIR /app
                    ENV NODE_ENV=production
                    COPY package*.json ./
                    RUN npm ci --omit=dev
                    COPY --from=build /app/dist ./dist

                    EXPOSE 143 993
                    CMD ["npm", "run", "start:imap"]
            ports:
                - id: imap
                  port: 143
                  type: TCP
                - id: imaps
                  port: 993
                  type: TCP
            env:
                DATABASE_URL:
                    default: ${DATABASE_URL}
                JWT_SECRET:
                    default: ${JWT_SECRET}
                LOG_LEVEL:
                    default: info
                NODE_ENV:
                    default: production
                TLS_CERT_PATH:
                    default: ${TLS_CERT_PATH}
                TLS_KEY_PATH:
                    default: ${TLS_KEY_PATH}
            healthCheck:
                type: TCP
                port: imap
            portForwarding:
                enabled: true
