In PHP on Microsoft Windows you have to simply configure two parameters to enable sending mails through SMTP server. In Unix / Linux it is slightly more complicated. The solution, however, is much more powerful and works also on Windows. Let's first start with Windows.

In Microsoft Windows PHP installation you just have to change two variables in php.ini:

SMTP = smtp.server.com
smtp_port = 25

Replace smtp.server.com with your SMTP server name and 25 with your SMTP server port (normally 25).

You can also set the default sender information in Windows:
sendmail_from = me@example.com

On Linux / Unix PHP relies on sendmail. You can specify the sendmail path here:
sendmail_path = /usr/sbin/sendmail -t -i

Unfortunately this doesn't work too well if your SMTP server is configured on a different machine or you are not using sendmail.

PHPMailerFortunately there is a much better solution in PHPMailer. The default mail capability provided by mail() function in PHP is very limited. PHPMailer is a full fledged mail API which can be used to do any kind of mailing tasks.

Features of PHPMailer

  1. Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
  2. Redundant SMTP servers
  3. Multipart/alternative emails for mail clients that do not read HTML email
  4. Support for 8bit, base64, binary, and quoted-printable encoding
  5. Uses the same methods as the very popular AspEmail active server (COM) component
  6. SMTP authentication
  7. Word wrap
  8. Address reset functions
  9. HTML email
  10. Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc
  11. Works on any platform
  12. Flexible debugging
  13. Custom mail headers
  14. Multiple fs, string, and binary attachments (those from database, string, etc)
  15. Embedded image support

How to use PHPMailer
To use PHPMailer you need to first download the files and save the relevant files (upload) on your server.

You need to upload class.phpmailer.php, class.smtp.php (for SMTP support) and language/phpmailer.lang-en.php. You should download the lang file corresponding to the language of your blog. For my english language sites I use language/phpmailer.lang-en.php, where en is the language code.

In your PHP file include class.phpmailer.php as follows:

if(!class_exists('PHPMailer')) {
    require(BASEPATH . '/class.phpmailer.php');
}

Replace BASEPATH with the actual path of class-phpmailer.php file. You may also define BASEPATH to achieve the same result (preferred).

Note: This check ensures only one copy of the class is loaded. This in turn loads other required classes.

Now you can send a mail using any SMTP server. Here is a simple example:

$mail = new PHPMailer();

$mail->From     = $senderemail;
$mail->FromName = $sendername;
$mail->AddAddress($receiveremail, $receivername);
// Fill in Username and Password for servers requiring authentication
$mail->Username = $smtp_username;
$mail->Password = $smtp_password;

// SMTP server name
$mail->Host     = $smtp_server;
$mail->Mailer   = "smtp";

$mail->Subject = $mail_subject;
$mail->Body    = $mail_body;

if(!$mail->Send()) $results = 'Error message';
else $results = 'Success message';

You can read the documentation, advanced example or the tutorial if you need further help.