Softpanorama

Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
May the source be with you, but remember the KISS principle ;-)
Skepticism and critical thinking is not panacea, but can help to understand the world better

Sending email using mail command

News SMTP Mail Recommended Links Pipes in ~/.forward File Dot forward file Mail aliases
Scriptable and Command Line Mailers Mutt mailx Grepmail Perl Mail Processing Scripts Decoding Mime Attachments
Scripting Language Based Spam and Mail filtering UUCP The Unix Hater’s Handbook Horror Stories Humor Etc

The external mailx command is an electronic post office. You can send, receive, and store messages using the mailx command. It is based on the old mail command but provides a much better interface for handling messages.  More modern and more capable version of mail command line client is Mutt which is cabale of handing attachment when sending emails.

You can use the tilde (~) commands to edit, review, and modify your message before sending it to the recipient. You can list, display, save, delete, and pipe messages while in command mode. In general terms mailx provides you with a memo/message handler. You can send to one or multiple users. You can reply to the original author or to the author and all other recipients of a message. It even allows you to store messages and replies in files based on user names.

Like many of the UNIX commands mailx has been changed and enhanced by most vendors. Therefore, there is no guarantee the information that follows pertains exactly to your system's mail program. The mailx information that follows is AT&T System V. The mail information is from various BSD based systems. Information common to all systems was used, hopefully providing a complete base of information for the BSD mail command.

Throughout the module we refer to the mailx command. The BSD mail command is assumed to be synonymous unless stated differently.


NOTE: 
The old mail command is not documented in this book. It is slow, limited, and cumbersome to use. The BSD mail is usually located in /usr/ucb/mail. The mail program in /bin/mail is the old version of mail.


 

To Send Mail:

     mailx [ -dFinUV ] [ -h num ] [ -r adr ] [ -s subject ] recipients


If you specify user_names, mailx attempts to send the message to the list of users. If you do not specify any user_names, mailx reads messages from your mailbox or specified file and enters command mode.

SENDING MAIL

To send mail you simply specify the users you want to send messages as arguments to mailx. For example,

     mailx bill jill phil

sends the message you enter to the users, bill, jill, and phil. When you specify user names on the command line, mailx knows you want to send a message to recipients. After you press Return on the command line mailx prompts you for a subject line. You can press Return to have mailx ignore the subject line or you can enter a line of text. For example,

     mailx barb
     Subject: About the database problem!

Only the first 40 characters of the subject line are displayed when the user displays the header information using the h command.

After you enter the Subject line, mailx enters a simple editor or input mode. You enter your message text line-by-line pressing Return at the end of each line. You can use the Backspace key to erase characters on the current line but you cannot return to previous lines. To end the message you press Ctrl-D at the beginning of a line.

You can use the tilde escape commands to perform special functions while in input mode. One of the tilde commands allows you to enter the editor of your choice to edit the message. So even though mailx does not provide a true editor, it does allow you to invoke one to edit your message. For example,

     mailx barb
     Subject: About the database problem!
     ~v

causes mailx to enter the visual editor for you to edit your message.

The mailx header and tilde escape commands are discussed in the next two sections.

Examples

Adapted from Linux mail command examples – send mails from command line – BinaryTides By  | July 23, 2020 24 Comments

Here are some examples of how to use the mail command to send mails from the command line. These examples shall give you a basic idea of the various options and features supported by the mail command.

Note: If you are trying to send a test mail to some mail provider like gmail or outlook, then you would need to run these commands on a server with proper configuration. If you try to send mail from you local machine or desktop, then most spam filters would probably block it.

Sending a simple mail

Run the command below, to send an email to someone@example.com. The s option specifies the subject of the mail followed by the recipient email address.

$ mail -s "Hello World" someone@example.com

The above command is not finished upon hitting Enter. Next you have to type in the message. When you're done, hit 'Ctrl-D' at the beginning of a line

$ mail -s "Hello World" someone@example.com
Cc:
Hi Peter
How are you
I am fine
Good Bye
<Ctrl+D>

The shell asks for the 'Cc' (Carbon copy) field. Enter the CC address and press enter or press enter without anything to skip.

From the next line type in your message. Pressing enter would create a new line in the message. Once you are done entering the message, press . Once you do that, the mail command would dispatch the message for delivery and done.

Send a single line message

You can send one liner email message using "<<<" in bash:

$ mail -s "This is the subject" somebody@example.com <<< 'This is the message'

Send a message with the body of the messages sluped from the file

If the email message is in a file then we can use it directly to send the mail. This is useful when calling the mail command from shell scripts or other programs written in perl or php for example.

$ mail -s "Hello World" user@yourmaildomain.com < /home/user/mailcontent.txt

Or , using cat,

$ cat  /home/user/mailcontent.txt | mail -s "This is the subject" mail@example.com

Specify CC and BCC recipients

Other useful parameters in the mail command are:

-c email-address (CC - send a carbon copy to email-address)
-b email-address (BCC - send a blind carbon copy to email-address)

Here's and example of how you might use these options

$ mail -s "Hello World" user1@example.com -c usertocc@example.com -b usertobcc@example.com

Sending to multiple recipients

It is also possible to specify multiple recipients by joining them with a comma.

$ mail -s "Hello World" user1@example.com,user2@example.com

Specify the FROM name and address

The "-a" option allows to specify additional header information to attach with the message. It can be used to provide the "FROM" name and address. Here is a quick example

# echo "This is the message body" | mail -s "This is the subject" mail@example.com -aFrom:sender@example.com

The a option basically adds additional headers. To specify the from name, use the following syntax.

$ echo "This is the body" | mail -s "Subject" -aFrom:Harry\<harry@gmail.com\> someone@example.com

Note that we have to escape the less/great arrows since they have special meaning for the shell prompt. When you are issuing the command from within some script, you would omit that.

Send mail to a local system user

To send mail to a local system user just use the username in place of the recipient address

$ mail -s "Hello World" username

You could also append "@hostname" to the username, where the hostname should be the hostname of the current system.

Verbose output

Sometimes when testing mail servers, you would want to check the SMTP commands being used by the mail command. Use the "-v" option for that

$ mail -v -s "This is the subject" somebody@example.com <<< 'This is the message'

If the mail fails to deliver due to an improperly configured mail server for example, the smtp command log will show what has gone wrong.

Send mail with bash/shell scripts

This example demonstrates how the output of a command can be used as the message in the email.
Here is an easy shell script that reports disc usage over mail.

#!/bin/bash
du -sh | mail -s "disk usage report" user@yourmaildomain.com

Open a new file and add the lines above to that file, save it and run on your box. You will receive an email that contains "du -sh" output.


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2020 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: November 15, 2020