/usr/sbin/sendmail -t — Sending Email with File Attachment (Script Example)
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.
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
# 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
- Headers:
- The script sets
MIME-Version,Content-Type, andboundaryto format the email as MIME. boundaryseparates different parts of the email (body and attachments).
- The script sets
- Body:
- The plain text content is added under
Content-Type: text/plain.
- The plain text content is added under
- Attachment:
- The file is encoded in base64 to ensure it is sent properly over email.
- The
Content-Dispositionheader includes the filename.
- Sendmail:
- The formatted email is piped to
/usr/sbin/sendmail -tfor delivery.
- The formatted email is piped to
How to Use the Script
Create a text file (e.g., example.txt) to attach.
echo "This is the content of the file." > example.txt
Save the script to a file, e.g., send_email_with_attachment.sh.
chmod +x send_email_with_attachment.sh
./send_email_with_attachment.sh
Requirements
- Sendmail Installed:
Ensure sendmail is installed and configured on your system.
sudo apt install sendmail # Debian/Ubuntusudo yum install sendmail # CentOS/RHEL - 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
- Attachment Corruption:
- Ensure the file is properly encoded using
base64.
- Ensure the file is properly encoded using
- Email Not Delivered:
- Check the mail logs for issues:
tail -f /var/log/mail.log - Permission Issues:
- Ensure the script and attachment file have the correct read permissions.
- 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.


