PHP
  Home arrow PHP arrow Page 4 - 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 - Code Examined
    ( Page 4 of 5 )

    Let's take a look at the code. First we check to see if the form was submitted:


    <?

    //is form submitted

    if(isset($_POST['key'])){


     

    Then we collect the form data and store it in shorter variables:


    //collect the information

    $from=$email;

    $cc=$_POST['cc'];

    $bcc=$_POST['bcc'];


     

    The next step is to filter the data and set the error message for each of the required fields:


    if(empty($_POST['tos'])){

    $error=true;

    }else{

    $to=$_POST['tos'];

    }

    if(empty($_POST['sub'])){

    $error=true;

    }else{

    $subject=$_POST['sub'];

    }

    if(empty($_POST['msg'])){

    $error=true;

    }else{

    $msg=$_POST['msg'];

    }



    Finally we test to see if the user wants to send an attachment. The attachment is sent through the $_FILES[''] array. If the file is loaded, we set the headers for the mail function. Notice that the content-disposition and content-transfer-encoding has also been set:


    //check if the an attachment is present

    if(isset($_FILES['userfile']['name'])){

    $attachment = $_FILES['userfile']['tmp_name'], $_FILES['userfile']['name'];

    $headers ="Content-disposition: attachment;

    $filename=.$attachment."n";

    $headers=.Content-Transfer-Encoding: base64n";

    }




    If there are no errors, the code continues to send the email message with or without the headers:


    if(!$error){

    if($headers <> ""){

    $res=mail($to,$subject,$msg,$headers);

    }else{

    $res=mail($to,$subject,$msg);

    }

    }



    Finally, an error check is made to inform the user if any errors occurred.



    if(!$res){

    echo "Mail error occurred";

    }

    }


    ?>




     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT