Linux and UNIX Mail Command to send and receive mail

Posted on in Categories , , , , , , , last updated April 22, 2007

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 [email protected]:
$ mail - "Hello" [email protected]
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 [email protected]:
$mail -s "Report 05/06/07" [email protected] < demo.txt

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

See also:

Howto send mail with attachment

32 comment

  1. 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.

  2. 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?

  3. hey Vivek,

    I got the resolution.

    The following is the syntax which will do the job:

    mail -s "subject" [email protected] -- -f [email protected]

  4. 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; [email protected]
    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 [email protected])
    by localhost.localdomain (8.13.8/8.13.8/Submit) id n7DGjU2o027727
    for [email protected]; Thu, 13 Aug 2009 18:45:30 +0200
    Date: Thu, 13 Aug 2009 18:45:30 +0200
    From: root
    Message-Id:
    To: [email protected]

    d

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

    Thanks in advance !!

    Greetings

  5. 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.

  6. This is an automatically generated Delivery Status Notification.

    Delivery to the following recipients failed.

    [email protected]

    Final-Recipient: rfc822;[email protected]
    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

  7. 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

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

  9. 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

    1. 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.

  10. 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?

  11. 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 [email protected]

    Thanks in advance

    1. Please, check your log file (ex: exim4)

      cat /var/log/exim4/mainlog

      Ex:

      ** [email protected] R=nonlocal: Mailing to remote domains not supported

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

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

    Thanks.

  13. 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

  14. 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.

  15. 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

  16. Hi everyone when write a mail from terminal for user while writing a mail how to save mail and exit from that mail

  17. Hi, I’m using mail utils in ubutnu 14.04. I can send the mails but when I check received mails it always shows “no mail for user” I don’t know what is going wrong ?

    Thanks in advance!!!

  18. Hi There,

    i want to know .. how to receive a mail from any domain(gmail.com/hotmail.com… etc) to my unix machine .

    how is it possible and what are required configurations we have to done in order to achieve that.

    NOTE:- i want to receive mail from gmail to my unix machine.

    Thanks,
    Mr.Cool

Leave a Comment