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.
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
# 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, and boundary to format the email as MIME.
- boundary separates different parts of the email (body and attachments).
- Body:
- The plain text content is added under Content-Type: text/plain.
- Attachment:
- The file is encoded in base64 to ensure it is sent properly over email.
- The Content-Disposition header includes the filename.
- Sendmail:
- The formatted email is piped to /usr/sbin/sendmail -t for delivery.
How to Use the Script
1 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
2 Save the Script:
Save the script to a file, e.g., send_email_with_attachment.sh.
3 Make the Script Executable:
chmod +x send_email_with_attachment.sh
4 Run the Script:
./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.
- 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.


