The mail/Mail/mailx commands are used in unix flavoured operating systems like Linux, HPUX, AIX,Linux and many more unix based systems are used to send emails to the users, to read the received emails, to delete the emails etc.
It would be very usefull when you are working with shell scripts. A good application for using mail/mailx would be to send alerts, or process a file and then email it to somebody, extract data from your database/application and email the resulting data or file, etc.
The syntax of mail command is:
When you see this prompt, enter any mailbox subcommand. To see a list of subcommands, type:
?
This entry lists the Mail subcommands.
Lets re-write above command for multi recipients
< operator
Lets say for example here we need send contents of somefile.txt through mail ,
-c & -b options. An example is shown below:
There is a workaround for attaching files using the uuencode command. Pipe the output of uuencode command for attaching files.
When you see this prompt, enter any mailbox subcommand. To see a list of subcommands, type:
?
Another way of viewing the emails is using the -f option. This is shown below:
+ symbol. To go back to the previous email, enter the - symbol at the ampersand prompt.
"reply" and pressing enter.
d and pressing enter. You can also specify the email numbers to d option for deleting them.
It would be very usefull when you are working with shell scripts. A good application for using mail/mailx would be to send alerts, or process a file and then email it to somebody, extract data from your database/application and email the resulting data or file, etc.
The syntax of mail command is:
mail [options] to-address [-- sendmail-options]
-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
-e : Tests for the presence of mail in the system mailbox.
-F : Records the message in a file named after the recipient.
-r : Specify the from address in send mail options.
-u : Specifies an abbreviated equivalent of doing mail -f /var/spool/mail/UserID.
-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
-e : Tests for the presence of mail in the system mailbox.
-F : Records the message in a file named after the recipient.
-r : Specify the from address in send mail options.
-u : Specifies an abbreviated equivalent of doing mail -f /var/spool/mail/UserID.
1)To start the Mail program and list the messages in your mailbox:
# mail
The mail command lists every messages in your system mailbox. The mail system then displays the mailbox prompt (?) to indicate waiting for input.When you see this prompt, enter any mailbox subcommand. To see a list of subcommands, type:
?
This entry lists the Mail subcommands.
2) Sending email to a user:
# echo "Test of Mail body" | mail -s "Mail subject" to@somedomain.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@somedomain.com
another way is
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@somedomain.com
# mail -s "Mail subject" to@somedomain.com
in this example you are then expected to type in your message, followed by an "control-D" at the beginning of a line. To stop simply type dot (.) as follows:
Hi,
This is a test
.
Cc:
if you wish to send to multiple mail users just add the mail ids side by side with spacesin this example you are then expected to type in your message, followed by an "control-D" at the beginning of a line. To stop simply type dot (.) as follows:
Hi,
This is a test
.
Cc:
Lets re-write above command for multi recipients
# echo "Test of Mail body" | mail -s "Mail subject" recipient1@somedomain.com recipient2@somedomain.com to1@example.com
3) Sending contents of a text file
you can send the contets in two ways using cat/echo or using a input redirectLets say for example here we need send contents of somefile.txt through mail ,
# cat somefile.txt | mail -s "Mail subject" "user1@example.com,user2@example.com"
# mail -s "Mail subject" "user1@example.com,user2@example.com" < somefile.txt
# mail -s "Mail subject" "user1@example.com,user2@example.com" < somefile.txt
4) Mail Usage with CC & Bcc :
Using the cc and bcc option You can copy the emails to more number of users by using the
# echo "something" | mailx -s "subject" -b bcc_user@unixmantra.com -c cc_user@some.com -r sender@some.com recipient@example.com
# mail -s "Mail subject" -c "cc_user@some.com" -b "bccuser@yahoo.com" "user@example.com" < somefile.txt
# mail -s "Mail subject" -c "cc_user@some.com" -b "bccuser@yahoo.com" "user@example.com" < somefile.txt
5) 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 attachment-file | mail -s "Mail subject" "to-user@example.com" < somefile.txt
Working with Mailbox in the server
1) To start the Mail program and list the messages in your mailbox:
# mail
The mail command lists every messages in your system mailbox. The mail system then displays the mailbox prompt (?) to indicate waiting for input.When you see this prompt, enter any mailbox subcommand. To see a list of subcommands, type:
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"
&
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.
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
&-
Message 1:
From root@hostname Tue May 17 00:00 21/1013
---------------
Subject: Mail subject 1
------------
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
To: root@hostname
root@hostname
Subject: Re: Mail subject1
To: root@hostname
root@hostname
Subject: Re: Mail subject1
5. Deleting emails.
You can delete a read email by typing the
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 *
&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 *