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.







October 21st, 2007 at 4:31 pm
[...] I’ve got another post where you can learn more on how to send emails with PHP. [...]