The most portable solution is using mailx
(or its variants like bsd-mailx
or heirloom-mailx
), which comes pre-installed on most Unix-like systems:
echo "Body text" | mailx -s "Subject" -a file.txt recipient@example.com
For more advanced features, mutt
is a great choice:
mutt -s "Subject" -a file.txt -- recipient@example.com < body.txt
For systems with sendmail installed:
( echo "From: you@example.com" echo "To: recipient@example.com" echo "Subject: File attachment" echo "MIME-Version: 1.0" echo "Content-Type: multipart/mixed; boundary=\"MAIL_BOUNDARY\"" echo "" echo "--MAIL_BOUNDARY" echo "Content-Type: text/plain" echo "" echo "Email body text" echo "--MAIL_BOUNDARY" echo "Content-Type: application/octet-stream" echo "Content-Disposition: attachment; filename=\"file.txt\"" echo "" cat file.txt echo "--MAIL_BOUNDARY--" ) | sendmail -t
For systems with Python installed:
python -c 'import smtplib; from email.mime.text import MIMEText; from email.mime.multipart import MIMEMultipart; msg = MIMEMultipart(); msg["From"] = "you@example.com"; msg["To"] = "recipient@example.com"; msg["Subject"] = "Subject"; msg.attach(MIMEText("Body text")); msg.attach(MIMEText(open("file.txt").read())); smtplib.SMTP("localhost").sendmail("you@example.com", "recipient@example.com", msg.as_string())'
If you have ssmtp configured:
ssmtp recipient@example.com <Common issues to watch for:
- Attachment size limits (usually 10-25MB for most MTAs)
- SMTP server authentication requirements
- File permissions when reading attachments
- MIME type detection (specify explicitly if needed)
The traditional way to send email attachments via command line is using
mailx
withmutt
. Most Unix-like systems have these tools pre-installed:echo "File attached" | mutt -a /path/to/file.txt -s "Subject" recipient@example.comFor systems with sendmail configured, this method works well:
( echo "Subject: File Attachment" echo "MIME-Version: 1.0" echo "Content-Type: multipart/mixed; boundary=\"MAIL_BOUNDARY\"" echo "" echo "--MAIL_BOUNDARY" echo "Content-Type: text/plain" echo "" echo "Please find attached file." echo "" echo "--MAIL_BOUNDARY" echo "Content-Type: application/octet-stream" echo "Content-Disposition: attachment; filename=\"file.txt\"" echo "Content-Transfer-Encoding: base64" echo "" base64 /path/to/file.txt echo "--MAIL_BOUNDARY--" ) | sendmail recipient@example.comFor systems with Python installed (which is nearly all modern distributions), this script offers more control:
#!/usr/bin/env python3 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders msg = MIMEMultipart() msg['From'] = 'sender@example.com' msg['To'] = 'recipient@example.com' msg['Subject'] = 'File Attachment' body = "Please find attached file." msg.attach(MIMEText(body, 'plain')) attachment = open("/path/to/file.txt", "rb") part = MIMEBase('application', 'octet-stream') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename=file.txt") msg.attach(part) server = smtplib.SMTP('localhost') server.send_message(msg) server.quit()If you have access to an SMTP server with API (like Mailgun or SendGrid), curl works great:
curl -s --user 'api:YOUR_API_KEY' \ https://api.mailgun.net/v3/YOUR_DOMAIN/messages \ -F from='Sender' \ -F to=recipient@example.com \ -F subject='File Attachment' \ -F text='Please find attached file.' \ -F attachment=@/path/to/file.txt Some Linux distributions come with specialized tools:
Ubuntu/Debian:
sudo apt install sharutils uuencode /path/to/file.txt file.txt | mail -s "File Attachment" recipient@example.comRHEL/CentOS:
yum install mailx echo "File attached" | mail -s "Subject" -a /path/to/file.txt recipient@example.comConsider these factors when selecting an approach:
- Portability: Python and curl solutions work across most systems
- Dependencies: mailx/mutt may need installation on minimal systems
- Security: API-based methods (curl) avoid exposing credentials
- Complexity: Simple attachments vs. multiple files or HTML content