If your machine is connected to the Internet, you can use it to send e-mail from the command line. If your machine is connected all the time ("24/7," as they say), then you can use it as a server to receive incoming e-mail and store messages in mailboxes for each user on your system.
Mac OS X comes with a common open -source e-mail server called Postfix. (Mac OS X 10.2 used another, even more common server called Sendmail.) In this section we will show how to configure the Postfix mail server to receive incoming e-mail and save messages to the appropriate mailbox for each user.
Postfix actually provides two kinds of e-mail services: It is a Mail Transport Agent ( MTA ) and a Mail Delivery Agent ( MDA ). MTAs handle the exchange of e-mail between MTAs across the Internet. That is, MTAs talk to other MTAs across networks. MDAs handle incoming messages and store them in the appropriate mailbox for each user. (In more complex configurations, a mail server may also forward incoming mail to other machines for further processing.)
Before proceeding with the following task, make sure your machine has an FQDN that has a DNS entry associating the FQDN of your machine's IP address. See "Setting Your Machine's Hostname," at the beginning of this chapter, for more details. The Postfix configuration will automatically read what the hostname command returns and use that as the FQDN for which it expects to be receiving e-mail (although you can override that behavior).
To configure Postfix to receive e-mail addressed to your FQDN:
You will need to know the FQDN that you machine thinks it has. Running the hostname command shows this to you. Make a note of the result.
Become root, entering your password if prompted:
You are going to perform several commands as root in this task, so we have you start a root shell rather than run sudo for each command line. Your shell prompt will change. Now you are going to back up the launchd configuration file that controls Postfix.
This changes your working directory to where the Postfix configuration files are kept.
cp main.cf main.cf. YYYYMMDD
You are making a backup of the file. Replace YYYYMMDD with the year, month, and day of the current datefor example:
cp main.cf main.cf.20050921
Edit main.cf .
Here's where you make some changes:
Change the line near the end of main.cf in the section beginning with the comment THE FOLLOWING DEFAULTS ARE SET BY APPLE from
inet_interfaces = localhost
inet_interfaces = localhost, $myhostname
This tells Postfix to listen on the IP address associated with the hostname. You should also add a comment (lines starting with # ) explaining what you did.
launchctl stop org.postfix.master
This stops the Postfix master daemon, if it happens to be running.
Change your working directory so that the pathnames you type next won't be so long.
launchctl unload -w org.postfix.
This sets the Disabled key in the launchd configuration file and writes it back to disk. (See Chapter 11 for more on launchctl and launchd .)
cp org.postfix.master.plist org.
Here you are making a backup copy of the plist file. Change YYYYMMDD to be the current year, month, and day.
Edit the file org.postfix.master.plist .
Using vi or another text editor, edit the file and make the following changes:
launchctl load -w org.postfix.
This loads the new plist file into launchd . The -w flag changes the Disabled key to false and writes the file back to disk.
launchctl start org.postfix.master
This tells launchd to start the Postfix daemon.
Stop being root:
You should be back at your regular shell prompt.
Check firewall settings.
Look in the Sharing pane of System Preferences and click the Firewall tab. If you have the firewall on, open up port 25. See Chapter 12, and in particular the section "About ports and sockets," for more on the firewall.
Test the setup.
Send yourself e-mail from another system (or ask a friend to do it), using the FQDN for your Mac OS X machine.
Watch the mail-server log file while testing. Type
sudo tail -f /var/log/mail.log
to see the mail server accept each incoming message.
The central source for information about Postfix is Postfix.org (http://postfix.org/). There you can find documentation, how-tos and FAQs, mailing lists, and more.
Sending Mail Addressed to an IP Address
It is perfectly valid to send e-mail that is addressed to an IP address rather than a domain name . To do so, you must enclose the IP address in square brackets:
If you are doing this from the command line, make sure to escape the brackets:
because they would otherwise be interpreted by the shell as the test command. See man test : Looking carefully at the NAME line in the manual, see the [ character? It's another name for the test command.
Sometimes you want e-mail that arrives for one address to be automatically forwarded to another address. There are a couple of ways to do this. What we describe here works when you want to forward e-mail to an account that already exists on your system. It is also possible to set up virtual userse-mail addresses on your system that do not correspond to actual user accounts but rather to things like roles or departments (for example, email@example.com). Setting up virtual users in Postfix is beyond the scope of this book, but you can find instructions for doing it at the Postfix Virtual Domain Hosting Howto (www.postfix.org/VIRTUAL_README.html).
To forward e-mail for an existing user account:
Create a file called .forward in the home directory of the user whose e-mail is to be forwarded.
The file should contain a single line: the e-mail address you want to forward to. For example,
sudo echo "firstname.lastname@example.org" > ~ user /.forward
where user is the user name of the user on your system and the address is whatever address you want to forward the mail to.
Enter your password if prompted for it.
If you want a copy of the forwarded e-mail to stay on your system, use this format:
\ user , email@example.com
Set the ownership on the file:
sudo chown user .forward
Spam e-mail is unsolicited material, usually asking for money or selling products, sent to multiple e-mail addresses (the name comes from the Monty Python sketch involving the seemingly endless repetition of the word spam ).
Spam wastes bandwidth, and, more important, it wastes the one thing none of us can ever get more of: time.
If you are running a mail server that is being hit with spam, you should look into anti-spam features. (The Mac OS X GUI e-mail client, Mail, has anti-spam features you can use to help with your personal e-mail account.)
Probably the premier anti-spam software for mail servers is SpamAssassin (http://spamassassin.org).
There is a Mac OS X how-to for Spam- Assassin at www.stupidfool.org/docs/sa.html.
Providing Remote Access to Users' E-mail: IMAP and POP
Another kind of mail server allows users to connect and download or read their e-mail from other machines. These servers provide remote access to users' mailboxes (kept in /var/mail on Mac OS X) and utilize either the complex and powerful IMAP ( Internet Mail Access Protocol ) or the simpler but less capable POP ( Post Office Protocol ).
For information on IMAP, go to the IMAP Connection (www.imap.org). It has a searchable database of IMAP software, such as clients and servers (www.imap.org/products/database.php).
If you are considering setting up your Mac OS X machine to provide remote access to users' e-mail, we suggest that you use an IMAP server. This will allow your users to read their mail from multiple remote machines (such as from home or the office), as well as provide better security, since IMAP servers can be configured to use encryption.
Two no-cost open-source IMAP servers:
Here are three commercial IMAP servers (which also handle POP):