Method: Using PHPMailer library

Download the PHPMailer library from here, https://github.com/Synchro/PHPMailer. It is free, easy to use library that supports sending e-mail using secure connection directly form SMTP. If you run you own virtual or dedicated server and you are not into setting up mailserver application, this is the right PHP library for you.

This library works perfectly with plaint text and HTML or you may create HTML mail and insert an plain text alternative to support older mail clients with no HTML support.

Example, if you have your website on http://www.example.com/ and your document root is /var/www/example.com, upload the phpmailer directory up to /var/www/example.com/scripts/phpmailer. 

TIP

Do not forget to escape your $_POST and $_GET variables, always put e-mailing scripts into if statement or at least use some protection to prevent “hackers” from using your page with script mailer as a e-mail spam gateway. 

require_once('scripts/phpmailer/class.phpmailer.php');

$mail = new PHPMailer();

$mail->IsSMTP();                       // telling the class to use SMTP

$mail->SMTPDebug = 0;                  
// 0 = no output, 1 = errors and messages, 2 = messages only.

$mail->SMTPAuth = true;                // enable SMTP authentication
$mail->SMTPSecure = "tls";              // sets the prefix to the servier
$mail->Host = "smtp.gmail.com";        // sets Gmail as the SMTP server
$mail->Port = 587;                     // set the SMTP port for the GMAIL

$mail->Username = "info@example.com";  // Gmail username
$mail->Password = "yourpassword";      // Gmail password

$mail->CharSet = 'windows-1250';
$mail->SetFrom ('info@example.com', 'Example.com Information');
$mail->AddBCC ( 'sales@example.com', 'Example.com Sales Dep.');
$mail->Subject = $subject;
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);

$mail->Body = $body_of_your_email; 
// you may also use $mail->Body = file_get_contents('your_mail_template.html');

$mail->AddAddress ('your.recipient@domain.com', 'Recipients Name');     
// you may also use this format $mail->AddAddress ($recipient);

if(!$mail->Send())
{
        $error_message = "Mailer Error: " . $mail->ErrorInfo;
} else 
{
        $error_message = "Successfully sent!";
}

// You may delete or alter these last lines reporting error messages, but beware, that if you delete the $mail->Send() part, the e-mail will not be sent, because that is the part of this code, that actually sends the e-mail. 

HTML vs. PLAIN TEXT

This above example is to send plain text e-mail. 

To send plain text, here are few tips:

  1. Do not use $mail->MsgHTML($body_of_your_email); instead use $mail->Body = $body_of_your_email; 
  2. Do not use $mail->AltBody = 'Your alternative message to HTML';
  3. Set $mail->IsHTML(false);

To send HTML e-mail, here are few tips:

  1. Of course set $mail->IsHTML(true); or you don’t have to set this variable at all, by default it is set to true.
  2. For e-mail body use only MsgHTML, for example: $mail->MsgHTML(file_get_contents('your_mail_template.html'));, do not use $mail->Body = $body_of_your_email; 

Other TIPs

You may use optional variables like:

Why use PHPMailer

I decided to use PHPMailer and not to install mailserver like Postfix, because of security issue, SPAM fighting and also this is a time saver, because I use Google Apps, I set up my Google Apps account to automatically sign e-mails with DKIM, I setup SPF DNS record and the best of all, as I host multiple sites as Virtual Hosts on my web server, I also want to sometimes use different e-mail accounts on different parts of my website, for example part of my website uses Amazon AWS SES, part of my website uses my company SMTP server and part uses Google Apps e-mail accounts, all set in PHP code, nothing to setup on server side.