The command /usr/sbin/sendmail -t -i is used in Unix-like systems to send emails from the command line or within scripts. It interacts directly with the Sendmail program (or compatible alternatives like Postfix, Exim, or Qmail) to process and deliver email messages.

Breaking Down the Command

/usr/sbin/sendmail

This is the path to the Sendmail binary, which is responsible for processing and routing email.

-t

This flag tells Sendmail to read the email recipients (To, Cc, Bcc) directly from the message headers instead of specifying them on the command line.

Example:

To: user@example.com
Cc: another@example.com

Useful when email content is passed as a file or piped input.

-i

This flag prevents Sendmail from treating a single dot (.) on a line as the end of the email message.

This is important when the email body contains lines with just a dot (e.g., a file with such content), avoiding premature message termination.

How to Use /usr/sbin/sendmail -t -i

Sending an Email from the Command Line

You can use echo or cat to create the email content and pipe it into Sendmail:

echo -e "To: recipient@example.com\nSubject: Test Email\n\nThis is the body of the email." | /usr/sbin/sendmail -t -i

Using a File as Email Content

If you have an email content file (email.txt), you can send it using:

File (email.txt):

To: recipient@example.com
Subject: Email Subject

This is the email body.

Command:

/usr/sbin/sendmail -t -i < email.txt

Common Use Cases

Email Notifications in Scripts

Automatically send alerts or logs via email from a script.

Example in a shell script:

#!/bin/bash
echo -e "To: admin@example.com\nSubject: Backup Completed\n\nBackup finished successfully at \$(date)." | /usr/sbin/sendmail -t -i

Sending Logs or Reports

Combine with cron jobs to send system reports or logs via email.

Integration with PHP

PHP’s mail() function often relies on Sendmail:

mail("recipient@example.com", "Subject", "Message Body", "From: sender@example.com");

Ensure that Sendmail is properly configured on the server.

Troubleshooting Common Issues

Issue Solution
Sendmail Not Found
  • Verify the path to Sendmail: which sendmail
  • If not installed, install a compatible MTA like Postfix or Exim.
Emails Not Sending
  • Check the mail queue: mailq
  • Inspect mail logs for errors:
    • /var/log/mail.log (Postfix)
    • /var/log/exim_mainlog (Exim)
Emails Marked as Spam Ensure proper DNS records (SPF, DKIM, and DMARC) are configured for your domain.

Alternatives to Sendmail

Postfix

A modern MTA, often a drop-in replacement for Sendmail.

Exim

Another popular MTA with flexible configuration.

SSMTP

A lightweight alternative for simple email sending.

msmtp

Great for sending mail via external SMTP servers (e.g., Gmail).