How to send mail in unix shell script
In a Unix shell script, you can send an email 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 the mail
command with attachments:
mail -s "Subject" [email protected] << EOF
Body of the email
Attachment: /path/to/attachment.txt
EOF
This will send an email to [email protected]
with the subject "Subject", the body "Body of the email", and an attachment /path/to/attachment.txt
.
Using the sendmail
command with attachments:
sendmail -v [email protected] << EOF
Subject: Subject
Body of the email
Content-Type: text/plain; name="attachment.txt"
Content-Disposition: attachment; filename="attachment.txt"
/path/to/attachment.txt
EOF
This will also send an email to [email protected]
with the subject "Subject", the body "Body of the email", and an attachment /path/to/attachment.txt
.
Using a third-party email client:
You can also use a third-party email client like mutt
or msmtp
to send emails from your Unix shell script.
For example, with mutt
:
mutt -s "Subject" [email protected] << EOF
Body of the email
EOF
And with msmtp
:
msmtp -v [email protected] << EOF
Subject: Subject
Body of the email
EOF
Make sure to configure your email client and set up your email account before using it in your shell script.
Note: The mail
and sendmail
commands are usually available on most Unix systems, but the syntax may vary depending on the specific implementation.