Introduction
Sending the emails using the PHP mail () function is simple, as follows:
Just a single line of code, how simple it is, right! But, the problem with PHP mail function is, its lack of flexibility and it doesn’t support SMTP authentication or SSL encryption.
Fortunately, for most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server, too. For enhanced security, secure SSL connections are supported.
PEAR stands for PHP Extension and Application Repository; this is a collection of PHP reusable classes.
In this tutorial I will try to explain the following things:
- Install PEAR on the windows
- Install PEAR on linux/unix
- Send emails using PEAR: MAIL
- Send emails using SMTP authentication
- Send emails using the SMTP authentication and SSL encryption
- Sending mixed-type messages with PEAR::Mail_Mime
Install PEAR on the windows
PEAR is delivered with PHP on the windows and Linux platforms. Below steps will help you to install the PEAR package on the windows system.
In your PHP installation directory, if you look carefully you will find a subdirectory named PEAR and a script called go-pear.bat is found. Execute this script from a DOS window, and note the following results:
D:\PHP>go-pear
-
Welcome to go-pear!
-
-
……………..
-
-
……….
-
-
Go-pear.bat script will install the ‘pear’ command and all the files needed by it. This command is your tool for PEAR installation and maintenance and it also lets you download and install the PEAR packages bundled with PHP: DB, Net_Socket, Net_SMTP, Mail, XML_Parser, PHPUnit.
-
-
If you wish to abort, press Control-C now, or press Enter to continue:
After several more “press enter”, a warning appears that we did not take a look at the include directory path:
-
WARNING! The include_path defined in the currently used php.ini does not contain the PEAR PHP directory you just specified:
-
-
<D:\PHP\pear>
-
-
If the specified directory is also not in the include_path used by
-
-
your scripts, you will have problems getting any PEAR packages working.
-
-
Current include path : .;C:\php5\pear
-
-
Configured directory : D:\PHP\pear
-
-
Currently used php.ini (guess) :
-
-
Press Enter to continue:
When the enter button is pressed, another warning comes up:
-
The ‘pear’ command is not currently in your PATH, so you need to
-
-
use ‘D:\PHP\pear.bat’ until you have added
-
-
‘D:\PHP’ to your PATH environment variable
-
-
Run it without parameters to see the available actions, try ‘pear list’ to see what packages are installed, or ‘pear help’ for help.
-
-
For more information about PEAR, see:
-
-
http://pear.php.net/faq.php
-
-
http://cvs.php.net/co.php/pearweb/doc/pear_package_manager.txt?p=1 http://pear.php.net/manual/
-
-
Thanks for using go-pear!
-
-
WINDOWS ENVIRONMENT VARIABLES
-
-
For convenience, a REG file is available under D:\PHP\PEAR_ENV.reg.
-
-
-
-
Press any key to continue . . .
Pressing enter finishes the installation script. However, there are several more tasks to do:
- Edit PHP.ini and put (PHP installation directory) C:\PHP and C:\PHP\PEAR in the include path.
- Load C:\PHP\PEAR_ENV.reg into the registry.
- Put C:\PHP into the PATH.
The first step is the easiest one to perform and is completely analogous to Linux. By using a favorite text editor, the following line is put into PHP.ini:
include_path = “.;C:\PHP;C:\PHP\PEAR”
This, of course means that the Apache2 web server will once again need to be restarted.
Next, start windows explorer, locate the file C:\PHP\PEAR_ENV.reg and double-click it. This will load it into the registry. For changing the path, invoke regedit and go to HKEY_CURRENT_USER->Environment. Once there, add C:\PHP to the PATH variable.
If the variable PATH does not exist, it can be created. Now, restart Apache2 and the system, so that the installed registry variables take effect. Next time the user logs in, they can start the DOS window, type “pear” and view the well known menu, exactly the same as for Linux. Using the PEAR installer on Windows is completely identical to using it on Linux. Here is the output from the post-installation steps:
C:\>pear upgrade-all
-
Will upgrade archive_tar
-
-
Will upgrade pear
-
-
Will upgrade xml_rpc
-
-
downloading Archive_Tar-1.3.0.tgz …
-
-
Starting to download Archive_Tar-1.3.0.tgz (15,200 bytes)
-
-
…..done: 15,200 bytes
-
-
downloading PEAR-1.3.5.tgz …
-
Starting to download PEAR-1.3.5.tgz (108,423 bytes)
-
-
…done: 108,423 bytes
-
-
downloading XML_RPC-1.2.1.tgz …
-
Starting to download XML_RPC-1.2.1.tgz (21,837 bytes)
-
-
…done: 21,837 bytes
-
-
upgrade-all ok: Archive_Tar 1.3.0
-
-
upgrade-all ok: XML_RPC 1.2.1
-
-
upgrade-all ok: PEAR 1.3.5
Install PEAR on linux/unix
Installing PEAR on the linux is same except the only difference from Linux is that the PEAR DB module comes standard on Windows, yet it does not come standard on Linux.
Download the go-pear.php script from http://go-pear.org/.
Open a command line shell and run the script:
php go-pear.php
-
Welcome to go-pear!
-
-
Go-pear will install the ‘pear’ command and all the files needed by
-
-
it. This command is your tool for PEAR installation and maintenance.
-
-
Go-pear also lets you download and install the PEAR packages bundled
-
-
with PHP: MDB2.
-
-
If you wish to abort, press Control-C now, or press Enter to continue:
-
-
HTTP proxy (http://user:password@proxy.myhost.com:port), or Enter for none:: http://my-proxy-server.com:80
-
-
-
change individual locations, type the number in front of the
-
-
directory. Type ‘all’ to change all of them or simply press Enter to
-
-
accept these locations.
-
-
1. Installation prefix : /usr
-
-
2. Binaries directory : $prefix/bin
-
-
3. PHP code directory ($php_dir) : $prefix/share/pear
-
-
4. Documentation base directory : $php_dir/docs
-
-
5. Data base directory : $php_dir/data
-
-
6. Tests base directory : $php_dir/tests
-
-
7. Temporary files directory : $prefix/temp
-
-
1-7, ‘all’ or Enter to continue:
-
-
The following PEAR packages are bundled with PHP: MDB2.
Would you like to install these as well? [Y/n] : Y
-
Loading zlib: ok
-
-
Downloading package: PEAR-stable……ok
-
-
Downloading package: Archive_Tar-stable….ok
-
-
Downloading package: Console_Getopt-stable….ok
-
-
Downloading package: Structures_Graph-stable….ok
-
-
Bootstrapping: PEAR……………….(remote) ok
-
-
Bootstrapping: Archive_Tar…………(remote) ok
-
-
Bootstrapping: Console_Getopt………(remote) ok
-
-
Downloading package: MDB2………….ok
-
-
Extracting installer………………ok
-
-
warning: pear/PEAR requires package "pear/Archive_Tar" (version >= 1.3.1)
-
-
warning: pear/PEAR requires package "pear/Console_Getopt" (version >= 1.2)
-
-
warning: pear/PEAR requires package "pear/Structures_Graph" (version >= 1.0.2)
-
-
pear/PEAR can optionally use package "pear/XML_RPC" (version >= 1.4.0)
-
-
pear/PEAR can optionally use package "pear/PEAR_Frontend_Web" (version >= 0.5.0)pear/PEAR can optionally use package "pear/PEAR_Frontend_Gtk" (version >= 0.4.0)install ok: channel://pear.php.net/PEAR-1.5.4
-
-
install ok: channel://pear.php.net/Archive_Tar-1.3.2
-
-
install ok: channel://pear.php.net/Console_Getopt-1.2.2
-
-
install ok: channel://pear.php.net/Structures_Graph-1.0.2
-
-
install ok: channel://pear.php.net/MDB2-2.4.1
-
-
MDB2: Optional feature fbsql available (Frontbase SQL driver for MDB2)
-
-
MDB2: Optional feature ibase available (Interbase/Firebird driver for MDB2)
-
-
-
MDB2: Optional feature mysqli available (MySQLi driver for MDB2)
-
-
MDB2: Optional feature mssql available (MS SQL Server driver for MDB2)
-
-
MDB2: Optional feature oci8 available (Oracle driver for MDB2)
-
-
MDB2: Optional feature pgsql available (PostgreSQL driver for MDB2)
-
-
MDB2: Optional feature querysim available (Querysim driver for MDB2)
-
-
MDB2: Optional feature sqlite available (SQLite2 driver for MDB2)
-
-
To install use "pear install pear/MDB2#featurename"
-
-
The ‘pear’ command is now at your service at /usr/bin/pear
See the last message: “The ‘pear’ command is now at your service at /usr/bin/pear”. This means the Pear installation is completed and this command is your tool for PEAR installation and maintenance and it also lets you download and install the PEAR packages bundled with PHP: DB, Net_Socket, Net_SMTP, Mail, XML_Parser, PHPUnit.
The remaining tasks editing of php.ini file and upgrade procedures which I explained above in this article for windows, same applies for Linux also.
Send mails using PEAR: MAIL
First we will go thru simple example, it is just three to five lines but, believe me there are many interesting and secrets behind the PEAR: MAIL will be know to us. So, look at the example carefully:
-
-
<?php
-
include(‘Mail.php’);
-
-
-
$body = "This is a test to know the secerets of PEAR: MAIL!";
-
$mail->send("best@friend.com", $headers, $body);
-
?>
Most important things to consider as follows:
- The Mail.php file which is included in the first line of the above example, is the PEAR::Mail script. It needs to be included before any PEAR::Mail functions are used.
- Line two creates a default instance of PEAR::Mail (Mail:factory)
- The parameter “mail” is passed in Mail:: factory 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).
Send mails using SMTP authentication
See the below example, it tries to help you to send the mail from PHP using the SMTP authentication.
From the above example, you can see, we passed “mail” as argument to the Mail:: factory ( $mail = Mail::factory(“mail”);) . Here we pass in “smtp”, which lets you send a second parameter that is an array containing five keys:
- Host, the SMTP server to connect to
- Port, the port number (defaults to 25)
- Auth, should be true if you want to authenticate with username and password (defaults to false)
- Username, should be set if you want to authenticate
- Password. should be set if you want to authenticate
-
-
<?php
-
$to = "xyz ";
-
$subject = "Hi!";
-
$body = "Hi,\n\nHow are you?";
-
-
$host = "mail.example.com";
-
$username = "smtp_username";
-
$password = "smtp_password";
-
-
‘To’ => $to,
-
‘Subject’ => $subject);
-
-
‘auth’ => true,
-
‘username’ => $username,
-
‘password’ => $password));
-
-
$result = $mail->send($to, $headers, $body);
-
-
if (PEAR::isError($result)) {
-
-
" . $result->getMessage() . "
-
-
");
-
} else {
-
-
Message successfully sent!
-
-
");
-
}
-
-
?>
Send mail using the SMTP authentication and SSL encryption
The example you seen above is same applies here, except, here we specify address of the ssl host: ssl://mail.example.com and sending the request on port number 465 as shown in the below example:
-
-
<?php
-
$to = "xyz ";
-
$subject = "Hi!";
-
$body = "Hi,\n\nHow are you?";
-
-
$host = "ssl://mail.example.com";
-
$port = "465";
-
$username = "smtp_username";
-
$password = "smtp_password";
-
-
‘To’ => $to,
-
‘Subject’ => $subject);
-
-
‘port’ => $port,
-
‘auth’ => true,
-
‘username’ => $username,
-
‘password’ => $password));
-
-
$result = $mail->send($to, $headers, $body);
-
-
if (PEAR::isError($result)) {
-
-
" . $result->getMessage() . "
-
-
");
-
} else {
-
-
Message successfully sent!
-
-
");
-
}
-
?>
The code found in this example should be fairly straightforward, so I won’t elaborate on its contents, although it’s worth noting that in this particular case PHP’s OpenSSL extension must be enabled.
Sending mixed-type messages with PEAR::Mail_Mime
PEAR::Mail was just there to lay the groundwork for our use of attachments. You see, there is a very close cousin of PEAR::Mail called PEAR::Mail_Mime, and this has a number of features to make sending attachments with PEAR::Mail emails very easy.
See the example below:
-
-
<?php
-
Hello<hr />This is the HTML email.‘;
-
-
$message->setTXTBody($text);
-
$message->setHTMLBody($html);
-
-
$body = $message->get();
-
-
$extraheaders = array(’From‘ => ‘me@example.com‘,
-
’Subject‘ => ‘Subject‘);
-
-
$headers = $message->headers($extraheaders);
-
-
-
$mail->send(’best@example.com‘, $headers, $body);
-
?>
-
I will try to explain the example and its main considerations as follows:
- Some parts of that will look familiar, but the majority is new. This time we have to include both PEAR::Mail and PEAR::Mail_Mime, as it takes both classes to get the full email sent.
- We have text message and HTML message, in $text and $html variables
- Both are converted to message using the setTXTBody and setHTMLBody.
- $message->get() retrieves the full message text to send, and is a combination of the HTML and text information all encoded for sending over the Internet. If you want to see how the system works behind the scenes, echo out $body and have a look through.
- With the line starting, “$extraheaders = “, things start getting a little more complicated. Last time you saw that PEAR::Mail->send() takes its headers as an array, and, to accommodate this, PEAR::Mail_Mime also returns its headers as an array. You see, when sending complex emails, you need to have a special set of headers in there that tells the mail reader what to expect. So, once you have your content in place, you just call headers() to get the header information. As you still need to use the old headers (from, subject, etc), you can pass into headers() an array of existing headers, and it will add these to the array it returns.
For example, calling headers() on its own might return something like this:
-
-
["MIME-Version"]=>
-
string(3) "1.0"
-
["Content-Type"]=>
-
string(64) "multipart/mixed;
-
boundary="=_067d506611ba7a0da2b6106b54282d16""
-
}
-
However, passing our array $extraheaders in as the only parameter, headers() returns this:
-
-
array(4) {
-
["MIME-Version"]=>
-
string(3) "1.0"
-
["From"]=>
-
string(14) "me@example.com"
-
["Subject"]=>
-
string(12) "My Subject 7"
-
["Content-Type"]=>
-
string(64) "multipart/mixed;
-
boundary="=_307c199ae5303dac356d5cf48c89fc7c""
-
}
-
Note that the “boundary” string in Content-Type is randomised, so yours will be different.
-
- Once we have the complete list of headers, this is passed into the send() call at the end, which is otherwise unchanged. Now when the mail is received, mail readers should automatically pick the best format for them and display it.
Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you, with 7 Years of experience on different technologies. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please