How to send a mail from shell script

You can send an email from a shell script using the mail command or sendmail command. Here are some examples:

Using the mail command:

mail -s "Subject" [email protected] << EOF
Body of the email
EOF

This will send an email to [email protected] with the subject "Subject" and the body "Body of the email".

Using the sendmail command:

sendmail -v [email protected] << EOF
Subject: Subject
Body of the email
EOF

This will also send an email to [email protected] with the subject "Subject" and the body "Body of the email".

Using mailx command:

mailx -s "Subject" [email protected] << EOF
Body of the email
EOF

This is similar to the mail command, but mailx is a more modern and feature-rich version.

Using mutt command:

mutt -s "Subject" [email protected] << EOF
Body of the email
EOF

This is another popular email client that can be used to send emails from a shell script.

Using ssmtp command:

ssmtp -v [email protected] << EOF
Subject: Subject
Body of the email
EOF

This is a simple SMTP client that can be used to send emails from a shell script.

Using mailutils command:

mail -s "Subject" [email protected] << EOF
Body of the email
EOF

This is a package that provides a set of command-line tools for sending and receiving email.

You can also use the echo command to send an email with a specific format:

echo "Subject: Subject" | mail -s "" [email protected]
echo "Body of the email" | mail -a "Content-Type: text/plain" [email protected]

This will send an email with the subject "Subject" and the body "Body of the email".

Make sure to replace [email protected] with the actual email address you want to send the email to. Also, make sure that the email address is valid and that the email server is configured correctly.

You can also use the mail command with the -c option to specify a configuration file:

mail -c /path/to/configfile [email protected] << EOF
Body of the email
EOF

This will use the configuration file specified in the -c option to send the email.

You can also use the mail command with the -a option to specify additional headers:

mail -a "From: [email protected]" -a "To: [email protected]" [email protected] << EOF
Body of the email
EOF

This will add the specified headers to the email.

You can also use the mail command with the -t option to specify a file containing the email:

mail -t /path/to/emailfile [email protected]

This will send the email contained in the file specified in the -t option.

Please note that the above examples are just a few examples of how you can send an email from a shell script. The actual command and options may vary depending on your system and email setup.