Linux and UNIX Mail Command to send and receive mail

by Vivek Gite on April 22, 2007 · 18 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 18 comments… read them below or add one }

1 Roby December 28, 2008

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

Hey Vivek,

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

Reply

3 Vivek Gite March 24, 2009

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

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

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 Vivek Gite March 25, 2009

Gagan,

Thanks for sharing back the resolution.

Reply

7 Raja December 16, 2010

Thank you so much….

thanks,
Raja K

Reply

8 caroline August 13, 2009

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

9 Gagan Brahmi August 20, 2009

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

10 Alee August 28, 2009

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

Reply

11 Crispin November 19, 2009

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

12 Mihir Joshi December 30, 2009

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

13 Tamilarasan March 21, 2010

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

14 Neha Srivastava August 12, 2010

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

15 sampath December 3, 2010

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

Reply

16 Arun December 5, 2010

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

Reply

17 Crispin Joshi January 6, 2011

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

18 Nits January 6, 2011

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

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 4 + 13 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: