Home arrow PHP arrow Page 5 - Sending Email with PHP Networking

Sending Mail using the PEAR::Mail - PHP

In this article we will look at the protocol that is involved in sending email messages. We will also examine the thorny issue of how to send an attachment with an email message. This article is the second of two parts.

TABLE OF CONTENTS:
  1. Sending Email with PHP Networking
  2. Sending mail with PHP
  3. Application Code
  4. Code Examined
  5. Sending Mail using the PEAR::Mail
By: David Web
Rating: starstarstarstarstar / 3
September 15, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By David Web
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: