Send E-mail by Bash Script using Fedora, Cent OS and/or Redhat
This is my guide for “How to send an e-mail using a bash script including an attachment…. In order to send an e-mail with an attachment from a bash script you will need to install the program MUTT, you can use YUM or RPM to install the program.
STEP 1 To install MUTT using YUM or UP2DATE run the following at your command line:
yum install mutt (Fedora and Cent OS method)
up2date mutt (Redhat)
STEP 2 Once installed you can start writing the script. The following is a simple example of how the line should look:
mutt -s “SUBJECT” -a /DIRECTORY_PATH [email protected] < /home/username/mail-message.txt
-s is the subject, where “SUBJECT” should be tittled accordingly
-a is the attachement, where “/DIRECTORY_PATH” should be the path to the file you need to attatch
“[email protected]” is the receiver, this is the person who will receive this e-mail
mail-message.txt is the file that is read into the message body in which the recipient will read
STEP 3 To put this all together in a script you would write the following:
(using my favorite text editor emacs I would type: emacs test_email.bsh, don’t forget to change the permissions, set them by typing: chmod +x test_email.bsh, if you don’t make the file executable it won’t run)
#!/bin/bash
mutt -s “this is a test” -a /home/username/log/test.log [email protected] < /home/username/mail-message.txt
The above script would send a message with a Subject of: “this is a test” and would include an attachment of the file: “test.log” and would be sent to” [email protected]
PLEASE NOTE that if you do not have MUTT installed you can send an e-mail using the following command, I am however unaware of a way to send an attachment using this method (this method uses sendmail which is usually installed by default on Linux systems):
echo “message body” | mail -s “subject” [email protected]
[ad#google-468×60]