Hi all, i want to write a shell script to unblock sendmail user whose mailbox exceed 100MB

Dani AI

Generated

asked for a script to "unblock sendmail user whose mailbox exceed 100MB". was correct to recommend doing the steps manually first. Below is a compact checklist plus a safe, adaptable shell pattern that locates mailboxes >=100MB and prints a suggested unblock action; the script runs in dry-run mode by default and leaves the actual unblock command as an explicit, easily-adapted step.

Common manual checks (run once before automating)

  • Determine mailbox format: mbox files are typically in /var/mail or /var/spool/mail; Maildir users usually have ~/Maildir or a virtual-mail location such as /var/vmail.
  • Confirm how "blocked" is implemented on the host: common places are /etc/mail/access (sendmail), /etc/postfix/access (postfix), hosts.deny, or a custom DB/script.
  • Test these commands manually to verify the unblock workflow (remove access entry, rebuild map, restart MTA) before putting anything into a cron job.

Example (conservative, dry-run) script pattern

#!/usr/bin/env bash
THRESHOLD_MB=${1:-100}
THRESHOLD_KB=$((THRESHOLD_MB * 1024))
DRY_RUN=1   # set to 0 to perform changes (only after manual verification)

echo "Threshold: ${THRESHOLD_MB}MB (dry run=${DRY_RUN})"

for f in /var/mail/* /var/spool/mail/*; do
  [ -f "$f" ] || continue
  user=$(basename "$f")
  size_kb=$(du -sk "$f" 2>/dev/null | cut -f1)
  if [ "$size_kb" -ge "$THRESHOLD_KB" ]; then
    printf "mbox: %s %sKB\n" "$user" "$size_kb"
    printf "suggested unblock: sed -i -e '/^%s@/d' -e '/^%s$/d' /etc/mail/access && makemap hash /etc/mail/access < /etc/mail/access\n" "$user" "$user"
    if [ "$DRY_RUN" -eq 0 ]; then
      sed -i -e "/^${user}@/d" -e "/^${user}$/d" /etc/mail/access
      makemap hash /etc/mail/access < /etc/mail/access
      if command -v systemctl >/dev/null 2>&1; then
        systemctl restart sendmail
      else
        service sendmail restart
      fi
    fi
  fi
done

for d in /home/*/Maildir; do
  [ -d "$d" ] || continue
  user=$(basename "$(dirname "$d")")
  size_kb=$(du -sk "$d" 2>/dev/null | cut -f1)
  if [ "$size_kb" -ge "$THRESHOLD_KB" ]; then
    printf "Maildir: %s %sKB\n" "$user" "$size_kb"
    printf "suggested unblock: same as mbox example (remove access entry and rebuild map)\n"
  fi
done

Notes and cautions

  • Back up any access/map files first (for example, cp -p /etc/mail/access /root/access.bak).
  • The unblock step in the example assumes sendmail uses /etc/mail/access; if the environment uses postfix or another MTA the rebuild command differs (postmap, postalias, etc.).
  • As advised, execute the printed commands manually for one test account, confirm the effect, then enable DRY_RUN=0 or schedule the script.

Here's the 411. First you do this manually then you convert those steps to a script. Some don't understand the need for the first step.

commented: Don't forget to record the steps during the manual phase! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.