You need to login to send post. No account yet? Create one:
Create account
Setup localhost mailserver to catch all outgoing mail using Postfix & Courier
Wed, 03/09/2011 - 14:35
Almost all PHP applications and webs are sending e-mails (passwords to new users, system events, cron job confirmations etc.). As a developer you need to read them, and identically as for Windows development workstation, there is no good way to let them actually send and deliver. You need catch them all, despite if recipient's e-mail address exists or you just fabricated this one for testing purpose.
As we don't need complex e-mail server, let use (relatively) easy to setup Postfix.
Install Postfix & Courier
- Use Yast to install Postfix
Unfortunatelly Postfix doesn't support IMAP or POP3 protocols which are used by majority of e-mail clients, so is worth to install Courier too:
- Install Courier using Yast
(If you are planning use Evolution e-mail client, you can skip Courier installation as it can use Postfix's Maildir-style mailboxes.)
- Still in the Yast go to System Services (Runlevel) and start postfix, courier-pop and courier-imap
Now create user account, which will get all the mails:
sudo useradd -m -s /bin/bash ubermail sudo passwd ubermail
Before next steps check Postfix's status:
sudo postfix status
Optionally you can test all functions together:
- Send email to ubermail
telnet localhost 25 ehlo localhost mail from: root@localhost rcpt to: ubermail@localhost data Subject: Testmail
Test,
I wonder if that works?
.
quit
- Then login as ubermail and check mail:
su - ubermail mail
Setup Postfix & Courier
There are 2 tasks to do: catch all emails sent by local Apache and redirect them all to one e-mail account. Therefore we need edit 2 different files. First is main Postfix config in file /etc/postfix/main.cf. Edit/Add:
virtual_alias_maps = regexp:/etc/postfix/virtual
virtual_alias_domains =
myhostname = your hostname (type hostname in console and use result here)
mydomain = localhost
inet_interfaces = all
relayhost =
mydestination = localhost, $myhostname, $myhostname.$mydomain
myorigin = $myhostname
home_mailbox = Maildir/
mailbox_command =
inet_interfaces = all
inet_protocols = all
virtual_alias_domains =
myhostname = your hostname (type hostname in console and use result here)
mydomain = localhost
inet_interfaces = all
relayhost =
mydestination = localhost, $myhostname, $myhostname.$mydomain
myorigin = $myhostname
home_mailbox = Maildir/
mailbox_command =
inet_interfaces = all
inet_protocols = all
The second is /etc/postfix/virtual: add at the end (substitute “hostname” by real hostname of your computer as in previous file):
/.*/ ubermail@hostname.localhost
Now is needed just rebuilding of the map file:
sudo postmap /etc/postfix/virtual
And restart Postfix:
sudo /etc/rc.d/postfix restart
Testing
Create php file with content:
<?php
$to = "someone@never.where";
$subject = "It works!";
$body = "What,\n\ndo you think?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
$to = "someone@never.where";
$subject = "It works!";
$body = "What,\n\ndo you think?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
and save it to /svr/www/htdocs/mail.php On web address http://localhost/mail.php you now should see “Message successfully sent!” and user ubermail should have new e-mail!
Previous chapter: How to setup wildcard subdomains support on linux localhost using Dnsmasq


















