Email is a very common communication medium in today’s world now. Anyone can easily communicate or share any document with friends, family members, and colleagues by sending the email. Generally, people use any free email server like Gmail, Yahoo, Hotmail etc. for sending the email. But you can send email from command line easily in Linux operating system. There are many ways to send email in Linux. Some common and easy ways to send email in Ubuntu operating system from the terminal is shown in this tutorial.
Initialization:
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.
https://linuxhint.com/how-to-send-email-from-php/
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.
FromLineOverride=YES
root=admin@example.com
mailhub=smtp.gmail.com:587
AuthUser=username@gmail.com
AuthPass=password
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.
email.txt
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<[email protected]>’ and recipient email address, [email protected] and message body, ‘testing message’.
username@gmail.com <<< ‘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.
test.html
`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 protected]”
When you will check the inbox of recipient email account then the list of emails will be shown as the following image.
Conclusion
After practicing the above commands properly, hope the reader will be able to send email from the command line easily in Linux.
Credit: https://linuxhint.com/bash_script_send_email/