Many Linux command is available to send email from the command line or by using a bash script. But any email sending command will not work if no SMTP server is set up properly in the system. You can setup your own SMTP server to send email from the command line or you can use any free SMTP server of any well-known email service provider like Gmail or Yahoo. SMTP server of a Gmail account is used in this tutorial. To do this task, you have to select a Gmail account and enable the option of ‘Allow less secure apps’ for that account to send the email. You can visit the following tutorial to enable this option.
After enabling this option, you have to open the file ‘ /etc/ssmtp/ssmtp.conf’ with ‘root’ privilege and add the following lines at the end of the file. You have to set your email address to ‘AuthUser’ and your email password to ‘AuthPass’ to complete the setup.
Example-1: Using Sendmail Command
One of the popular email sending commands of Linux is `sendmail`. The user can easily send email from the command line by using this command. If Sendmail package is not installed in the system then run the following command to install the package.
Suppose, the email content is stored in a file named ‘email.txt’ with the following content. Here, the text after the ‘Subject:’ will be sent as email subject and the remaining part will be sent as email body.
Testing email body
Run `sendmail’ command with recipient email address like the following command.
Example-2: Using ‘mail’ Command
The most common command for sending email in Linux is `mail` command. This command is not installed on Ubuntu by default. Run the following command to install `mail` command.
The following command will show the version of this command if it is installed in the system.
‘-s’ option is used in the `mail` command to define the subject of the email. Run `mail’ command by ‘-s’ option with email subject and the recipient email address like the following command. It will ask for Cc: address. If you don’t want to use Cc: field then keep it blank and press enter. Type the message body and press Ctrl+D to send the email.
If you want to add email message body in the command then use ‘<<<’ operator like the following command.
You can also add the sender email address with `mail` command by using the ‘-a’ option. The following command will send the email with the subject, ‘message subject’, sender’s name and email address, ‘Admin<firstname.lastname@example.org>’ and recipient email address, email@example.com and message body, ‘testing message’.
firstname.lastname@example.org <<< 'testing message'
You can also send an email message body by using echo and pipe( | ) command. The following command uses the pipe (|) command to send the message body.
Email can be sent to multiple recipients by using `mail` command and adding the recipients’ addresses by separating comma. The following command will be sent an email to two recipients.
Example-3: Using `mutt` command
Like `mail` command, `mutt` command is not installed on Ubuntu by default. Run the following command to install `mutt` command.
`mutt` command works similar to `mail` command but `mutt` command has own editor to send the email. Run the following command with the valid email address to send the email.
You can attach any file using the ‘-a’ option with `mail` or `mutt` command. The following command will attach the file items.txt while sending the email.
Example-4: Using ‘SSMTP’ Command
Another email command is `ssmtp` command. If ssmtp is not installed before then install the package first. The installation command for ssmtp is shown in the example-1. ssmtp and sendmail commands work similarly.
Run the following command with the valid email address. Type the email subject and body. Press ctrl+D to exit and send the email.
Example-5: Using mailx Command
You can send HTML content as email body without just sending the text message. Create an HTML file named ‘test.html’ to use as a message body.
`mailx` command works like `mail` command. You have to mention Content-Type as text/html to send HTML content as the message body in `mailx` command. The following command will send the content of ‘test.html' file as the email message body.
< test.html "email@example.com"
When you will check the inbox of recipient email account then the list of emails will be shown as the following image.
After practicing the above commands properly, hope the reader will be able to send email from the command line easily in Linux.