Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Mail() From Start To Finish
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap