Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Mail() From Start To Finish
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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? 
MISCELLANEOUS

Mail() From Start To Finish
By: Andrew Walsh
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2004-06-17

    Table of Contents:
  • Mail() From Start To Finish
  • Sending Html Emails
  • Sending Emails With Attachments:

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Mail() From Start To Finish - Sending Emails With Attachments:


    (Page 3 of 3 )

    So you have learnt how to send basic text emails, html emails, but now you want to send emails with attachments. The example below shows you how to send a email with an attachment using a form with a "file" field to browse your computer for a file to attach.

    <?php
    if(!isset($_REQUEST['submit'])){
    ?&
    gt;
    &
    lt;form action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;" method="post"
    enctype="multipart/form-data"&gt;
    &
    lt;table&gt;
    &
    lt;tr&gt;
    &
    lt;td&gt;To:&lt;/td&gt;
    &
    lt;td&gt;&lt;input type="text" name="to" size="40"/&gt;&lt;/td&gt;
    &
    lt;/tr&gt;
    &
    lt;tr&gt;
    &
    lt;td&gt;From:&lt;/td&gt;
    &
    lt;td&gt;&lt;input type="text" name="from" size="40" /&gt;&lt;/td&gt;
    &
    lt;/tr&gt;
    &
    lt;tr&gt;
    &
    lt;td&gt;Subject:&lt;/td&gt;
    &
    lt;td&gt;&lt;input type="text" name="re" size="40" /&gt;&lt;/td&gt;
    &
    lt;/tr&gt;
    &
    lt;tr&gt;
    &
    lt;td&gt;Message:&lt;/td&gt;
    &
    lt;td&gt;&lt;textarea cols="30" rows="5" name="comments"&gt;&lt;/textarea&gt;&lt;/td&gt;
    &
    lt;/tr&gt;
    &
    lt;tr&gt;
    &
    lt;td&gt;Attachment:&lt;/td&gt;
    &
    lt;td&gt;&lt;input type="file" name="att" size="26" /&gt;&lt;/td&gt;
    &
    lt;/tr&gt;
    &
    lt;td colspan="2"&gt;&lt;input type="submit" name="submit" value="Send Form" /&gt;&lt;/td&gt;
    &
    lt;/tr&gt;
    &
    lt;/table&gt;
    &
    lt;/form&gt;
    &
    lt;?php
    }else{
    extract($_POST);

            
    $fp fopen$att"r");

            
    $file fread$fp$att_size );

            
    /*
    Encode The Data For Transition using base64_encode();
    And get a 32-character hexadecimal number
    */
            
    $file chunk_split(base64_encode($file));
            
    $num md5time() );

            
    /*
    Define the main message headers
    */
            
    $hdr  "From:".$_REQUEST['from']."\r\n";
            
    $hdr .= "MIME-Version: 1.0\r\n";
            
    $hdr .= "Content-Type: multipart/mixed; ";
            
    $hdr .= "boundary=".$num."\r\n";
            
    $hdr .= "--$num\r\n";
    /*
    Define message section
    */
            
    $hdr .= "Content-Type: text/plain\r\n";
            
    $hdr .= "Content-Transfer-Encoding: 8bit\r\n\n";
            
    $hdr .= "".$_REQUEST['comments']."\r\n";
            
    $hdr .= "--".$num."\n";

            
    /*
    Define the attachment section
    */
            
    $hdr .= "Content-Type:"$att_type." ";
            
    $hdr .= "name="".$att_name.""r\n";
            
    $hdr .= "Content-Transfer-Encoding: base64\r\n";
            
    $hdr .= "Content-Disposition: attachment; ";
            
    $hdr .= "filename="".$att_name.""\r\n\n";
            
    $hdr .= "".$file."\r\n";
            
    $hdr .= "--".$num."--";

            
    /*
    Send the email
    */
            
    mail$_REQUEST['to'], $_REQUEST['re'], $_REQUEST['comments'], $hdr);
            
            
    /*
    Close the attachment
    */
            
    fclose$fp );
    echo 
    "Mail sent...";
    }
    ?>

    Note: I have not hard coded in the $to variable it is part of the form.

    So that’s it, you now should know how to, send text emails, send html emails, send emails with attachments. Wasnt it fairly easy? So now you can send html emails with attachments so theres no more links to download files or very plain emails.

    About the Author

    Andrew Walsh (Andrew on the forums) lives in the UK and is engaged in his secondary education. He wishes to persue a higher education in computer studies. He Started programming at the age of 13 years, currently he has knowledge of php/mysql, html, css, javascript.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Let's add some content checking to the above. We'll check to ensure they typed out...
       · H! KendoVery well done indeed, At such a tender age U R writing such fabulous...
       · Hi,Nice tutorial, especially with having the HTML and file attachment sections...
       · Thanks for the nice scripts!To get the second script to work I must add the...
       · thank you for spotting it ill get it corrected.
       · The line $submit=$_POST['submit']; has been added to the appropriate pages.
       · Mail never arrives at destination.
       · Attachments section has been updated and proven to work. Thank you for pointing it...
       · Andrew, I copied and adapted your script for attaching a document to a form that I...
       · I've copied the code exactly but the email never makes it to my inbox. However if I...
       · I am sorry to say, that these days, sending mail with PHP is a little more...
     

    MISCELLANEOUS ARTICLES

    - Stopping CSRF Attacks in Your PHP Applicatio...
    - Quick and Dirty AJAX Tutorial
    - Flickr Puzzle Mashup
    - The PAVISE of Security
    - Creating a CAPTCHA with PHP
    - Sending SMS Thru HTTP
    - The Postal Fix - Part 2
    - Adding Mail with Exim
    - The Postal Fix - Part 1
    - Create Your Own Custom API
    - Adding Drop Shadows with PHP
    - Writing a Basic Authentication System in PHP
    - Overlapping Images with GD
    - Using Sockets in PHP
    - Dynamic CSS with PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway