Send Email with attachment from the command line in Ubuntu Linux

Sending emails with attachments from the command line in Ubuntu is easily accomplished using a tool called mutt. Mutt allows us to send emails non-interactively. That is, we can invoke mutt from a shell script and it will perform the specified tasks for us with no further intervention needed on our part.

We can also run mutt from a shell (such as bash) or even as a cron job and have it carry out our desired tasks.

Send Email with attachment in Ubuntu - Install mutt

We first need to install the utility called mutt. This is the piece of software which does all the hard work for us. In a terminal run the following as the superuser to install the required software:

apt-get install mutt

If you are using some other flavour of Linux, go ahead and install mutt the same way you would install any other software from the command line.

After the installation has completed successfully, we can go ahead and start sending emails (with attachments if necessary) from the command line in Ubuntu or any other version of Linux that we are running.

How to Send Email with attachment in Ubuntu using mutt

Below is an example of sending an email with an attachment in Ubuntu using mutt. Open up a terminal and execute code similar to the one below:

mutt -s "Subject" -a attachment -- example@tutorialarena.com < message

In the above code, note that:

  • attachment - This is the path to the file which is to be sent as the attachment
  • message - This is the path to the file which contains the message to be sent

These paths can be absolute paths or paths relative to the current directory. So if the attachment was located at /tmp/logs, and the message to be sent is in the current directory in a file called msg, and the subject of the message is Backup we could send the email like this:

mutt -s "Backup" -a /tmp/logs -- webmaster@tutorialarena.com < ./msg

Believe it or not, that's all there is to sending an email with an attachment from the terminal in Ubuntu. The handy piece of software called mutt does all the hard work for us.

You can now go ahead and write your shell scripts to invoke this handy utility. You can even use this technique with cron to make backups of your server and have your server email them to you.

For more information, visit the Mutt E-Mail Client Homepage.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...