Linux Task 8: Sending Emails from the Terminal in RHEL 9

Kartikkhatri
2 min readJul 17, 2023

Introduction:
The ability to send emails from the command line can be incredibly useful for automation, scripting, and server management tasks in Red Hat Enterprise Linux (RHEL) 9. In this guide, we will explore a simple method to send emails directly from the terminal using the `mailx` command-line utility, which is available in RHEL 9 by default. Let’s dive in and learn how to send emails effortlessly.

Step 1: Install the Required Packages
1. Open a terminal in your RHEL 9 system.

2. Ensure that the `mailx` package is installed by running the following command:

sudo dnf install mailx

3. If the package is already installed, you will see a message indicating that it is up to date. Otherwise, the installation will begin.

Step 2: Configure the SMTP Server
1. Open the configuration file for `mailx` using a text editor:

sudo vi /etc/mail.rc

2. Add the following lines at the end of the file, replacing `smtp.example.com` with the SMTP server address and `your-email@example.com` with your email address:

set smtp=smtp://smtp.example.com:25
set smtp-auth-user=your-email@example.com
set smtp-auth-password=your-email-password
set smtp-auth=login

3. Save the changes and exit the file.

Step 3: Compose and Send the Email
1. In the terminal, use the following command to start composing an email:

echo "Your message body" | mailx -s "Subject line" recipient@example.com

- Replace `”Your message body”` with the content of your email.
— Replace `”Subject line”` with the desired subject of your email.
— Replace `recipient@example.com` with the recipient’s email address.

2. Press Enter to send the email.

Step 4: Verifying Successful Delivery
1. After sending the email, check your email inbox to verify its successful delivery.

2. You can also check the `/var/log/maillog` file for any error messages or additional information regarding the email delivery process.

--

--