Tutorial: Set up Sendmail, dovecot, SpamAssassin, ClamAV as a email server

last updated in Categories Howto, Linux, Mail server, RedHat/Fedora Linux, Suse Linux, Ubuntu Linux

This article will guide you about setting up your own home email server without being spammed to death.

There are many reasons to host your own email. Perhaps you don’t like the limits placed on you by your current ISP. Maybe they aren’t willing to host the domain you want, or give you the access you want. And if they do fit your needs, they want to charge a small fortune. Maybe you want complete privacy. Or perhaps you just want to access your email from anywhere using a web-based frontend. The list goes on and on…

There are several many ways to accomplish this task. Everyone has their preferred MTA program, but for the purposes of this article, we’ll use sendmail. The same can also be done using postfix, or exim. Each approach has its own merits.

This how to covers installation and configuration of following services/servers:

  • dovecot IMAP/POP3 server
  • sendmail MTA
  • sendmail-cf (M4 configuration)
  • squirrelmail (web based email client)
  • perl / gcc for Anti spam
  • Anti spam and Anti virus configuration – MailScanner, SpamAssassin, and ClamAV

How to set up a home email server (without being spammed to death) [redhat.com]

PHP Send Email Using Authenticated SMTP Mail Server In Real Time

last updated in Categories Apache, Howto, lighttpd, Mail server, php, Postfix, Security, Troubleshooting

PHP has mail() function to send an email to users. However this mail() will not work:

=> If sendmail (or compatible binary) is not installed

=> If Apache Web server / Lighttpd running in chrooted jail

=> And your smtp server needs an authentication before sending an email

=> Or you just need to send email using PHP PEAR

In all these cases you need to use PHP PEAR’s Mail:: interface. It defines the interface for implementing mailers under the PEAR hierarchy, and provides supporting functions which are useful in multiple mailer backends. In this tip you will learn about how to send an e-mail directly to client smtp server in real time.

PHP Pear’s Mail.php is located in /usr/share/pear/ directory. Following is sample code to send an email via authenticated smtp server.

PHP send email using PHP SMTP mail Pear functions – Sample source code

Following code is well commented, you need to make necessary changes as per your setup.

/* mail setup recipients, subject etc */
$recipients = "feedback@yourdot.com";
$headers["From"] = "user@somewhere.com";
$headers["To"] = "feedback@yourdot.com";
$headers["Subject"] = "User feedback";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.mycorp.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtpusername";
$smtpinfo["password"] = "smtpPassword";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);

Sending smtp email from chrooted Apache or Lighttpd webserver

Read following section, if you are running a secure chrooted Apache or Lighttpd web server. I have already written about setting php mail() function in chrooted jail. If you are using chrooted jail server setup, copy all files from /usr/share/pear directory to /chroot-directory/usr/share/pear directory. For example if lighttpd chrooted jail located in /webroot directory, you need to type following commands to install PHP pear support:
# mkdir -p /webroot/usr/share/pear
# cd /webroot/usr/share/pear
# cp -avr /usr/share/pear .

If PHP SAFE MODE is on, you must set /webroot/usr/share/pear directory permission to webserver username to allow access. Otherwise you will see error as follows:

1-Nov-2006 09:43:19] PHP Warning:  main(): SAFE MODE Restriction in effect.  The script whose uid is 506 is not allowed to access /usr/share/pear/PEAR.php owned by uid 0 in /usr/share/pear/Mail.php on line 636.

So if webserver username is lighttpd or apache use following command to setup correct ownership:
# chown lighttpd:lighttpd /webroot/usr/share/pear -ROR# chown apache:apache /webroot/usr/share/pear -R

You may also find modified wordpress WP-ContactForm plugin useful. It is a drop in form for users to contact you. It can be implemented on a page or a post. Original authored by Ryan Duff, which use php mail() function to send email. I have modified the same to send email via my ISP authenticated gateway using PHP PEAR’s Mail:: interface 😀

How to remove unwanted mails from UNIX mailboxes or folders

last updated in Categories Howto, Linux, Mail server, RedHat/Fedora Linux, Suse Linux, Ubuntu Linux, UNIX

We have already documented procedure for removing all emails from POP3 using a shell script.

But what about IMAP based mailboxes? There is cleanup-maildir python script. It It cleans up old messages in FOLDERNAME; the exact action taken depends on COMMAND.

From the article:
Maildir saves each mail in a separate file, it is much easier than it used to be to manipulate the mails. Everyone can write some simple script to do some cleanup based on its needs. A while ago I have stumbled across this python script that does most of the things I needed to cleanup maildir folders.

Now, even if Maildir is much faster for many emails kept on the server, when we reach a huge number of files in a single folder the access times to that folder will be considerably slower. Now I am not talking here about a couple of hundreds of mails, but some huge mailboxes with thousands of mails and huge sizes (over 3-5GB in size). You will be amazed that there are peoples that will do that? They are most probably using IMAP and keeping all their mails on the server, or even POP3 and saving one copy of each mail on the server.

Sendmail masquerade outgoing email address

last updated in Categories FreeBSD, Linux, Mail server, Security, Tuning, UNIX

If you just need an outgoing email from web server use sendmail. But what is masquerading?

With the help of masquerading your outgoing email appers from user@cyberciti.biz instead of realunixuser@server01.cyberciti.biz. This will also hide your internal user name or host name from rest of the world.

So this feature rewrites the hostname in the address of outgoing mail. This is also used when you have centralized mail server i.e. mail hub.

Sendmail masquerading configuration

Open your sendmail config file /etc/mail/sendmail.mc:
# vi /etc/mail/sendmail.mc
Append/add/modify the lines as follows:

Save and close the file. Replace domain name cyberciti.biz with your actual domain name. Update and restat sendmail server:
# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
# /etc/init.d/sendmail restart

Further reading

=> Masquerading and relaying