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

/usr/sbin/sendmail -t — Sending Email with File Attachment (Script Example)

4 min read
23.03.2025

To send an email with a file attachment using /usr/sbin/sendmail -t, you need to construct a MIME-formatted email. The MIME format allows you to include attachments along with text content.

Sendmail Attachment Script
Sendmail -t + MIME — works, but mutt or PHPMailer is cleaner.

For closely related sendmail / script-mail topics, see Using /usr/sbin/sendmail -bs for Email Notifications, /usr/sbin/sendmail -t -i, and Understanding sendmail -fcrondaemon.

Script to Send an Email with an Attachment

#!/bin/bash

# Variables
TO="recipient@example.com"
SUBJECT="Email with Attachment"
BODY="Hello,\n\nPlease find the attached file.\n\nBest regards,\nYour Script"
ATTACHMENT="example.txt" # Path to the file you want to attach

# Create a unique boundary string
BOUNDARY="====MULTIPART_BOUNDARY_$(date +%s)===="

# Create the email headers
{
    echo "To: $TO"
    echo "Subject: $SUBJECT"
    echo "MIME-Version: 1.0"
    echo "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\""
    echo
    echo "This is a multi-part message in MIME format."
    echo
    # Add the email body
    echo "--$BOUNDARY"
    echo "Content-Type: text/plain; charset=\"utf-8\""
    echo "Content-Transfer-Encoding: 7bit"
    echo
    echo -e "$BODY"
    echo
    # Add the attachment
    echo "--$BOUNDARY"
    echo "Content-Type: $(file --mime-type -b "$ATTACHMENT")"
    echo "Content-Transfer-Encoding: base64"
    echo "Content-Disposition: attachment; filename=\"$(basename "$ATTACHMENT")\""
    echo
    base64 "$ATTACHMENT"
    echo
    # End the email
    echo "--$BOUNDARY--"
} | /usr/sbin/sendmail -t

Explanation

  1. Headers:
    • The script sets MIME-Version, Content-Type, and boundary to format the email as MIME.
    • boundary separates different parts of the email (body and attachments).
  2. Body:
    • The plain text content is added under Content-Type: text/plain.
  3. Attachment:
    • The file is encoded in base64 to ensure it is sent properly over email.
    • The Content-Disposition header includes the filename.
  4. Sendmail:
    • The formatted email is piped to /usr/sbin/sendmail -t for delivery.
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

How to Use the Script

Create the File to Attach:

Create a text file (e.g., example.txt) to attach.

echo "This is the content of the file." > example.txt
Save the Script:

Save the script to a file, e.g., send_email_with_attachment.sh.

Make the Script Executable:
chmod +x send_email_with_attachment.sh
Run the Script:
./send_email_with_attachment.sh

Requirements

  1. Sendmail Installed:

    Ensure sendmail is installed and configured on your system.

    sudo apt install sendmail  # Debian/Ubuntu
    sudo yum install sendmail  # CentOS/RHEL
  2. Valid Mail Configuration:

    Check that your server can send emails using sendmail. Test with a simple email:

    echo "Test email" | /usr/sbin/sendmail -t recipient@example.com

Troubleshooting

  1. Attachment Corruption:
    • Ensure the file is properly encoded using base64.
  2. Email Not Delivered:
    • Check the mail logs for issues:
    • tail -f /var/log/mail.log
  3. Permission Issues:
    • Ensure the script and attachment file have the correct read permissions.
  4. Email Marked as Spam:
    • Configure SPF, DKIM, and DMARC for your email domain.

This script demonstrates how to send an email with a file attachment using /usr/sbin/sendmail -t by properly formatting the email with MIME types and base64 encoding.

Frequently asked questions
MIME boundary mismatched or missing base64 encoding. Check headers: `Content-Type: multipart/mixed; boundary="XYZ"`, then `--XYZ` on a line by itself between parts. Attachment part needs `Content-Transfer-Encoding: base64` + base64-encoded content. Wrong encoding = garbled.
MIME requires CRLF (\r\n). Some sendmail implementations auto-convert LF; others don't. If receiver sees one big line or run-together content, missing CRLF. Use printf with explicit \r\n or pipe through `unix2dos`. For PHP: `\r\n` in strings, not just `\n`.
`mutt -a attachment.pdf -s 'Subject' -- recipient@example.com < body.txt` — handles MIME boundary and encoding for you. Far cleaner than DIY sendmail script. If mutt installed, prefer it. Raw sendmail useful only when mutt isn't available or you need very specific MIME structure.
PHPMailer or Symfony Mailer library. Handles MIME, encoding, SMTP auth, TLS — abstracts away the gotchas. `mail()` with attachment requires manually-constructed MIME body, fragile. For non-trivial mail in PHP, PHPMailer is the standard. Composer install + 10 lines vs 100 lines of MIME assembly.
Related articles
Using /usr/sbin/sendmail -bs for Email Notifications in Scripts
"EHLO not accepted from server!" — Complete SMTP Error Fix
Exim Syntax Errors: Causes and Fixes