Send mail from bash script
You can send an email from a Bash script using the mail
command or sendmail
command. Here are some examples:
Using the mail
command:
#!/bin/bash
# Set the recipient's email address
TO="[email protected]"
# Set the subject of the email
SUBJECT="Subject of the email"
# Set the body of the email
BODY="This is the body of the email"
# Send the email
mail -s "$SUBJECT" "$TO" << EOF
$BODY
EOF
This will send an email to the recipient with the specified subject and body.
Using the sendmail
command:
#!/bin/bash
# Set the recipient's email address
TO="[email protected]"
# Set the subject of the email
SUBJECT="Subject of the email"
# Set the body of the email
BODY="This is the body of the email"
# Send the email
sendmail -t << EOF
To: $TO
Subject: $SUBJECT
$BODY
EOF
This will also send an email to the recipient with the specified subject and body.
Using a mail client like mutt
:
#!/bin/bash
# Set the recipient's email address
TO="[email protected]"
# Set the subject of the email
SUBJECT="Subject of the email"
# Set the body of the email
BODY="This is the body of the email"
# Send the email using mutt
mutt -s "$SUBJECT" "$TO" << EOF
$BODY
EOF
This will also send an email to the recipient with the specified subject and body.
Using a mail library like libmail
:
#!/bin/bash
# Set the recipient's email address
TO="[email protected]"
# Set the subject of the email
SUBJECT="Subject of the email"
# Set the body of the email
BODY="This is the body of the email"
# Send the email using libmail
libmail -s "$SUBJECT" "$TO" << EOF
$BODY
EOF
This will also send an email to the recipient with the specified subject and body.
Note: You may need to install the mail
or sendmail
command on your system, depending on your Linux distribution. Additionally, you may need to configure your email client or mail server to allow sending emails from your script.