Hudzilla.org - the homepage of Paul Hudson
Contents > Networks > Sending mail: mail() Wish List | Report Bug | About Me ]

15.5.2     Easier mail sending with PEAR::Mail

This is NOT the latest copy of this book; click here for the latest version.

Although mail() sends email well enough, I want to introduce you to the much more advanced PEAR::Mail class. This does much the same as using mail(), with the exception that it has slightly smarter address and header handling - rather than taking a string of "header: value\r\nheader: value", it takes an array where the keys are the header names and the values are the header values. Similarly, you can send the same email to lots of people by passing an array of email addresses rather than just one.

Using PEAR::Mail we can write a simple email script like this:

<?php
    
include('Mail.php');
    
$mail = Mail::factory("mail");

    
$headers = array("From"=>"me@example.com", "Subject"=>"Test Mail");
    
$body = "This is a test!";
    
$mail->send("best@friend.com", $headers, $body);
?>

The Mail.php file is the PEAR::Mail script, so it needs to be included before any PEAR::Mail functions are used. Line two creates a default instance of PEAR::Mail - the parameter "mail" is passed in so that PEAR::Mail will use PHP's mail() function to send the email. If you pass in "sendmail", it will send direct via the sendmail program (Unix only).

Alternatively you can pass in "smtp", which lets you send a second parameter that is an array containing five keys: host, port, auth, username, and password. Each of these should have values assigned to them: host should be the SMTP server to connect to, port should be the port number (defaults to 25), auth should be true if you want to authenticate with username and password (defaults to false), and username and password should be set if you want to authenticate. Unless you really want the extra power of connecting directly by hand, it's best to stick with "mail"!

Line three sets up the headers to use in the email. Note that this time we need to provide the subject inside a header as well as the sender information. Here you can use all the techniques we have looked at so far, for example the From element could have the value "Me <me@example.com>" to have the email addresses pretty-printed.

Line four sets the body text to use in the email, which is standard enough. Line five is where the email is actually sent, and you will see that send() takes three parameters: address to send to, headers to use, and the content of the email. As mentioned already, the first parameter can either be a string with each person's name separated by a comma, or it can be an array.





<< 15.5.1 MIME types: mime_content_type()   15.5.3 Sending mixed-type messages with PEAR::Mail_Mime >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Dec 2008

FUCKIN FILTERS!!!....
go to suck!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

A PHP User - 05 Dec 2008

<script>alert:("HI!")</script>

A PHP User - 05 Dec 2008

<script>alert:("HI!")</script>

Raquel /raquesita@gmail.com - 05 Dec 2008

Copia el archivo.tar en C:\wamp\php5 luego por línea de comandos debes instalar el paquete q bajaste. Ejemplo:

C:\wamp\php5\pear install "package" ENTER

C:\wamp\php5\pear install "DB-1.7.6.tar"

Listo Una vez instaladas todas las librerías y paquetes das reiniciar servicio. Si quieres ver todos los comandos coloca en la línea de comandos pear.bat

noam - 05 Dec 2008

where can i download this PEAR::Mail stuff?
in the pair website they have a .tar file that contains one text file with xml and multiple php scripts together in one file, and you're supposed to spererate the code into different files by yourself..

and i'm probably doing it wrong...

is there anywhere you can download the "FOLDER" with the included "FILES" and simply upload it to your server?

thanks

A PHP User - 05 Dec 2008

I want to learn more about PHP mail



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow