Sending Files as Email Attachments from the Linux Command Line

Sending Files as Email Attachments from the Linux Command Line

Often, system administrators and developers need to automate the sending of files—like log files, backups, or reports—via email from a Linux server. While sending the file contents directly in the email body is possible, it’s often impractical for larger files, leading to readability issues and potential email size limitations. This tutorial demonstrates how to send files as attachments using command-line tools in Linux.

Methods for Sending Attachments

Several methods can achieve this, each with its own strengths and dependencies. We’ll cover the most common approaches using mutt, mail, and sendemail.

1. Using mutt

mutt is a powerful email client that offers robust attachment capabilities directly from the command line. If mutt is installed on your system, it’s often the simplest solution.

echo "This is the message body" | mutt -a /path/to/file.to.attach -s "Subject of message" [email protected]

Let’s break down the command:

  • echo "This is the message body": Provides the body of the email. You can replace this with a file containing the message using input redirection (e.g., cat message.txt | mutt ...).
  • -a /path/to/file.to.attach: Specifies the file to attach. You can attach multiple files by including multiple -a options.
  • -s "Subject of message": Sets the subject of the email.
  • [email protected]: Specifies the recipient’s email address.

To see all available options for mutt, run mutt -h.

2. Using mail (with uuencode)

The mail utility is a standard command-line email program available on most Linux distributions. However, mail doesn’t natively support attachments. We can use uuencode to encode the file in a format suitable for inclusion in the email body.

gzip -c mysqldbbackup.sql | uuencode mysqldbbackup.sql.gz  | mail -s "MySQL DB Backup" [email protected]

Here’s what each part does:

  • gzip -c mysqldbbackup.sql: Compresses the mysqldbbackup.sql file and sends the compressed output to standard output. This reduces the file size.
  • uuencode mysqldbbackup.sql.gz: Encodes the compressed file in a format that can be safely included in an email. The mysqldbbackup.sql.gz part is the encoded filename; it’s not an actual file that’s created.
  • mail -s "MySQL DB Backup" [email protected]: Sends the encoded data as an email with the specified subject and recipient.

3. Using sendemail

sendemail is a versatile command-line tool specifically designed for sending emails with attachments. It’s not always installed by default, so you might need to install it using your distribution’s package manager (e.g., apt-get install sendemail on Debian/Ubuntu).

sendemail -f [email protected] -t [email protected] -m "Here are your files!" -a file1.jpg file2.zip
  • -f [email protected]: Sets the sender’s email address.
  • -t [email protected]: Specifies the recipient’s email address.
  • -m "Here are your files!": Sets the message body.
  • -a file1.jpg file2.zip: Lists the files to attach. You can specify multiple -a options for each file.

sendemail offers many advanced options for configuring SMTP servers, authentication, and more. Consult the sendemail documentation for a complete list of features.

Choosing the Right Method

  • mutt: The most straightforward option if mutt is already installed. It’s easy to use and offers a good balance of features.
  • mail + uuencode: A useful workaround if mutt isn’t available, but requires compression and encoding steps.
  • sendemail: The most flexible option, especially for complex configurations or when you need fine-grained control over SMTP settings. However, it requires installation and configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *