Skip to content

Instantly share code, notes, and snippets.

@ripsnortntear
Created March 31, 2025 06:24
Show Gist options
  • Save ripsnortntear/e5ca95ca8444d312f25bca782cb462f4 to your computer and use it in GitHub Desktop.
Save ripsnortntear/e5ca95ca8444d312f25bca782cb462f4 to your computer and use it in GitHub Desktop.
This script creates a new email user, sets up their Maildir structure, assigns them to the appropriate groups, and prompts for a password, ensuring the user is configured for email services.
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0"
echo "This script creates a new email user and sets up the necessary directories and groups."
}
# Check if the script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or use sudo."
exit 1
fi
# Prompt for the new email username
read -p "Enter the new email username (without domain, e.g., 'support'): " username
# Check if the username is empty
if [ -z "$username" ]; then
echo "Username cannot be empty."
exit 1
fi
# Create the user
useradd -m -s /bin/bash "$username"
# Set a password for the user
passwd "$username"
# Create the Maildir structure
mkdir -p /home/"$username"/Maildir/{cur,new,tmp}
chown -R "$username":"$username" /home/"$username"/Maildir
chmod -R 700 /home/"$username"/Maildir
# Add user to mail and dovecot groups
if ! getent group mail > /dev/null; then
groupadd mail
fi
if ! getent group dovecot > /dev/null; then
groupadd dovecot
fi
usermod -aG mail,dovecot "$username"
# Output the user's email address
echo "User created successfully!"
echo "Email address: [email protected]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment