How to send an email using sendmail command?

2 min read 06-10-2024
How to send an email using sendmail command?


Sending Emails with the sendmail Command: A Comprehensive Guide

Sending emails from the command line might seem like a relic of the past, but the sendmail command remains a powerful and versatile tool for automating email communication. This guide will walk you through the basics of using sendmail and explore its capabilities beyond simple email transmission.

Understanding the sendmail Command

The sendmail command is a mail transfer agent (MTA) responsible for relaying emails between systems. It acts as a bridge between your email client and the actual email servers handling the delivery process. While often associated with Unix-based systems, sendmail is also available on Windows and other platforms.

The Basics of Sending an Email

Let's start with the simplest example: sending an email to a single recipient.

echo "This is the body of the email." | sendmail -t < [email protected]

This code snippet does the following:

  • echo "This is the body of the email.": Creates a simple email body. You can replace this with any text you want to send.
  • |: Pipes the output of the echo command to the sendmail command.
  • sendmail: Invokes the sendmail command.
  • -t: Tells sendmail to read recipient addresses from the To: header line.
  • < [email protected]: Specifies the email address of the recipient.

This command will send an email with the subject line "Subject: (empty)" and the specified body to the given email address.

Enhancing Your Emails

The sendmail command allows for greater flexibility and control over your email communication. Here are some ways to improve your emails:

  • Setting the Subject Line: You can set the subject line using the -s option:

    echo "This is the email body." | sendmail -s "Important Information" -t < [email protected]
    
  • Adding Attachments: While sendmail doesn't directly support attaching files, you can leverage utilities like uuencode to encode files and include them in the email body.

    uuencode myfile.txt myfile.txt | sendmail -t < [email protected]
    
  • Customizing Headers: You can modify other email headers using the -f option to set the sender address and -h to add custom headers.

    echo "Email body" | sendmail -f "[email protected]" -h "X-Custom-Header: My Value" -t < [email protected]
    
  • Sending to Multiple Recipients: Include multiple email addresses separated by commas in the To: header.

    echo "Email body" | sendmail -t < [email protected], [email protected]
    

Advanced Usage: Scripts and Automation

sendmail is often used in scripts for automating email notifications, reports, and other tasks. You can use it to send emails based on specific events, such as system updates, cron job completions, or file changes.

Example: Automated Script

#!/bin/bash

# Send an email when a file is modified
if [ "$1" == "modified" ]; then
  echo "The file $2 has been modified." | sendmail -t < [email protected]
fi

This script checks for the "modified" argument and, if present, sends an email notifying the recipient of the modified file name.

Conclusion

sendmail remains a powerful tool for sending emails from the command line. It provides a simple yet flexible approach to programmatically send emails, enabling you to automate notifications, create custom reports, and manage email communication efficiently. While graphical email clients offer a more user-friendly experience, sendmail empowers you to interact with email infrastructure directly, allowing for greater control and customization in your email workflow.

Resources