The command /usr/sbin/sendmail -t -i is used to send emails from the command line or within scripts using the sendmail program or compatible Mail Transfer Agents (MTAs) like Postfix or Exim.

Understanding the Command

/usr/sbin/sendmail

The executable path for the sendmail program or its compatible MTA.

-t

This flag tells sendmail to parse the headers (To, Cc, Bcc) from the message body for recipient information.

-i

This flag prevents sendmail from terminating the message input on a single line containing a dot (.).

Usage Example

Sending an Email

You can use this command to send an email by piping the email content into it.

Command:

echo -e "Subject: Test Email\nTo: recipient@example.com\n\nThis is a test email." | /usr/sbin/sendmail -t -i

Explanation:

  • Subject: Test Email sets the email subject line.
  • To: recipient@example.com specifies the recipient.
  • The body follows after an empty line.

Sending an Email with a File Attachment

To send an email with an attachment, you can use tools like mutt or mail combined with sendmail.

Script Example

Here is an example of using /usr/sbin/sendmail in a shell script:

#!/bin/bash

TO="recipient@example.com"
SUBJECT="Automated Email"
BODY="This is an email sent from a shell script."

# Build the email content
EMAIL="Subject: $SUBJECT\nTo: $TO\n\n$BODY"

# Send the email
echo -e "$EMAIL" | /usr/sbin/sendmail -t -i

Common Use Cases

Sending Email from Cron Jobs

Automate email notifications for task completion, errors, or logs.

Scripts and Applications

Use in PHP, Python, or Bash scripts for email functionality.

Email Testing

Quickly test email delivery functionality on a server.

Troubleshooting

Sendmail Not Found

  • Verify the location of the sendmail binary:
  • which sendmail
  • If not installed, install it:
  • On Debian/Ubuntu:
  • sudo apt install sendmail
  • On CentOS/RHEL:
  • sudo yum install sendmail

Emails Not Delivering

  • Check the mail log for errors:
  • tail -f /var/log/mail.log

SMTP Authentication Required

  • If your server requires authentication, consider using an SMTP client like msmtp or ssmtp instead.

The /usr/sbin/sendmail -t -i command provides a simple and effective way to send emails from scripts and command line on Unix-like systems.