PHPMailer

Have you ever tried to use the mail function in PHP and it doesn’t work? Chances are the sendmail on the host server is not configured correctly. Maybe you may need to play around with PHP.INI to make it work. There is an easy workaround to make your mail function work without tinkering with sendmail. Try PHPMailer.

PHPMailer is a PHP class for PHP (www.php.net) that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments. PHPMailer supports nearly all possiblities to send email: mail(), Sendmail, qmail & direct to SMTP server. You can use any feature of SMTP-based e-mail, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send e-mail within PHP.

The key feature of PHPMailer is having the ability to use the mail function with a smtp server. There several features worth noting such as the ability to send an HTML email, CC and BCC recipients, embedded images, attachments, custom mail headers, redundant SMTP servers, SMTP authentication, wordwrap, etc.

A sample PHP mail function may look like this:

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
           'Reply-To: webmaster@example.com' . "\r\n" .
mail($to, $subject, $message, $headers);

In PHPMailer, you would write something like this using SMTP server:

require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.host.com";
$mail->From = "donotreply@host.com";
$mail->FromName = "Your name";
$mail->AddAddress("someone@domain.com", "John Doe");
$mail->Subject = "Hello there";
$mail->Body = "Hi, just wanted to drop by and say hi." ;
if(!$mail->Send()) {
  echo "Message could not be sent.";
} else {
  echo "Message successful";
}

Some smtp servers may require authentication. You may need to add these lines:

$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "password";

PHPMailer is an excellent and simple replacement for the PHP mail function. It’s easy and it works. Give PHPMailer a try.

3 thoughts on “PHPMailer

  1. You may want to check Swift mailer for PHP (http://www.swiftmailer.org/). It looks like it is more powerful than the class you are describing including few connection handlers like direct SMTP protocol use to contact the server. Hope that helps!

  2. Lukaszp, I was poking arond the swiftmailer.org website just now. Yes, swiftmailer has more features. PHPMailer was a good find. This might be better. Great tip. Thank you. Hopefully, I can use it on my next project. Cheers.

Comments are closed.