To send a simple mail from command, follow the steps below:
====
root@newserver [~]# mail user@domainname.com
Subject: test mail
test mail from support
.
EOT
====
Use a "." dot to end the mail.
Note: Use "mail -v user@domainname.com" to get verbose output.
Now, the important point that we have to see is how this is helpful while writing a shell script.
=====
echo "This is the message body" | mail -s "This is the subject" user@domainname.com
====
The above command is very useful while writing shell scripts to send mails.
Now, we can also utilize this in other way. By adding the content of a file as body to the mail.
====
mail -s "Hello World" user@domain.com < /home/user/mailcontent.txt
====
In the above example the contents of the file will be the body of the mail.
Other useful parameters in the mail command are:
-s subject (The subject of the mail)
-c email-address (CC - send a carbon copy to email-address)
-b email-address (BCC - send a blind carbon copy to email-address)
====
mail -s "Hello World" user@domain.com -c usertocc@domain.com -b usertobcc@domain.com
===
It has always been a question how to specify multiple recipients while sending mail, this can be done by joining them with a comma.
===
mail -s "Hello" user1@domain.com,user2@domain.com
===
Setting the "FROM" address during sending mails from command line is easy but a little bit tricky.
The trick is : after the mail you want to send to, you add
(double dash) (space) (single dash) (NO space) f
===
mail -s "Hello World" user@domain.com -- -f from_user@domain.com
====
However, the above will work on centos, but not on debian or ubuntu. On ubuntu/debian system an alternative syntax has to be used
====
echo "This is the message body" | mail -s "This is the subject" user@domain.com -aFrom:blog@maninmanoj.com
=====
Sending attachment using mutt:
First install mutt using yum in Centos and in Ubuntu use apt-get
Centos:
====
yum install mutt
====
Debain/ Ubuntu:
====
apt-get install mutt
====
Command:
====
mutt -s "hello" maninmanoj@gmail.com -a /root/test.sh.tar.gz
===
-a ==> stands for attachment
Send mail with bash/shell scripts
====
#!/bin/bash
df -h | mail -s "details of partition" user@domain.com
====
Open a new file and add the lines above to that file, save it and run on your box. You will be getting the report of partitions and disk usage in a server as mail.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.