How to Send an E-mail from CentOS 7: A Step-by-Step Guide

How to Send an E-mail from CentOS 7: A Step-by-Step Guide


Table of Contents


Introduction:

Sending an email from a CentOS 7 server might seem like a daunting task, especially if you’re new to the Linux operating system. However, this step-by-step guide will walk you through the process, making it easy for you to set up email functionality on your server using Postfix, a popular mail transfer agent.

Prerequisites:

  • CentOS 7 server with root access
  • A valid email account to use for sending emails

Step 1: Update Your System

Before we begin, it’s important to ensure your system is up-to-date. Open a terminal and run the following command:

sudo yum update -y

Step 2: Install Postfix and Mailx

In this tutorial, we’ll use Postfix to handle email delivery and ‘mailx’ as a command-line utility for sending emails. To install packages, run:

sudo yum install -y postfix cyrus-sasl-plain mailx

Step 3: Configure Postfix

In this example, we’ll use Gmail’s SMTP server to relay emails. Replace youremail@gmail.com and yourpassword with your Gmail email address and password, respectively.

Create or edit the /etc/postfix/main.cf file using your preferred text editor, such as ‘nano’ or ‘vi’:

sudo nano /etc/postfix/main.cf

Add the following lines to the file, replacing the placeholders with your own email account details:

relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt

Save and exit the file.

Next, create the /etc/postfix/sasl_passwd file and add the Gmail SMTP server along with your email account credentials:

sudo nano /etc/postfix/sasl_passwd

Add the following line, replacing the placeholders with your own email account details:

[smtp.gmail.com]:587 youremail@gmail.com:yourpassword

Save and exit the file.

Note: Storing your email credentials in plain text poses a security risk. Consider using a dedicated email account for this purpose, or explore alternative methods of securing your credentials.

Step 4: Secure the Credentials and Update Postfix

Set the appropriate ownership and permissions for the /etc/postfix/sasl_passwd file, and then create a hashed version of the file for Postfix to use:

sudo chown root:root /etc/postfix/sasl_passwd
sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Now, restart and enable Postfix to apply the configuration changes:

sudo systemctl restart postfix
sudo systemctl enable postfix

Step 5: Test Your Email Configuration

Now it’s time to test your email configuration by sending a test email. Replace recipient@example.com with the recipient’s email address and run the following command:

echo "This is a test email" | mailx -s "Test Email Subject" recipient@example.com

If everything is configured correctly, the recipient should receive the test email.

Conclusion:

Congratulations! You have successfully set up email functionality on your CentOS 7 server using Postfix and Mailx. Now you can easily send emails from your server for various purposes, such as notifications, alerts, or reports.

© 2024 Virendra Giri