Tuesday, November 13, 2007

MailX - Mail Facility via Terminal

One linux command available from most linux distro is mailx.

In general, mailx is a simnple command line mail sending and receiving facility. Mailx tool, when invoked with correct CLI parameter, is capable of checking emails, sending and receiving mails from command line terminal. Mailx is a powerful tool which can offer a flexible and various command line options down from composing emails, sending attachment files, checking emails and receiving incoming mails. You can see more of its man pages.

With Fedora and RedHat, Mailx binary is part of redhat-lsb rpm package.

MailX Options:
==============

How to check emails using mailx from command line terminal?
-------------------------------------------------

As root, simply issue from terminal:
~~~~~~~~~~~~~~~~~~~
# mailx
~~~~~~~~~~~~~~~~~~~

You can access more mailx command options and help by pressing ?

Several interactive key commands inside mailx facility are:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t type messages
n goto and type next message
e edit messages
f give head lines of messages
d delete messages
s file append messages to file
u undelete messages
R reply to message senders
r reply to message senders and all recipients
pre make messages go back to /usr/spool/mail
m mail to specific users
q quit, saving unresolved messages in mbox
x quit, do not remove system mailbox
h print out active message headers
! shell escape
cd [directory] chdir to directory or home if none given
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To quit mailx facility, simply type q and hit enter key.


How to compose and send an email using mailx via terminal?
------------------------------~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The below samples sends an email to root@localhost using mailx

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# mailx -v root@localhost
Subject: linux sysad blog
and this is my email body
.
Cc: [ENTER]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After hitting the ENTER key, mailx mail facility shows a more verbose logging from mail handshake to mail delivery status.


Alternatively, to send an email using mailx from terminal, simply issue
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# echo "Body of Email" | mailx -v -s "Linux Blog Subject" root@localhost -c root@google.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


How to inject or insert a text file as an email body to mailx mail message?
-------------------------------------------------------------------------

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# cat body.txt
Hello, How are you ?

# cat body.txt | mailx -v -s "Linux Blog Subject" root@localhost -c root@google.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As shown above, the file body.txt is a text file that would be appended into the email message serving as the body of that particular email message.


How to attach and append a text file inside mailx mail facility via terminal?
------------------------------------------------------------------------------

To attach a text file from the email using mailx, this can be done inside the mailx mail facility by specifying ~r interactively. The ~r format should be followed by a text filename separated by a space like so

# cat text.txt
~~~~~~~~~~~~~~~~~~~~~
Hello, How are you ?
~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# mailx -v -s "Linux Blog Subject" root@localhost
I am typing the body of email, now I will attach a text file using the below command
~r text.txt
.
Cc: root@google.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note that ~r parameter works only with text file. Yes, you need a separate character encoding mechanism when attaching any non-text files using mailx mail facility. This can be done using a separate linux binary then.

How to send and attach non-binary file as email attachments from terminal?
--------------------------------------------------------------------------

To achieve this, as an alternative to mailx mail facility, you can use the linux mail binary command on sending non-binary email attachments from terminal.

For further sample of mail binary command, refer to previous posting here.

Hope this helps.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy