Linux and UNIX Mail Command to send and receive mail

by on April 22, 2007 · 28 comments· LAST UPDATED April 22, 2007

in , ,

Q. Hey vivek, I need to know how to read and send mail from command line. I’m using Debian Linux. Also can you tell me how do I print names of those who have sent mail?

A. Sending and receiving mail from command line is quite command under UNIX or Linux operating system. You need to use following commands:

a) mail: send and receive mail

b) from: print names of those who have sent mail

c) mutt: Mutt is a small but very powerful text based program for reading electronic mail under UNIX operating systems, including support color terminals, MIME, and a threaded sorting mode.

d) pine: Yet another text based program for reading electronic mail under UNIX operating systems. Originally written for inexperienced users, Pine's basic features are generally very easy to use.

Task: Check for mail / print names of those who have sent mail

The from utility prints out the mail header lines from the invoker’s mailbox.
$ from

Task: Read mail

Mail is an intelligent mail processing system:
$ mail

In normal usage mail is given no arguments and checks your mail out of the post office, then prints out a one line header of each message found. The current message is initially the first message (numbered 1) and can be printed using the print command (which can be abbreviated ‘p’). You can move among the messages much as you move between lines in ed, with the commands ‘+’ and ‘-’ moving backwards and forwards, and simple numbers.

After examining a message you can delete message by typing d
You can reply message by typing r
The message can be undeleted u by giving its number, or the mail session can be aborted by giving the exit x command.

Task: Compose mail

Use following format:
mail -s <subject> <mailaddress>

For example write mail to boss@yahoo.com:
$ mail - "Hello" boss@yahoo.com
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 (.):
Output:

Hi,
This is a test
.
Cc: 

Task: Mail a whole text file

Mail a whole text file called demo.txt to boss@yahoo.com:
$mail -s "Report 05/06/07" boss@yahoo.com < demo.txt

You can also type mutt or pine to read and send mail:
$ mutt
OR
$ pine

See also:

Howto send mail with attachment

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 28 comments… read them below or add one }

1 Roby December 28, 2008 at 6:06 pm

i got an acer aspire one for crimbo.

it has msn on it but i cant seem 2 get it 2 work even though i already have an account on windows msn. do i have 2 have a certain email network because everytime i try to log in it says “our protocol is not supported by the server”

please help me.

Reply

2 Gagan Brahmi March 24, 2009 at 2:16 pm

Hey Vivek,

Do you know how can we change the from email address while using the mail command to send emails?

Reply

3 nixCraft March 24, 2009 at 2:46 pm

You can’t.

But, you can write a perl or python script. There is also a utility called ssmtp or sendmail -ba option.

HTH

Reply

4 Gagan Brahmi March 24, 2009 at 2:52 pm

Yeah I know that option, but I am using postfix. So the other option I see if using content filtering in postfix to change the from email address to certain domains. Any ideas about this filter setup?

Reply

5 Gagan Brahmi March 25, 2009 at 9:07 am

hey Vivek,

I got the resolution.

The following is the syntax which will do the job:

mail -s "subject" recipient@domain.tld -- -f sender@domain.tld

Reply

6 nixCraft March 25, 2009 at 10:04 am

Gagan,

Thanks for sharing back the resolution.

Reply

7 Raja December 16, 2010 at 7:22 am

Thank you so much….

thanks,
Raja K

Reply

8 Subbiah September 14, 2012 at 12:34 am

Thanks Gagan. The command you provided helped me as well.

Reply

9 Sunil October 31, 2012 at 12:04 pm

Hi,

I have used but it is taking long time.

My OS is Red hat 4.

Reply

10 caroline August 13, 2009 at 5:51 pm

Hi,

I started Sendmail.
And then I wanted to send an email with the mail command to my Hotmail-account.
But it doesn’t work.
My mail always end up in the file /var/spool/mail/root, namely:
————————————————————————————————————
Final-Recipient: RFC822; cccc@hotmail.com
Action: failed
Status: 5.0.0
Diagnostic-Code: SMTP; 550 DY-001 Mail rejected by Windows Live Hotmail for policy reasons. We generally do not accept email from dynamic IP’s as they are not typically used to deliver unauthenticated SMTP e-mail to an Internet mail server. http://www.spamhaus.org maintains lists of dynamic and residential IP addresses. If you are not an email/network admin please contact your E-mail/Internet Service Provider for help. Email/network admins, please visit http://postmaster.live.com for email delivery information and support
Last-Attempt-Date: Thu, 13 Aug 2009 18:45:32 +0200

–n7DGjWx4027730.1250181932/localhost.localdomain
Content-Type: message/rfc822

Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.8/8.13.8) with ESMTP id n7DGjVx4027728
for ; Thu, 13 Aug 2009 18:45:31 +0200
Received: (from root@localhost)
by localhost.localdomain (8.13.8/8.13.8/Submit) id n7DGjU2o027727
for cccc@hotmail.com; Thu, 13 Aug 2009 18:45:30 +0200
Date: Thu, 13 Aug 2009 18:45:30 +0200
From: root
Message-Id:
To: ccccc@hotmail.com

d

–n7DGjWx4027730.1250181932/localhost.localdomain–
——————————————————————————————————————
Do you maybe know what the reason is?

Thanks in advance !!

Greetings

Reply

11 Gagan Brahmi August 20, 2009 at 10:19 pm

Problem is the hostname set for your machine. It seems that you haven’t changed the hostname for your machine. It still shows localhost.localdomain. Many mail servers around do not accept emails originating from localhost.localdomain. The hostname should be a FQDN if you to send emails over the internet.

Reply

12 Alee August 28, 2009 at 1:54 am

Can somebody teach me how to configure mail using solaris 9.

Reply

13 Crispin November 19, 2009 at 7:52 am

This is an automatically generated Delivery Status Notification.

Delivery to the following recipients failed.

rd.shiva@hotmail.com

Final-Recipient: rfc822;rd.shiva@hotmail.com
Action: failed
Status: 5.0.0
Diagnostic-Code: smtp;550 DY-001 Mail rejected by Windows Live Hotmail for policy reasons. We generally do not accept email from dynamic IP’s as they are not typically used to deliver unauthenticated SMTP e-mail to an Internet mail server. http://www.spamhaus.org maintains lists of dynamic and residential IP addresses. If you are not an email/network admin please contact your E-mail/Internet Service Provider for help. Email/network admins, please visit http://postmaster.live.com for email delivery information and support

Please anybody help me…. urgent….

Thanks
Cris J

Reply

14 Mihir Joshi December 30, 2009 at 7:16 pm

is that possible to send a txt file which includes colored text as it it?
When i am trying to send the txt file, the recipient receives the file with default color text but originally file contains text with red and blue color.
I am trying to send via a command line

Reply

15 Tamilarasan March 21, 2010 at 4:17 am

Hello Sir/Madam,
I need basic and full detail about solarise administrator.can you please send me.
I am waiting for your favorable reply.

Reply

16 Neha Srivastava August 12, 2010 at 5:44 am

Hi, Can anyone tell me or suggest me as what should be my move if I want that a procedure which I run in UNIX is; if accidently or intentionally killed then it should shoot a mail to my mail ID as to inform that the process has been terminated.

I want to know that what has to be done and studied to create such procedure and knowledge of what is required???

Thanks for helping me in advance

Reply

17 sampath December 3, 2010 at 8:00 am

How can you find out if you have the permission to send a message?

Reply

18 Arun December 5, 2010 at 9:29 pm

We are not here to solve ur TCS tutorials. What is your DT ID, Password Sampath, I’ll complete your tute myself?

Reply

19 Crispin Joshi January 6, 2011 at 4:30 pm

To check the file permissions, we can give the command;

% ls -l

If, in the output, we get; -rw——- , then only the owner have the right to send the message, while others can’t.

Reply

20 Nits January 6, 2011 at 8:23 pm

1. Create a tree structure named ‘training’ in which there are 3 subdirectories – ‘level 1’,’ level2’ and ‘cep’. Each one is again further divided into 3. The ‘level 1’ is divided into ‘sdp’, ‘re’ and ‘se’. From the subdirectory ‘se’ how can one reach the home directory in one step and also how to navigate to the subdirectory ‘sdp’ in one step? Give the commands, which do the above actions?

2. How will you copy a directory structure dir1 to dir2 ? (with all the subdirectories)

3. How can you find out if you have the permission to send a message?

4. Find the space occupied ( in Bytes) by the /home directory including all its subdirectories.

5. What is the command for printing the current time in 24-hour format?

6. What is the command for printing the year, month, and date with a horizontal tab between the fields?

7. Create the following files: chapa, chapb, chapc, chapd, chape, chapA, chapB, chapC, chapD, chapE, chap01, chap02, chap03, chap04, chap05, chap11, chap12, chap13, chap14, and chap15.

8. With reference to question 7, What is the command for listing all files ending in small letters?

9. With reference to question 7, What is the command for listing all files ending in capitals?

10. With reference to question 7, What is the command for listing all files whose last but one character is 0?

11. With reference to question 7, What is the command for listing all files which end in small letters but not ‘a’ and ‘c’?

12. In an organisation one wants to know how many programmers are there. The employee data is stored in a file called ‘personnel’ with one record per employee. Every record has field for designation. How can grep be used for this purpose?

13. In the organisation mentioned in question 12 how can sed be used to print only the records of all employees who are programmers.

14. In the organisation mentioned in question 12 how can sed be used to change the designation ‘programmer’ to ‘software professional’ every where in the ‘personnel’ file

15. Find out about the sleep command and start five jobs in the background, each one sleeping for 10 minutes.

16. How do you get the status of all the processes running on the system? i.e. using what option?

Reply

21 Lucho March 23, 2012 at 5:17 pm

You can use this line:
echo ‘This is my message’ | mail -s ‘This is the subject’ lucho@server.com.uy

regards!

Reply

22 Ramandeep August 9, 2012 at 9:59 am

Hi,
I want to send the output of mail.txt to my mail id but i am getting the following output.

[@****** ~/Scripts]$ cat mail.txt | mail -s “BAU Monitoring” Ramandeep.Garg@***.com
[@***** ~/Scripts]$

I got the prompt back but did not receive any mail.
* Email id is correct.
* I ran this command in putty in UNIX.
Can you please help me out and send your understanding to ramandeep_garg@infosys.com

Thanks in advance

Reply

23 Lucho August 9, 2012 at 3:48 pm

Please, check your log file (ex: exim4)

cat /var/log/exim4/mainlog

Ex:

** myemail@domain R=nonlocal: Mailing to remote domains not supported

PS: You can paste here a link to your log file (use pastebin)

Reply

24 vanuser September 12, 2012 at 6:15 pm

Hi,
Is there any way we can send high priority email using mail.
mail -s “TEST” “to@xyz.com” — -r “from@xyz.com”
Do you know what parameter to add high prioruty email for above command.

Thanks.

Reply

25 Mohanad October 16, 2012 at 10:12 am

Hi,

Thanks alot for your great script but i have two questions :

1. Now if there is more than one mount point exceeding 90% it will send an email for each mount point, i need one email contain all moint point exceeding 90%.

2. How to send mount point name instead of filesystme name?

Thank your very much
Muhanad

Reply

26 Yosef March 3, 2013 at 7:32 pm

I use Alpine to send and recive mail.
I send mail to gmail and it is work.
I send from gmail to my local server and it not get to me (no error in gmail).
I send from my local server to it selfe :
The mail found in send mails but not recived to inbox.

Reply

27 Alucarx85 October 22, 2013 at 8:54 pm

how can you use “grep” to see a list of all emails got by a user,, let’s say the user name is “hpedro” between times 10:00:00 & 16:59:59.? maillog

Reply

28 hari May 30, 2014 at 6:27 am

Hi,
This helps a lot. Thank you.

Reply

Leave a Comment

Tagged as:

Previous Faq:

Next Faq: