PHP
  Home arrow PHP arrow Page 5 - Sending Email with PHP Networking
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Sending Email with PHP Networking
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-09-15


    Table of Contents:
  • Sending Email with PHP Networking
  • Sending mail with PHP
  • Application Code
  • Code Examined
  • Sending Mail using the PEAR::Mail

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT