! /bin/bash
#Use: To Send Mail From Shell Script in Linux
#Prepared By: Online cyber Study Group
TO_ADDRESS=”testusr@yourdomain.com”
FROM_ADDRESS=”root@ yourdomain.com”
SUBJECT=”HOW TO SEND MAIL FROM SHELL SCRIPT”
BODY=” This is free webmail server we also called it open source mail server and we have write a bash script that have title HOW TO SEND MAIL FROM SHELL SCRIPT”
echo ${BODY MESSAGE}| mail -s ${SUBJECT } ${TO_ADDRESS} — -r ${FROM_ADDRESS}
:wq!
In above BASH SCRIPT I have used below four variable along with switch.
TO_ADDRESS = Write here user mail id which you want to send mail.
FROM_ADDRESS= Write sender user mail id
SUBJECT= Writer here subject of mail
BODY= Write here body message
-S = for Subject
-r = recipient address
How to send mail in shell script with attachment
#!/bin/bash
#Use: send mail in shell script with attachment
#Prepared By: Online cyber Study Group
TO_ADDRESS=”testusr@domain.com”
FROM_ADDRESS=”sender@yourdomain.com”
SUBJECT=”linux mail send attachment”
BODY_FILE=”server.dat”
ATTACHMENT_FILE=”serverlogfile.txt”
CC_LIST=”testuser2@gmail.com;testuser3@redhat.com;testuser3@onlinecyberstudy.com”
uuencode ${ATTACHMENT_FILE} | mail -s ${SUBJECT} -c ${CC_LIST} ${TO_ADDRESS} — -r ${FROM_ADDRESS} < ${BODY_FILE}
:wq!
CC_LIST variable you can put here group mail id or more user id which you want to send mail. –s option in mail command user to mail subject and –c option in mail command used to set more user mail in in CC ( carbon copy). Uuencode is used to send attachment file with mail command.
Recent Comments