How to Set Up mailx on Linux to Send Emails via Gmail

Sending emails from the command line in Linux can be incredibly useful for automation, notifications, or simply managing tasks efficiently. In this guide, I’ll walk you through setting up mailx— a popular command-line mail client that allows sending emails directly from the terminal, on Linux to send emails using your Gmail account. Let’s get started!
Step 1 : Install mailtutils.
First, check if mailx is installed. Open your terminal and run:
mailx -V
If it’s not installed, install it using your package manager.
On Ubuntu/Debian:
sudo apt update
sudo apt install mailutils
On CentOS/RHEL:
sudo yum install mailx
On Fedora:
sudo dnf install mailx
Once installed, verify the installation with mailx -V.
A. When installing mailx, Select General Email Configuration Type
You’ll see a prompt titled "General type of mail configuration".
Use the arrow keys to highlight "Internet Site" and press "Enter."
Why? This option configures Postfix to send emails directly to external mail servers (like Gmail’s SMTP), which is exactly what we need for relaying emails.
B. Set System Mail Name
Next, you’ll be asked for the "System mail name".
Enter: localhost.localdomain
localhost.localdomainWhat is this? This is the fully qualified domain name (FQDN) that Postfix uses to identify itself when sending emails. For our purposes—relaying through Gmail—it’s not critical what you choose, since Gmail will handle the actual delivery. However,
localhost.localdomainis a safe, generic choice for a local or test system.Alternative: If your server has a real domain (e.g.,
myserver.example.com), you can use that instead. To check your system’shostname, run:hostname -f
Once you’ve entered the system mail name, the installation will complete, and postfix will be ready for further configuration.
Step 2 : Generate a Gmail App Password
Gmail requires secure authentication, so we’ll use an App Password. You can create gmail password using the following link : https://account.google.com/apppasswords
Generate and copy the 16-character App Password. Keep it safe—you’ll need it soon!
Sample Password format :
bugc musn equg wscb
Step 3 : Configure Postfix
Next, we’ll edit Postfix’s main main.cf configuration file present on path /etc/postfix/main.cf
Backup the Configuration: Always take a backup before making changes:
sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.bkpEdit
main.cfconfiguration file : Open the file in a text editor:sudo vim /etc/postfix/main.cfAdd or modify these lines at the end of the file:
# Gmail SMTP relay relayhost = [smtp.gmail.com]:587 # Enable SASL authentication smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous # Enable TLS encryption smtp_use_tls = yes smtp_tls_security_level = encrypt smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
relayhost: Specifies Gmail’s SMTP server and port 587 (STARTTLS).smtp_sasl_*: Configures SASL authentication with the hashed password file.smtp_tls_*: Ensures encrypted communication with Gmail.
Step 4 : Configure SMTP Authentication
Postfix uses a SASL (Simple Authentication and Security Layer) password file to store Gmail credentials securely.
Edit the SASL Password File: Open
/etc/postfix/sasl_passwdwith a text editor (create it if it doesn’t exist):sudo vim /etc/postfix/sasl_passwdAdd this line, replacing
<your-email@gmail.com>and<your-app-password>:[smtp.gmail.com]:587 <your-email@gmail.com>:<your-generated-app-password>Example :
[smtp.gmail.com]:587 test-example@gmail.com:bugc musn equg wscbSave and exit (
:wqin vim).Secure the File: Restrict permissions to protect your credentials:
sudo chmod 600 /etc/postfix/sasl_passwdCreate a Hashed Database: Convert the password file into a format Postfix can read:
sudo postmap /etc/postfix/sasl_passwdThis generates
/etc/postfix/sasl_passwd.db. Verify it exists:ls -l /etc/postfix/sasl_passwd.dbYou should see the
.dbfile in the directory.
Step 5 : Restart Postfix
Apply the changes by restarting the Postfix service:
sudo systemctl restart postfix
Ensure it’s running:
sudo systemctl status postfix
Step 6 : Test Sending an Email
Use the mailx command to test your setup:
echo "This is a test email body" | mailx -s "Test Email via Postfix" <recipient@example.com>
Replace <recipient@example.com> with the target email address. If successful, the recipient should receive the email, and it’ll appear in your Gmail "Sent" folder.
You’ve now configured Postfix to send emails through Gmail! This setup is ideal for system alerts, cron job outputs, or scripted notifications.
Troubleshoot
If the email doesn’t send:
Check Logs: View Postfix logs for errors:
sudo tail -f /var/log/mail.logApp Password: Ensure it’s correct (no extra spaces). You can also check Step 2 to check the password format.
Firewall: Verify port 587 is open (
sudo ufw allow 587if usingUFW).