The /usr/sbin/sendmail -bs command is used to interact with the Sendmail program (or a compatible Mail Transfer Agent like Postfix) in SMTP mode. This method is useful for scripts or applications that need to send email notifications programmatically while maintaining control over the SMTP conversation.

What Does -bs Do?

  • -bs: Enables SMTP (Simple Mail Transfer Protocol) mode.
  • Instead of feeding a message directly to Sendmail, the script communicates with it as if it were an SMTP server.
  • This allows the script to control the entire email transaction using SMTP commands like MAIL FROM, RCPT TO, DATA, etc.

Advantages of Using -bs

Flexibility

Provides granular control over the SMTP process.

Standardization

Compatible with SMTP-based workflows and scripts.

Interoperability

Works seamlessly with MTAs like Postfix, Exim, or Qmail.

Example: Sending Email Notifications Using a Shell Script

Script Example

This script uses /usr/sbin/sendmail -bs to send an email notification.

#!/bin/bash

# Variables
SMTP_SERVER="/usr/sbin/sendmail -bs"
FROM="sender@example.com"
TO="recipient@example.com"
SUBJECT="Server Notification"
BODY="This is a test email sent using /usr/sbin/sendmail -bs."

# Build the SMTP transaction
{
  echo "EHLO localhost"
  echo "MAIL FROM:<$FROM>"
  echo "RCPT TO:<$TO>"
  echo "DATA"
  echo "Subject: $SUBJECT"
  echo "To: $TO"
  echo ""
  echo "$BODY"
  echo "."
  echo "QUIT"
} | $SMTP_SERVER

Explanation of the Script

  1. SMTP Commands:
    • EHLO localhost: Identifies the client to the server.
    • MAIL FROM:<sender@example.com>: Specifies the sender.
    • RCPT TO:<recipient@example.com>: Specifies the recipient.
    • DATA: Marks the start of the email body.
    • .: Indicates the end of the email body.
    • QUIT: Ends the SMTP session.
  2. Pipeline to sendmail:

    The echo commands simulate an SMTP conversation, piped directly into /usr/sbin/sendmail -bs.

Advanced Example: Dynamic Notifications

For more dynamic notifications, you can modify the script to include variables and log outputs.

#!/bin/bash

# Email variables
SMTP_SERVER="/usr/sbin/sendmail -bs"
FROM="alerts@example.com"
TO="admin@example.com"
SUBJECT="[$(hostname)] Script Alert"
BODY="The scheduled task completed successfully at $(date)."

# Send email
{
  echo "EHLO localhost"
  echo "MAIL FROM:<$FROM>"
  echo "RCPT TO:<$TO>"
  echo "DATA"
  echo "Subject: $SUBJECT"
  echo "To: $TO"
  echo ""
  echo "$BODY"
  echo "."
  echo "QUIT"
} | $SMTP_SERVER

# Log the notification
echo "Email sent to $TO with subject '$SUBJECT' at $(date)" >> /var/log/email_notifications.log

Use Cases

Cron Job Alerts

Notify administrators of the success or failure of scheduled tasks.

Server Monitoring

Send alerts for CPU, memory, or disk usage thresholds.

Application Notifications

Use in applications to send automated emails (e.g., registration confirmations or password resets).

Troubleshooting

Issue Solution
Emails Not Sending Check the mail logs for errors:
  • Postfix: /var/log/mail.log
  • Exim: /var/log/exim_mainlog
Invalid Commands Ensure the SMTP conversation complies with the server's requirements (e.g., authentication, TLS).
Authentication Issues If your SMTP server requires authentication, consider using tools like msmtp or swaks for more secure workflows.

Alternative Tools for Email Notifications

1. mail Command

A simpler option for basic notifications.

Example:

echo "Task completed successfully" | mail -s "Task Alert" admin@example.com

2. msmtp or ssmtp

Lightweight alternatives for authenticated SMTP.

3. Python (smtplib)

Ideal for complex email workflows in Python scripts.

Conclusion

Using /usr/sbin/sendmail -bs provides fine-grained control over email notifications in scripts. It is ideal for environments where direct interaction with the SMTP protocol is required. For simpler use cases, consider alternatives like mailx or msmtp.