How to Send Email in Linux from the Command Line with Sendmail and Mailx

Sendmail and Mailx

Sending emails from the Linux command line is a skill that boosts productivity for system administrators and developers. It allows for efficient system notifications, report automation, and log file distribution. This guide provides a step-by-step approach to using sendmail and mailx for email communication via the command line. You will learn to install and configure these tools on various Linux distributions, craft basic email commands, attach files, automate email tasks, and address security considerations. With practical examples and straightforward explanations, we aim to empower you with the knowledge to enhance your system management tasks through command-line email functionalities. 

Installing and Configuring Sendmail and Mailx in Linux

Email functionality from the command line in Linux is primarily facilitated by two tools: sendmail and mailx. Both serve important roles in sending emails, with sendmail acting as a mail transfer agent and mailx serving as a mail user agent. This section guides you through their installation and configuration on various Linux distributions, ensuring you are set up for seamless email communication. 

Sendmail Installation and Configuration

For Ubuntu/Debian: 

Update your system packages: 

sudo apt-get update

Install sendmail: 

sudo apt-get install sendmail

Configure sendmail with: 

sudo sendmailconfig

For CentOS/RHEL: 

Ensure your package database is up-to-date with: 

sudo yum update

Install sendmail by executing: 

sudo yum install sendmail sendmail-cf

Start and enable sendmail on boot with: 

sudo systemctl start sendmail
sudo systemctl enable sendmail

Mailx Installation

For Ubuntu/Debian: 

Install mailx by running:

sudo apt-get install mailutils

For CentOS/RHEL:

Use the command to install mailx to install mailx. 

sudo yum install

Basic Configuration of Mailx

After installation, a minimal configuration is needed to start sending emails. For sendmail, most setups work fine with the default configuration. However, for specific needs, such as using a relay server, you might need to edit the /etc/mail/sendmail.mc file and then regenerate the sendmail configuration with: 

sudo make -C /etc/mail

For mailx, configuration often involves setting up the mailrc file (~/.mailrc) with your email server settings. A simple mailrc configuration might look like this: 

set smtp=smtp://your.smtp.server 
set from="[email protected]" 
set smtp-auth-user=your_username 
set smtp-auth-password=your_password 
set smtp-auth=login

This setup allows you to start sending emails using the command line, leveraging sendmail for email routing and delivery, and mailx for composing and sending the emails. 

Sending Your First Email from the Linux Command Line 

Once you have sendmail and mailx installed and configured on your Linux system, sending emails from the command line becomes a simple task. This section will guide you through sending a basic email using both sendmail and mailx, catering to different preferences and requirements. 

Sending an Email with Sendmail

Sendmail allows for direct email sending through the command line. Although it is more commonly used as a mail transfer agent, you can also use it to send simple messages directly. Here is how: 

Create a file named email.txt that contains the following: 

Subject: Test Email via Sendmail 
 
This is the body of the email. Hello from the command line! 

Send the email using sendmail with the following command syntax: 

sendmail [email protected] < email.txt 

Replace [email protected] with the actual email address of your recipient. This method is straightforward but less commonly used for sending emails directly due to its simplicity and limitations compared to mailx. 

Sending an Email with Mailx

Mailx offers a more flexible and interactive approach to sending emails from the command line. Here is a basic example: 

echo "This is the body of the email" | mailx -s "Subject Here" [email protected] 

In this command: 

  • echo generates the email body. 
  • The output from echo is piped into mailx using |. 
  • -s is used to specify the subject of the email. 
  • Finally, you include the recipient’s email address. 

Example Command with Mailx: 

For sending an email to [email protected] with the subject “Test Email” and the message “Hello, this is a test email from the command line!”: 

echo "Hello, this is a test email from the command line!" | mailx -s "Test Email" [email protected] 

Attaching Files to an Email

Sending emails from the command line with attachments is an essential task for system administrators and developers, allowing the distribution of reports, documents, or log files with ease. While mailx offers a straightforward method for including attachments, it is important to note the limitations when considering sendmail for this purpose. 

Attaching Files with Mailx

To attach a file to an email using mailx, use the -a option followed by the path to the file. The command structure is as follows: 

echo "This is the body of the email" | mailx -a /path/to/file -s "Subject Here" [email protected] 

Example Command: 

Sending an email with an attached file named report.pdf: 

echo "Please find the attached report" | mailx -a ./report.pdf -s "Monthly Report" [email protected] 

This will send an email to [email protected] with the subject “Monthly Report,” containing a message and the report.pdf file attached. 

For Multiple Attachments: 

You can attach multiple files by repeating the -a option: 

echo "Please find the attached reports" | mailx -a ./report1.pdf -a ./report2.csv -s "Monthly Reports" [email protected]

Limitations with Sendmail 

When it comes to sendmail, attaching files directly through the command line is not inherently supported. Sendmail itself is a mail transfer agent (MTA) focused on the routing and delivery of email, rather than the composition of messages or inclusion of attachments. To send attachments via sendmail, you would typically need to construct a properly formatted email message that includes MIME (Multipurpose Internet Mail Extensions) encoded attachments, which can be cumbersome and is less straightforward than using mailx. 

For users needing to send emails with attachments from the command line, mailx (or similar tools that facilitate MIME encoding) is recommended for its ease of use and direct support for attachments. 

Automating Email Sending in Linux

Automating email dispatch from the command line is an important efficiency booster for system administrators and developers. It aids in the seamless execution of system alerts, report dissemination, and log file distribution. While we have covered using mailx for its simplicity, incorporating sendmail into automation scripts is also viable and provides a robust alternative for those who prefer or require its use. Here, we will explore automation examples using both sendmail and mailx. 

Basic Automation with Bash Scripts Using Mailx

Creating a simple bash script can facilitate the automated sending of emails. Below is a straightforward example demonstrating this with mailx: 

#!/bin/bash 
 
# Define recipient, subject, and body 
recipient="[email protected]" 
subject="System Update Notification" 
body="The system update completed successfully." 
 
# Sending email 
echo "$body" | mailx -s "$subject" $recipient 

This script can be manually executed or scheduled as a cron job for automated tasks. 

Incorporating Attachments with Mailx 

To automate sending an email with attachments using mailx, append the -a option: 

#!/bin/bash 
 
# Attachment path 
attachment="/path/to/report.pdf" 
 
# Other variables as previously defined 
 
# Send email with attachment 
echo "$body" | mailx -a "$attachment" -s "$subject" $recipient 

Basic Automation with Bash Scripts Using Sendmail 

Sendmail can also be employed for email automation, although it involves a slightly different approach, especially when dealing with MIME types for attachments. Here is an example that sends a plain text email: 

#!/bin/bash 
 
# Define recipient and subject 
recipient="[email protected]" 
subject="System Update Notification" 
body="The system update completed successfully." 
 
# Construct email headers and body 
( 
echo "To: $recipient" 
echo "Subject: $subject" 
echo "Content-Type: text/plain" 
echo 
echo "$body" 
) | sendmail -t 

This script forms the basic structure of an email and sends it via sendmail. 

Scheduling Emails with Cron 

Scheduling automated emails, whether through sendmail or mailx, can be accomplished with cron jobs. To schedule your script: 

  1. Edit your cron table with crontab -e. 
  1. Add a schedule entry, for example: 
0 8 * * * /path/to/your_script.sh

This sets the script to run daily at 8:00 AM server time. 

Security Considerations for Sending Emails from the Linux Command Line

When utilizing the command line for email sending, prioritizing security is important. Here is a condensed guide to keeping your email interactions secure: 

Security PracticeDescription
Secure Connections  Use SMTP over SSL/TLS to protect your emails during transmission.  
Authentication and Access Controls Employ secure authentication methods and restrict access to scripts and configuration files containing sensitive details. Avoid storing passwords in plain text.  
Sanitize Input  Ensure thorough sanitization of external inputs for scripts to prevent exploitation.  
Activity Monitoring  Keep an eye on email sending activities and maintain logs for auditing and identifying anomalies.  
Updates and Patches  Regularly update your email-related software to safeguard against vulnerabilities.  
Attachment Security  Scan attachments for malware to prevent spreading harmful software.  
Sensitive Information  Exercise caution when sending sensitive data. Use encryption or secure file transfer methods for highly confidential information.  

Conclusion

This guide has equipped you with the essentials for sending emails from the Linux command line, showcasing sendmail and mailx setup, basic usage, file attachments, and automation. We emphasized the importance of security, from secure connections to handling sensitive data cautiously. By mastering these skills, system administrators and developers can optimize their workflow and maintain robust security protocols. Embrace these practices to streamline your email tasks, ensuring secure and effective communication within your Linux environment. 

Scroll to Top