How to send an email using sendmail command in linux
Categories:
Mastering Email Sending with the Linux sendmail Command

Learn how to send emails directly from the command line on Linux using the powerful and versatile sendmail utility, covering basic usage, advanced options, and troubleshooting.
The sendmail command is a venerable and robust Mail Transfer Agent (MTA) that has been a cornerstone of email systems on Unix-like operating systems for decades. While modern email clients and services abstract away much of its complexity, understanding how to use sendmail directly from the command line is invaluable for system administrators, developers, and anyone needing to send automated notifications, system alerts, or simple text-based emails without a full-fledged email client. This article will guide you through the essentials of sending emails using sendmail.
Understanding sendmail Basics
sendmail acts as an interface to the underlying mail system. When you invoke sendmail, you're essentially instructing the MTA to process and deliver an email. It expects the email's headers and body to be provided via standard input (stdin). The most basic form involves specifying the recipient(s) as command-line arguments and then typing the email content, ending with a single dot (.) on a new line.
sendmail recipient@example.com
Subject: Test Email from Linux
From: your_user@your_server.com
Hello, this is a test email sent from the Linux command line using sendmail.
.
EOT
Basic sendmail command for sending a simple email.
From: header is crucial. If omitted, sendmail might use a default sender address, which could lead to emails being flagged as spam or rejected by recipient mail servers. Always specify a valid From: address.Sending Email with a Message Body from a File
For more complex emails or when sending automated messages, it's often more practical to prepare the email content (headers and body) in a file and then pipe that file's content to sendmail. This approach allows for easier management of email templates and dynamic content generation.
echo -e "Subject: Automated Report\nFrom: reports@your_server.com\n\nThis is your daily report.\n$(date)\n" > email_body.txt
sendmail recipient@example.com < email_body.txt
Creating an email body in a file and piping it to sendmail.
flowchart TD
A[Prepare Email Content (Headers + Body)] --> B{Pipe to sendmail}
B --> C[sendmail recipient@example.com]
C --> D[sendmail MTA processes email]
D --> E[Email delivered to recipient]Workflow for sending an email using sendmail with piped input.
Advanced sendmail Options and Troubleshooting
sendmail offers various command-line options to control its behavior. Understanding these can be helpful for debugging or specific use cases. Common options include -t for reading recipients from headers, -v for verbose output, and -i to ignore dots on lines by themselves in incoming messages (useful when piping content).
Common Issues and Solutions:
- Permission Denied: Ensure the user running
sendmailhas appropriate permissions to access thesendmailexecutable and its configuration files. - Mail Not Sent/Delayed: Check the mail logs (e.g.,
/var/log/maillogor/var/log/mail.log) for error messages. These logs provide crucial information about why an email might have failed or been delayed. - Spam Filtering: If emails are going to spam, verify your
From:address, ensure your server has proper SPF/DKIM records, and that the server's IP address isn't blacklisted. sendmailnot found: On some systems,sendmailmight be a symbolic link to another MTA (like Postfix or Exim). Ensure an MTA is installed and configured correctly.
sendmail -t -v < email_with_headers.txt
# Example email_with_headers.txt content:
# To: recipient@example.com
# Subject: Verbose Test
# From: sender@your_server.com
#
# This email uses the -t and -v options.
# .
Using sendmail with -t (read recipients from headers) and -v (verbose output).
sendmail for bulk email or sensitive applications without proper security configurations (like SPF, DKIM, and DMARC) can lead to emails being marked as spam or even server blacklisting. For production environments, consider using a dedicated email service or a more robust MTA configuration.1. Verify sendmail Installation
Open your terminal and type which sendmail. If it returns a path (e.g., /usr/sbin/sendmail), it's installed. If not, you may need to install an MTA (like Postfix, which often provides a sendmail compatible interface) using your distribution's package manager (e.g., sudo apt install postfix on Debian/Ubuntu or sudo yum install postfix on CentOS/RHEL).
2. Construct Your Email Content
Create a file (e.g., my_email.txt) containing the email headers and body. Remember to include Subject: and From: headers, followed by a blank line, then the message body. For example:
To: target@example.com
Subject: My First Command Line Email
From: me@mydomain.com
Hello from the command line!
This is a test message.
3. Send the Email
Execute the sendmail command, piping your email content file to it. Use the -t option to tell sendmail to read the To: header from the file. For example:
sendmail -t < my_email.txt
Alternatively, you can specify recipients directly:
sendmail target@example.com < my_email.txt
4. Check Mail Logs for Status
After sending, check your system's mail logs for delivery status or errors. Common log locations include /var/log/maillog, /var/log/mail.log, or /var/log/syslog. Look for entries related to sendmail or your MTA.