How to create a script to mail from unix

To create a script to mail from a Unix system, you can use the mail command or the sendmail command. Here are some examples:

Using the mail command:

  1. Create a new file with a .sh extension (e.g., mail_script.sh) and add the following content:
    
    #!/bin/bash

Set the recipient's email address

TO_EMAIL="[email protected]"

Set the subject of the email

SUBJECT="Test email from Unix script"

Set the body of the email

BODY="This is a test email sent from a Unix script."

Use the mail command to send the email

mail -s "$SUBJECT" "$TO_EMAIL" << EOF $BODY EOF

2. Make the script executable by running the command `chmod +x mail_script.sh`.
3. Run the script by typing `./mail_script.sh`.

**Using the `sendmail` command:**

1. Create a new file with a `.sh` extension (e.g., `sendmail_script.sh`) and add the following content:
```bash
#!/bin/bash

# Set the recipient's email address
TO_EMAIL="[email protected]"

# Set the subject of the email
SUBJECT="Test email from Unix script"

# Set the body of the email
BODY="This is a test email sent from a Unix script."

# Use the sendmail command to send the email
sendmail -v -t << EOF
To: $TO_EMAIL
Subject: $SUBJECT

$BODY
EOF
  1. Make the script executable by running the command chmod +x sendmail_script.sh.
  2. Run the script by typing ./sendmail_script.sh.

Tips and Variations:

Remember to replace the [email protected] email address with the actual email address you want to send the email to.