Filed under Web Development

Web developers often face the need of automating the sending of emails through their scripts. Of course, there is the possibility of using the PHP mail() function. While using this function for sending a basic email (i.e. one recipient, a subject and a message) is pretty easy, pushing the envelope (i.e. multiple recipients, attachments, etc.) may require a lot more coding.

Here’s the basic mail() function syntax:

mail ( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters]] )

A good knowledge of mail headers is required in order to achieve advanced results. Fortunately there are easier ways of sending emails with PHP.

Introducing The PHPMailer Class

PHPMailer is a full featured email transfer class for PHP. Here are a few features:

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

How To Use PHPMailer

First get your copy of PHPMailer from SourceForge or you can get it here too. Extract the phpmailer folder from the archive and upload it to the root of your website. You can delete the following files / folders:

  • /phpmailer/test
  • /phpmailer/ChangeLog.txt
  • /phpmailer/docs
  • /phpmailer/phpdoc
  • /phpmailer/LICENCE
  • /phpmailer/README

Now let’s create a php file named sendemail.php at the root of your website. At this point you should have a file / folder structure like this:

  • http://www.yourwebsite.com/phpmailer
  • http://www.yourwebsite.com/sendemail.php

Here’s the content of sendemail.php:

<?
error_reporting(E_ALL);

require(’phpmailer/class.phpmailer.php’);

$mail = new PHPMailer();

$mail->Host = “localhost”;
$mail->Mailer = “smtp”;

$mail->From = “my@email.com”;
$mail->FromName = “Bill Gate”;
$mail->Subject = “How are you?”;

$htmlmsg = “Hey there,<br><br>What have you been up to lately?<br>Waiting for your news!<br><br>” .
  ”See you soon,<br>Your friend”;
$txtmsg = str_replace(”<br>”, “\n”, $htmlmsg);

$mail->Body = $htmlmsg;
$mail->AltBody = $txtmsg;

$mail->AddAddress(myfriend@hotmail.com, “Joe”);
$mail->AddAddress(”myotherfriend@gmail.com”, “Jack”);

if($mail->Send())
 echo “Email sent successfully”;
else
 echo “Email has not been sent”;
?>

Of course, there is a lot more you can do with PHPMailer like embedding images, sending attachments, etc. Have a look at “docs\extending.html” from the PHPMailer archive.


Related Posts

Comments (1) Posted by Stephane on Friday, October 12th, 2007


You can follow any responses to this entry through the magic of "RSS 2.0" and leave a trackback from your own site.

One Response to “How To Send Emails With PHP”

Post A Comment