Tuesday 3 July 2018

Mail Command Examples in Unix / Linux

Mail Command, Linux Study Materials, LPI Certification, LPI Guides, LPI Tutorials and Materials

The Mail command in unix or linux system is used to send emails to the users, to read the received emails, to delete the emails etc. Mail command will come in handy especially when writing automated scripts. For example, you have written an automated script for taking weekly backup of oracle database. How to know the status of backup, whether it is succeeded or not? In this case, sending an email from the automated script at the end of the backup will be helpful in knowing the status.

The syntax of mail command is:

mail [options] to-address [-- sendmail-options]

The options of mail command are listed below:

-v : Verbose mode. Delivery details are displayed on the terminal.
-s : Specify the subject of the mail
-c : Send carbon copies of the mail to the list of users. This is like cc option in Microsoft outlook.
-b : Send blind copies of the mail to the list of users. This is like bcc option in outlook.
-f : Read the contents of the mailbox
-r : Specify the from address in send mail options.

Mail Command Examples - Sending Emails:


1. Sending sample email to user

The basic functionality of the mail command in unix or linux system is to send an email to the user.

echo "Mail body" | mail -s "Mail subject" to@example.com

Here the echo statement is used for specifying the body of the email. The -s option is used for specifying the mail subject. The mail command sends the email to the user to@example.com

2. Specifying the body in a file

You want to compose a mail which contains 100 lines in the body. Specifying the body with the echo statement is a tedious process. So write the contents of the body in a file and send the mail using one of the following options:

Using cat statement:

cat body.txt | mail -s "Mail subject" to@example.com

Using input redirection operator

mail -s "Mail subject" to@example.com < body.txt

Here the body.txt file contains the body of the email.

3. Send mail to more than one user You can send email to more than one user by specifying the users in comma separated list. 

mail -s "Mail subject" "user1@example.com,user2@example.com" < body.txt

4. Using the cc and bcc option You can copy the emails to more number of users by using the -c and -b options. An example is shown below: 

mail -s "Mail subject" -c "ccuser@gmail.com" -b "bccuser@yahoo.com" "user@example.com" < body.txt

5. Specifying the from address So far the above examples send the emails with from address as the logged in user. You can explicitly specify the from-address using the -r option. 

cat body.txt | mail -s "Mail subject" "to-user@example.com" -- -r "from-user@example.com"

6. Attaching files. The mail command does not provide an option for attaching files. There is a workaround for attaching files using the uuencode command. Pipe the output of uuencode command for attaching files. 

uuencode attachment-file | mail -s "Mail subject" "to-user@example.com" < body.txt

Mail Command Examples - Reading Emails: 


1. Viewing all the received emails Simply type the mail and then press enter to view the received emails. 

mail

Another way of viewing the emails is using the -f option. This is shown below:

> mail -f /var/spool/mail/user

Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user": 2 messages 2 new
>N  1 root@hostname  Tue May 17 00:00  21/1013  "Mail subject 1"
 N  2 root@hostname  Wed May 18 00:00  21/1053  "Mail subject 2"
&

From the above output, you can see that, it displays the from-address, date and subject of the emails in the inbox. It also displays the ampersand (&) prompt at the end. To go back to the main prompt, type CTRL+z or CTRL+d depending on your operating system and press enter. The ampersand prompt allows you to read, reply, navigate and delete the emails.

2. Reading an email.

To read the Nth email, just enter the mail number at the ampersand prompt and press enter. This is shown below:

> mail -f /var/spool/mail/user

Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user": 2 messages 2 new
>N  1 root@hostname  Tue May 17 00:00  21/1013  "Mail subject 1"
 N  2 root@hostname  Wed May 18 00:00  21/1053  "Mail subject 2"
&2
Message 2:
From root@hostname  Wed May 18 00:00  21/1053
---------------
Subject: Mail subject 2
------------

This displays the second email details.

3. Navigating through inbox emails. To go to the next email, enter the + symbol. To go back to the previous email, enter the - symbol at the ampersand prompt. 

&-
Message 1:
From root@hostname  Tue May 17 00:00  21/1013
---------------
Subject: Mail subject 1
------------

4. Replying email. Once you have read an email, you can give reply to the mail by typing "reply" and pressing enter. 

&reply
To: root@hostname
    root@hostname
Subject: Re: Mail subject1

5. Deleting emails. You can delete a read email by typing the d and pressing enter. You can also specify the email numbers to d option for deleting them. 

To delete read email
&d
To delete emails 1 and 2
&d 1 2
To delete range emails from 10 to 30
&d 10-30
To delete all emails in the mbox (mail box)
&d *

Related Posts

0 comments:

Post a Comment