Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Using /usr/sbin/sendmail -t to Send an Email from the Command Line

2 min read
12.10.2025

The /usr/sbin/sendmail -t command allows you to send emails directly from the command line on Unix-like systems. It reads the recipients from the email headers (e.g., To, Cc, Bcc) within the email content. This method is commonly used for automation in scripts or cron jobs.

sendmail -t Command Line
sendmail -t CLI — quick scripts; mutt for more polished use.

For closely related sendmail / script-mail topics, see Using /usr/sbin/sendmail -bs, /usr/sbin/sendmail -t -i, What is /usr/sbin/sendmail -t -i?, and sendmail -fcrondaemon — cron variant.

Steps to Send an Email with /usr/sbin/sendmail -t

Create the Email Content

The email content must include the headers (e.g., To, Subject) followed by the message body.

Example file (email.txt):

Use the sendmail Command

Run the following command to send the email:

/usr/sbin/sendmail -t < email.txt
  • -t: Instructs sendmail to read the recipient addresses (To, Cc, Bcc) from the email headers.
  • < email.txt: Feeds the email content to the command.
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Example: Combining with echo and heredoc

Using echo:

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

Using a Heredoc:

/usr/sbin/sendmail -t <

Use Cases

Automation

Ideal for sending system alerts or logs in scripts.

Example:

echo -e "To: admin@example.com\nSubject: Backup Completed\n\nThe backup was successful." | /usr/sbin/sendmail -t

Cron Jobs

Send output or error notifications from scheduled tasks.

Example cron entry:

0 2 * * * /path/to/script.sh | /usr/sbin/sendmail -t

Testing Email Server Configuration

Useful for verifying if sendmail is properly configured.

Troubleshooting Common Issues

Issue Solution
Command Not Found
  • Verify if sendmail is installed: which sendmail
  • If not installed, install a compatible Mail Transfer Agent (MTA), such as Postfix:
    sudo apt install postfix
Emails Not Delivered
  • Check the mail queue for stuck messages: mailq
  • Examine the mail logs:
    • Postfix: sudo tail -f /var/log/mail.log
    • Exim: sudo tail -f /var/log/exim_mainlog
Emails Marked as Spam Ensure the sender domain has correct DNS records:
  • SPF: Authorizes the server to send emails.
  • DKIM: Digitally signs emails.
  • DMARC: Defines email handling policies.
Debugging the Sendmail Command Use verbose mode to see detailed output:
echo -e "To: recipient@example.com\nSubject: Test Email\n\nBody content" | /usr/sbin/sendmail -t -v

Enhancements and Alternatives

  1. Use a Lightweight Mail Client:

    Instead of sendmail, consider using mailx or msmtp for simpler email sending:

    echo "This is a test email" | mail -s "Subject" recipient@example.com
  2. Send Emails via SMTP:

    Use tools like Python?s smtplib or a service like SendGrid for more control and security.

Frequently asked questions
Heredoc for multi-line messages with embedded headers: `sendmail -t <
Cron's environment differs from interactive shell. PATH limited, no TTY, no working dir. sendmail might not be in cron's PATH. Use full path `/usr/sbin/sendmail` in cron scripts. Or set PATH in crontab (`PATH=/usr/sbin:/usr/bin:/bin`).
Manual MIME assembly with sendmail -t is painful. Use mutt: `mutt -a /path/file.pdf -s 'Subject' -- user@example.com < body.txt`. Handles MIME, encoding, boundary. Or PHP's PHPMailer for app-driven mail. Raw sendmail -t is for plain-text only.
If you pipe user input into sendmail, attackers can inject CRLF + new headers (Bcc, fake From). Validate/sanitize input strictly. PHP's mail() with sendmail -t backend is vulnerable to header injection if you put user data in headers. Use a proper mail library which handles this.
Related articles
What is /usr/sbin/sendmail -t -i?
Understanding /usr/sbin/sendmail -fcrondaemon -i -odi -oem -oi -t -f root
/usr/sbin/sendmail -t -i