Bash script mail
Here is an example of a simple bash script that sends an email using the mail
command:
#!/bin/bash
# Set the recipient's email address
RECIPIENT="[email protected]"
# Set the subject of the email
SUBJECT="Test email from bash script"
# Set the body of the email
BODY="This is a test email sent from a bash script."
# Use the mail command to send the email
echo "To: $RECIPIENT" | mail -s "$SUBJECT" $RECIPIENT
Let me explain what each line does:
#!/bin/bash
: This line specifies the interpreter that should be used to run the script. In this case, it's bash.RECIPIENT="[email protected]"
: This line sets the recipient's email address to[email protected]
.SUBJECT="Test email from bash script"
: This line sets the subject of the email toTest email from bash script
.BODY="This is a test email sent from a bash script."
: This line sets the body of the email toThis is a test email sent from a bash script.
.echo "To: $RECIPIENT" | mail -s "$SUBJECT" $RECIPIENT
: This line uses theecho
command to print the email headers (including the recipient's address) and themail
command to send the email. The-s
option specifies the subject of the email, and the$RECIPIENT
variable is used to specify the recipient's email address.
To use this script, save it to a file (e.g., send_email.sh
), make the file executable with chmod +x send_email.sh
, and then run it with ./send_email.sh
.
Note: This script assumes that you have the mail
command installed and configured on your system. If you don't have mail
installed, you may need to use a different email client or service to send the email.