How to Send Emails via Command Line in Linux with Postfix
It is common to send emails using web interfaces like Gmail, Outlook, or Yahoo. However, it is also possible to send emails via the command line. In this blog, we will learn how to send emails using the command line on Linux-based systems. The steps are straightforward and easy to follow.
What You Need
- A Linux OS
- Postfix installed
- Mailutils installed
Step 1: Download the Necessary Tools
Start by installing the required tools on your Linux machine.
Tip: Ask ChatGPT for corresponding commands if you are using a Linux distribution other than Debian-based Linux.
Install Postfix
Postfix is the mail server we will use for sending emails. To install it on a Debian-based Linux system, run the following command:
sudo apt install postfix
During installation, choose Internet Site as the configuration option.
Tip: If your system doesn’t find the repository, update it first with
sudo apt update
Install Mailutils
Mailutils provides the mail
command that we will use to send emails. Install it with:
sudo apt install mailutils
Check Postfix Installation
Ensure Postfix is installed and running by checking its status:
systemctl status postfix
If Postfix is disabled, enable it by running:
sudo systemctl start postfix
Step 2: Test Email Sending
To test if everything is working, you can send an email to a temporary email address (e.g., from Temp Mail).
Use the following command to send a simple email:
echo "This is a test email content" | mail -a "From: Test <[email protected]>" -s "Put the subject here" [email protected]
- Replace
[email protected]
with the actual recipient’s email address.
Note: This method will not work with email providers like Gmail or Outlook because they often block untrusted mail servers. Use Temp Mail for testing.
Step 3: Bypass Email Filtering
To send emails to services like Gmail or Outlook, you need to use a trusted mail relay SMTP server. The command from last step will be filtered, because systems like Gmail and Outlook are very secure. Here’s how to set up SMTP relay server in postfix:
Obtain SMTP Relay Credentials
Find a third-party email relay service that provides SMTP credentials. This is an example:
- SMTP Server: smtp.server.test
- Port: 587
- Email: [email protected]
- Password: password
Install SASL Authentication Modules
SASL (Simple Authentication and Security Layer) is a protocol used to add authentication support to communication protocols, like SMTP, ensuring secure email transmission.
Install the required modules for SASL authentication:
sudo apt install libsasl2-modules
Configure SASL with SMTP Relay Credentials
-
Create the file
/etc/postfix/sasl/sasl_passwd
and add your SMTP server credentials:smtp.server.test:587 [email protected]:password
-
Generate a hash database file:
sudo postmap /etc/postfix/sasl/sasl_passwd
This will create a database file at
/etc/postfix/sasl/sasl_passwd.db
. -
Protect the credentials by setting the correct permissions:
sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
Update Postfix Configuration
Edit the Postfix configuration file /etc/postfix/main.cf
and update the following settings:
mydestination = $myhostname, localhost, localhost.localdomain
relayhost = [smtp.server.test]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
Add these lines to enable SASL authentication:
# Enable SASL authentication
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_tls_security_level = encrypt
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Restart Postfix
Apply the changes by restarting Postfix:
sudo systemctl restart postfix
Test if it’s working
You can test if the SASL authentication is working by running:
echo "This is a test email content" | mail -a "From: Test <[email protected]>" -s "Put the subject here" [email protected]
Note: If the recipient’s mail server is not secure enough, the sender can choose any email they want and spoof that email address. Report such security issues to the system owner to resolve them.
Conclusion
You have now set up email sending from the Linux command line using Postfix and Mailutils. For trusted delivery, configure Postfix to use a reliable SMTP relay service. This approach is powerful but should be used responsibly, especially to avoid unethical practices like email spoofing.