UNIX / Linux: Shell Scripting With mail Command

The mail command in Unix/Linux can be used to send emails from the command line. Here’s an example of how to use the mail command in a shell script:

#!/bin/bash

to="recipient@example.com"
subject="Test Email"
message="This is a test email sent from the shell script."

echo "$message" | mail -s "$subject" "$to"

In the example above, the to variable contains the email address of the recipient, the subject variable contains the subject of the email, and the message variable contains the message body.

The echo command is used to send the message to the standard input of the mail command. The -s option is used to specify the subject of the email, and the recipient email address is specified as the last argument.

When you run the script, it will send an email to the specified recipient with the specified subject and message. You can customize this script to send emails with different subjects, messages, and recipients based on your requirements.

Note: The mail command is not available on all Unix/Linux systems. If you get an error that the mail command is not found, you may need to install the mailutils package using your system’s package manager.

Leave a Comment