Sending Email with PHP Networking - Sending Mail using the PEAR::Mail (
Page 5 of 5 )
While using PHP's mail() function makes it relatively easy for us to send mail, it lacks certain things. One of these is the validation of an email address. In other words it does not check to see if an email address is valid before sending mail.
This particular functionality is incorporated into PEAR's mail class. To use this class you need to have PEAR installed and you also need to download the mail package. If this package is already in your pear directory, you simply run the following command in your shell or DOS prompt:
pear install mail
If the installation was successful you will get an "installation OK" message. You can also verify if the package was installed by running the following command:
pear list
This will give you a list of all the installed packages. It looks something like this:
Now you can use the class like so:
<?php
require "Mail.php";
$to = array("nita@mysite.com");
$headers = array(
'From' => "duimpie@hissite.com",
'Cc' => "xuros@siteways.com",
'Subject' => "Using PEAR::Mail to send email"
);
$body = <<< BODY
Hi Nita,
Just testing how to send email from a PHP script with the PEAR::Mail class.
Your eager to learn friend
Duimpie
BODY;
$mail = Mail::factory('mail');
$mail->send($to, $headers, $body);
?>
The code above simply sends a plain text message with headers. It uses the built-in mail() function to send the message. Notice the line highlighted in red, particularly the word factory. The PEAR::Mail class has multiple factories to choose from (mail, sendmail, and smtp). Those of you who use object-oriented programming will know that it uses factories to load and implement different technologies with the same application programming interfaces (APIs). In the case of the Mail() class, you can load the class elements needed to communicate with the mail server when the Mail object is created. The elements do the following:
-
The mail factory - uses the built-in mail() function to send email.
-
Sendmail factory - initiates communication directly with the sendmail program.
-
Smtp factory - uses sockets to connect directly to the server. It also supports authentication, which of course allows for secure communication.
For more complicated mail messages, PEAR provides a Multipurpose Internet Mail Extension (or MIME as is commonly known) class that allows you to build HTML mail messages. The class itself is called PEAR::Mail_mime and is used like so:
<?php
require "Mail.php";
require "Mail/mime.php";
$to = array("nita@mysite.com");
$headers = array(
'From' => "duimpie@hissite.com",
'Cc' => "xuros@siteways.com",
'Subject' => "Using PEAR::Mail to send email"
);
$mime = new Mail_mime();
$txtBody = <<< BODY
Hi Nita,
Just testing how to send email from a PHP script with the PEAR::Mail class.
Your eager to learn friend
Duimpie
BODY;
$mime->setTXTBody($txtBody);
$htmlBody = <<< BODY
<html><body>
<h1>Hi Nita,</h1><br>
Just testing how to send email from a PHP script with the PEAR::Mail class.
Your eager to learn friend
Duimpie
<br>
</body></html>
BODY;
$mime->setHTMLBody($htmlBody);?>
$body = $mime->get();
$headers = $mime->headers($headers);
$mail = Mail::factory('mail');
$mail->send($to, $headers, $body);
The code above has the same basic structure as the previous example, except for the HTML formatting. It also has a plain text version. This is so that your message is sent through even if the receiving server does not accept HTML messages.