Untitled Document
mailx Command

mailx command is used to send mail and to check received mail . When you run mailx with sending mode , you have to give email address of the recipient as an argument .In receiving mode , you generally use it without arguments to handle your received mail .Here we will see most of the sending mail exmples of mailx.

Example 1 -

Run below mailx command . The command will send a test mail to user mohan . When we invoke mailx command with username , it prompts for Subject , type the subject for your mail then press enter , type the message body and press [ ctrl-d]. The email will be sent to recipient .

   mailx mohan 
   Subject : Test Mail 
   Your login credential are generated , 
   Please try to loging with you new credentials .
   [ctrl -d ]
   EOT     

Example 2 - Sending Mail Non interactively

We often need to send mail via shell script , we can use below command which sends the message body from a file .I have some jobs to run which generates some logs and want to send it over email . So here goes my command . We have added -s ( Subject ) option which takes input for subject .

   mailx -s "Execution Logs " mangesh < logs.txt    

Example 3 - Sending Multiple copies of email .

add -c ( copy contact ) option to above example and put multiple recipient in quotes . The same will be send to multiple users .

   mailx -s "Execution Logs" -c "mohan,nikhil" mangesh < logs.txt  

Example 4 - Send the list via email for the user logged in to particular host

To send the user list logged in to host via email - we need combination of three commands who , cut and mailx. Lets run the below command and check whether it generates the expected o/p or not .

  who | cut -d " " f1 | mailx -s "List of user logged in on `hostname` mangesh 
Untitled Document