TIP Postfix Setup for Local Mail Only
From Gentoo Linux Wiki
| Terminals / Shells • Network • X Window System • Portage • System • Filesystems • Kernel • Other |
Contents |
[edit] Introduction
Below I show how to setup a simple mail server (MTA) on your machine for local only delivery of local mail. In other words, following the instructions below you will be able to receive in your account on the local machine the mail sent from the machine itself. This could sound silly, but it is useful if you want to receive the mail generated as automatic notification by various programs, most notably cron.
Disclaimer for the novice user: The steps described below do not interfere with the "mail-retrieving" ability of your mail reading software (MUA), which is probably based on IMAP or POP protocols. On the other hand, if you want to configure a stand-alone mail server on your local machine, i.e. if you want to be able to send email directly to the Internet, without relying on the mail server of your Internet provider, or if you want to be able to receive mail on your machine, again without POP or IMAP connection to your Internet provider mail server, this document is NOT for you. See HOWTO Email System for the Home Network for a guide on how to setup a mail server under Gentoo. See also the Linux Documentation Project Mail-User-HOWTO for a general introduction on mail under Linux.
This Tip is largely based on the discussion here.
[edit] Installing Postfix
The standard mail transport agent installed by Gentoo is mail-mta/ssmtp. So let's unmerge ssmtp and merge mail-mta/postfix instead
emerge --unmerge ssmtp emerge postfix
NOTE: enable mbox if you intend to use system mail spool (/var/spool/mail).
Once postfix is installed, you need to edit its main configuration file, /etc/postfix/main.cf , for few modifications. This file is rather long, and you must leave it essentially untouched. Below I show only the lines that should be modified together with their appropriate settings
| File: /etc/postfix/main.cf |
myhostname = localhost mydomain = localdomain inet_interfaces = $myhostname, localhost mydestination = $myhostname, localhost.$mydomain, localhost mynetworks_style = host |
The first two lines set the name and domain of the mail system to special names. The variable inet_interfaces lists the network interface addresses on which mail are received. The variable mydestination specifies the list of domains for which the machine considers itself the final destination. Modify all these lines to read exactly as above.
Also add the following setting to /etc/postfix/main.cf to disable the delivery of non-local mail
| File: /etc/postfix/main.cf |
default_transport = error:outside mail is not deliverable |
The variable default_transport specifies which transport is used to deliver non-local mail (by default smtp). With this setting, any outside mail will bounce back with an error instead of being stuck in the mail queue forever.
In some old postfix ebuild (notably version 2.2.5) the main.cf file seems
also to require this extra line
| File: /etc/postfix/main.cf |
unknown_local_recipient_reject_code = 450 |
but this is no longer necessary with the last ebuild (2.2.10).
As a final step, you have to decide where to store the received mail and set accordingly the home_mailbox variable in /etc/postfix/main.cf . Your choice depends on the format you use to store your mail. In the current ebuild (2.2.10) home_mailbox is set twice in the default /etc/postfix/main.cf , once in the main body of the file (where is it commented out) and once on the last line. You should fix this so that the value you set does not get overridden by the second one.
Note that is generally acceptable to not specify a home_mailbox if you just want postfix to use the system mail spool. Some MUAs, such as mail-client/mailx expect to find new mail in the system mail spool and will not notice new mail if it is delivered to a home mailbox.
[edit] Mbox Format
For the mbox format do
| File: /etc/postfix/main.cf for mbox |
home_mailbox = Mailbox |
to store all the received mail in the single file Mailbox in your home directory.
[edit] Maildir Format
If you prefer a maildir format, do
| File: /etc/postfix/main.cf for maildir |
home_mailbox = Maildir/ |
to store messages in the subdirectory Maildir of your home directory. Notice the trailing / that tells postfix to deliver mail to a maildir local storage.
[edit] MH Format
If you prefer MH format to store your mail (for instance, if your are a sylpheed user, like me) you don't need to add anything to main.cf. Instead, you need some external help, since postfix does not directly manage this format. Emerge the powerful set of utilities in mail-client/nmh
emerge nmh
and use the command (as regular user)
install-mh
to properly configure the package. This command creates the file .mh_profile in your home directory which defines the subdirectory where the received mails should be stored. Let's assume you have chosen Mail (which is the mail-client/nmh default) as the directory for your mail. Check the position of the rcvstore program on your system with
which rcvstore
On Gentoo it should be /usr/bin/rcvstore, but in different Linux flavors you can obtain different results. Assuming the returned path is the Gentoo default, create the file .forward in your home directory containing the single line
| File: ~/.forward for MH |
| /usr/bin/rcvstore |
In this way postfix will pass an incoming mail automatically to rcvstore that, in turns, will store it in ~/Mail/inbox. If you want your message stored in a different folder, let's say in ~/Mail/myfolder, use
| File: ~/.forward for MH different folder |
| "/usr/bin/rcvstore +myfolder" |
[edit] Testing the Configuration and Starting the Service
When the configuration is complete you can check if everything is set properly using
postfix upgrade-configuration postfix check
Also don't forget to run
newaliases
even if you didn't modify /etc/mail/aliases, as Postfix will not operate properly without the alias database /etc/mail/aliases.db.
If everything is OK do not forget to start the service
/etc/init.d/postfix start
and add it to the default runlevel
rc-update add postfix default
As a final check, try to send an email to yourself. For instance, using the mailto program distributed in net-mail/metamail do
mailto username
or
mailto username@localhost.localdomain
where username is your user name.
[edit] Mail Aliases
If you don't want to become root in order to check the mail automatically delivered to you by the various programs (for instance by cron) it would be a good idea to make your aliases in /etc/mail/aliases. Edit this file and set the root and operator according to
| File: /etc/mail/aliases |
root: username operator: username |
After, do not forget to update the alias database with
newaliases
[edit] Mail checking script
This will notify you of new system mail every time you open up an interactive terminal if you're using maildir format
echo 'if [ -d "${HOME}/.maildir/new" ];\
then NEWMAIL=`find ${HOME}/.maildir/new/ -type f`;\
if [ ! -z "${NEWMAIL}" ];\
then echo "You have new mail. Read it with 'mutt'.";\
fi;\
fi' >> /etc/skel/.bash_profile
Or you if you use bash :
echo "MAILCHECK=30" >> ~/.bashrc echo 'MAILPATH=~/.maildir/new?"You have a new mail. Read it with 'mutt'."' >> ~/.bashrc
For more explanation : man bash
