UNIX mail

From HaFrWiki
Jump to: navigation, search

The UNIX-command line Mail can do everything but it is a little bit nasty. Feel free to add more.

After a while experimenting with the standard mail in terminal and the usage of SMTP and postfix, I decided not to use this method anymore and have installed PHPMailer.php.
Please see my experiences on PHP Mail Local OSX and Web-Internet.

Send with Attachment

To send an email from UNIX/Linux with the attachment as filename. Use:

 uuencode DbTables.txt TheDbTables.txt| mail -s "Title DbTables" frielink@xx.yyy.com


  1. uuencode the file DbTables.txt and name in as attachment TheDbTables.txt.
  2. Give it the title Title DbTables.
  3. Send to email address frielink@xx.yyy.com.

To send an email with attachment and body use:

 cat body.txt;uuencoce file file.txt|mailx -s "Attachments" xyz@xxx.com

Mail Basics

To send a short note to the person with the email address nobody@december.com. Here is an example of how I would do this:

$ mail nobody@december.com
Subject: Hi
This is just a short note to say hello.
I don't have anything else right now.

The first line:

 $ mail nobody@december.com

Unix responded with the line starting with Subject: after hitting the Enter key.

  • Type the text Hi and hit the Enter key.
  • Type the first sentence and hit the Enter key.
  • Type the second sentence and hit the Enter key.
  • When done with the email message, type a period (.) in the first column.
    This period in the first column told the mail program to stop.

The mail program then prompted with Cc: to ask for anyone else's email address whom will get copies of this message. Since there are no more email addresses used, press Enter, which will end the mail session, and give the shell prompt back. Alternatively to stop the mail use ctrl-D.

Mail on Mac OS X Lion

I have been trying to get my command line mail working on my MacOS (Lion) today and I noticed that the normal postfix emails get treated as SPAM by Google and because I was sending emails to myself on my gmail account, that was an issue for me. So, I thought of using gmail as my outgoing SMTP server for this. Here are the steps I followed :

Configure Postfix for Gmail SMTP Edit file /etc/postfix/main.cf

  1. sudo vim /etc/postfix/main.cf

and add in the following below the commented out relayhosts :-

  1. relayhost = [smtp.gmail.com]:587
  2. smtp_sasl_auth_enable = yes
  3. smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
  4. smtp_sasl_security_options = no anonymous
  5. smtp_use_tls = yes

Generate sasl_password if not already exists

  1. sudo vim /etc/postfix/sasl_passwd

and enter in the following:-

  1. [smtp.gmail.com]:587 username@gmail.com:password

Run the following commands

  1. sudo chmod 600 /etc/postfix/sasl_passwd
  2. sudo postmap /etc/postfix/sasl_passwd
  3. sudo launchctl stop org.postfix.master
  4. sudo launchctl start org.postfix.master

And you are done….
Now, you should be able to send emails from within the command line e.g. to send the contents of a directory as a tree to an email address

  1. tree /var/www/somefolder | mail -s "contents" your@yourdomain.com

Hope someone finds this useful!


To stop postfix completely [1]

  • sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/org.postfix.master.plist

To Relaunch

  • sudo /bin/launchctl load -w /System/Library/LaunchDaemons/org.postfix.master.plist

The normal procedure are:

  • sudo postfix start|stop|reload

But the stop functionality will immediate start postfix again that's why the /bin/launchctl unload is needed.

See also


Postfix tls


  • MacWorld, Hints - Send mail from Terminal using mail/sendmail .