Guide: Sending Emails from the CLI

1. Basics: What do you need for email sending via CLI?

  • A mail server or SMTP relay to process the emails.
  • A CLI tool like mail, msmtp, or sendmail for sending emails.
  • A working network configuration (DNS, internet access).

Tools we will use:

  • msmtp: A simple-to-configure SMTP client.
  • mailutils: For the mail command.

2. Installing the Tools

For Debian/Ubuntu:

sudo apt update
sudo apt install msmtp mailutils

For RHEL/CentOS:

sudo yum install msmtp mailx

3. Setting Up msmtp

Creating the Configuration File:

  1. Create the file ~/.msmtprc:

    nano ~/.msmtprc
  2. Example configuration for Gmail:

    account default
    host smtp.gmail.com
    port 587
    auth on
    user your.email@gmail.com
    password your_password
    tls on
    tls_starttls on
    logfile ~/.msmtp.log
  3. Set file permissions:

    chmod 600 ~/.msmtprc

4. Setting Up Your Own Mail Server (Postfix)

A. Installing Postfix

  1. Install Postfix (for Debian/Ubuntu):

    sudo apt install postfix

    For RHEL/CentOS:

    sudo yum install postfix
  2. During installation, select "Internet Site" as the configuration type.

B. Configuring Postfix

Edit the main configuration file /etc/postfix/main.cf:

myhostname = your-hostname
mydomain = your-domain.com
myorigin = $mydomain
inet_interfaces = all
relayhost =

Restart Postfix after changes:

sudo systemctl restart postfix

C. Testing Postfix

Send a test email:

echo "This is a test email" | mail -s "Postfix Test" recipient@example.com

5. Sending Files via Email

Sending a Single File:

echo "Here is the attached file" | mail -s "Subject" -A /path/to/file recipient@example.com

Sending Multiple Attachments:

echo "Multiple files attached" | mail -s "Subject" -A /path/to/file1 -A /path/to/file2 recipient@example.com

6. Configuring DNS Records for Hetzner

A. Adding an SPF Record

  1. Log in to the Hetzner DNS Console:

  2. Add a TXT Record for SPF:

    • Select your domain and add the following TXT record:
      @  IN  TXT  "v=spf1 mx a ip4:YOUR_IP_ADDRESS ~all"

B. Adding a DKIM Record

  1. Generate a DKIM Key:

    opendkim-genkey -s default -d your-domain.com
  2. Add the Public Key to Hetzner DNS:

    • Add the content of the default.txt file as a new TXT record:
      default._domainkey  IN  TXT  "v=DKIM1; k=rsa; p=PUBLIC_KEY_HERE"

C. Adding a DMARC Record

  1. Add the DMARC Record:

    • Add the following TXT record to Hetzner DNS:
      _dmarc  IN  TXT  "v=DMARC1; p=none; rua=mailto:report@your-domain.com"
  2. For stricter rules:

    _dmarc  IN  TXT  "v=DMARC1; p=reject; rua=mailto:report@your-domain.com"

7. Testing DNS Records

Testing SPF:

dig TXT your-domain.com

Testing DKIM:

Testing DMARC:


8. Summary

  • We have configured CLI email sending using msmtp and mailutils.
  • A complete mail server setup with Postfix has been included.
  • DNS records (SPF, DKIM, DMARC) for Hetzner were configured for secure email sending.

Good luck sending emails! 😊