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, orsendmailfor sending emails. - A working network configuration (DNS, internet access).
Tools we will use:
msmtp: A simple-to-configure SMTP client.mailutils: For themailcommand.
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:
-
Create the file
~/.msmtprc:nano ~/.msmtprc -
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 -
Set file permissions:
chmod 600 ~/.msmtprc
4. Setting Up Your Own Mail Server (Postfix)
A. Installing Postfix
-
Install Postfix (for Debian/Ubuntu):
sudo apt install postfixFor RHEL/CentOS:
sudo yum install postfix -
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
-
Log in to the Hetzner DNS Console:
- Go to Hetzner DNS Console.
-
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"
- Select your domain and add the following TXT record:
B. Adding a DKIM Record
-
Generate a DKIM Key:
opendkim-genkey -s default -d your-domain.com -
Add the Public Key to Hetzner DNS:
- Add the content of the
default.txtfile as a new TXT record:default._domainkey IN TXT "v=DKIM1; k=rsa; p=PUBLIC_KEY_HERE"
- Add the content of the
C. Adding a DMARC Record
-
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"
- Add the following TXT record to Hetzner DNS:
-
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:
- Send a test email to dkimvalidator.com.
Testing DMARC:
- Use tools like mxtoolbox.com.
8. Summary
- We have configured CLI email sending using
msmtpandmailutils. - 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! 😊