www.webdeveloper.com
Results 1 to 2 of 2

Thread: a problem with mail attachment

  1. #1
    Join Date
    Sep 2006
    Posts
    5

    a problem with mail attachment

    Hi am building a mail form that can sendt an email with up to 3 files as attachement. i have found som code but i cant get it working 100% i only get the text in my mail.

    email.php:
    PHP Code:
    <?php 
    $attachments
    [] = $_POST['fileatt1']; // don't forget to add the path 
    $attachments[] = $_POST['fileatt2']; // don't forget to add the path 
    $attachments[] = $_POST['fileatt3']; // don't forget to add the path 
    $sendto 'my@mail.com'// use a conventional mail server 
    $sendtoCC 'my@mail.com'
    $sendtoBCC 'my@mail.com'
    $message $_POST['comments']; 
    $subject $_POST['subject']; 
    $sendfrom $_POST['email']; // How about adding the sender 

    $headers 'Cc: '.$sendtoCC."\r\n".'Bcc: '.$sendtoBCC."\r\n".'From: '.$sendfrom

    $sent send_mime_mail($sendto$subject$message$headersFALSE$attachments); 

    if(
    $sent) print 'Sent ok'
    else print 
    'Error sending'


    function 
    send_mime_mail($recipient$subject$message$headers NULL$html NULL$attachments NULL$css NULL


        if (!
    function_exists('mime_content_type')){ 
            function 
    mime_content_type($file){ 
                if(!
    is_readable($file)) return false
                @
    $size getimagesize($file); 
                if(!empty(
    $size['mime'])){ 
                    return(
    $size['mime']); 
              
       }else{ 
                    
    $extensions = array('doc' => 'application/msword''html'=> 'text/html''htm' => 'text/html'
                        
    'pdf' => 'application/pdf''ppt' => 'application/vnd.ms-powerpoint''rtf' => 'text/rtf'
                        
    'xls' => 'application/vnd.ms-excel''zip' => 'application/zip'); 
                    
    $keys array_keys($extensions); 
                    
    $parts array_reverse(explode('.'$file)); 
                    
    $extension $parts['0']; 
                    if(
    in_array($extension$keys)) return $extensions[$extension]; 
                    
    $data file_get_contents($filename); 
                    
    $bad false
                    for(
    $x 0$y strlen($data); !$bad && $x $y$x++) 
                    { 
                        
    $bad = (ord($data{$x}) > 127); 
                    } 
                    if(!
    $bad) return ('text/plain'); 
                return(
    'application/octet-stream'); 
                } 
            } 
        } 
        function 
    convert_links($string$protocol TRUE
        { 
            
    $search[] = '/<a\s[^>]*href=[\'"]mailto:([^\'"]+)[\'"][^>]*>([^<]+)<\/a>/i'
            
    $search[] = '/<a\s[^>]*href=mailto:([^\s>]+)[^>]*>([^<]+)<\/a>/i'
            
    $search[] = '/<a\s[^>]*href=[\'"](http:\/\/[^\'"]+)[\'"][^>]*>([^<]+)<\/a>/i'
            
    $search[] = '/<a\s[^>]*href=(http:\/\/[^\s>]+)[^>]*>([^<]+)<\/a>/i'
            
    $replace '\2 (\1)'
            return(
    strip_tags(preg_replace($search$replace$string))); 
        } 

        function 
    html_to_plain($string
        { 
            
    $string preg_replace("(\r\n|\n|\r)"' '$string); 
            
    $string preg_replace('/<p>/i'"\n\n"$string); 
            
    $string preg_replace('/(<br>|<br \/>)/i'"\n"$string); 
            return (
    convert_links($string)); 
        } 
        if(!isset(
    $css)) $css ''
        
    // Generate a boundary string 
        
    $mime_boundary "mime_boundry_multipart/mixed".md5(time())."x"
        
    $alternative_mime_boundary "mime_boundry_multipart/alternative_".md5(time())."x"

        if(empty(
    $headers)) $headers ''
        
    $headers .= "\nMIME-Version: 1.0\n"

        if(
    count($attachments) > 0){ 
            
    $headers    .= "Content-Type: multipart/mixed;\n" 
                           
    " boundary=\"{$mime_boundary}\"\n\n"

                if(!empty(
    $html) and $html == TRUE){ 
                    
    $content   "This is a multi-part message in MIME format.\n\n"
                                 
    "--{$mime_boundary}\n"
                                 
    "Content-Type: multipart/alternative;\n"
                                 
    " boundary=\"{$alternative_mime_boundary}\"\n\n"
                                 
    "--{$alternative_mime_boundary}\n"
                                 
    "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n"
                                 
    "Content-Transfer-Encoding: 7bit\n\n"
                                 
    html_to_plain($message)."\n\n"
                                 
    "--{$alternative_mime_boundary}\n"
                                 
    "Content-Type: text/html; charset=iso-8859-1\n"
                                 
    "Content-Transfer-Encoding: 7bit\n\n"
                                 
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
                                 
    "<html>\n"
                                 
    "<head>\n"
                                 
    " <style type=\"text/javascript\">\n"
                                 
    $css \n"
                                 
    " </style>\n"
                                 
    " <meta content=\"text/html;charset=iso-8859-1\" http-equiv=\"Content-Type\">\n"
                                 
    " <title></title>\n"
                                 
    "</head>\n"
                                 
    "<body>\n"
                                 
    $message."\n"
                                 
    "</body>\n"
                                 
    "</html>\n\n"
                                 
    "--{$alternative_mime_boundary}--\n"
                             }else{ 
                    
    $content   "This is a multi-part message in MIME format.\n\n"
                                 
    "--{$mime_boundary}\n"
                                 
    "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" 
                                 
    "Content-Transfer-Encoding: 7bit\n\n" 
                                 
    convert_links($message)."\n\n"
                               } 



                
    // Deal with attachments array 
                
    if(count($attachments) > 0){ 
                    foreach(
    $attachments as $key => $value){ 
                        
    $mime_type mime_content_type($value); 
                        if(
    is_file($value) and is_readable($value) ){ 
                            if(
    ereg('^image'$mime_type)){ 
                                
    $disposition 'inline'
                            }else{ 
                                
    $disposition 'attachment'
                            } 
                            
    $filesize filesize($value); 
                            
    $file fopen($value,'rb'); 
                            
    $data fread($file,$filesize); 
                            
    fclose($file); 

                            
    // Base64 encode the file data 
                            
    $data chunk_split(base64_encode($data)); 
                            
    $value pathinfo($value); 
                            
    $content    .=  "--{$mime_boundary}\n" 
                                            
    "Content-Type: $mime_type;\n" 
                                            
    " name=\"{$value['basename']}\"\n" 
                                            
    "Content-Transfer-Encoding: base64\n" 
                                            
    "Content-Disposition: $disposition;\n" 
                                            
    " filename=\"{$value['basename']}\"\n\n" 
                                            
    $data "\n"
                        } 
                    } 
                } 

                
    $content .= "--{$mime_boundary}--\n"




        }elseif(!empty(
    $html) and $html == TRUE){ 
               
    $headers .=  "Content-Type: multipart/alternative;\n"
                            
    " boundary=\"{$alternative_mime_boundary}\"\n\n"
               
    $content  "--{$alternative_mime_boundary}\n"
                            
    "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n"
                            
    "Content-Transfer-Encoding: 7bit\n\n"
                            
    html_to_plain($message)."\n\n"
                            
    "--{$alternative_mime_boundary}\n"
                            
    "Content-Type: text/html; charset=iso-8859-1\n"
                            
    "Content-Transfer-Encoding: 7bit\n\n"
                            
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
                            
    "<html>\n"
                            
    "<head>\n"
                            
    " <style type=\"text/javascript\">\n"
                            
    $css \n"
                            
    " </style>\n"
                            
    " <meta content=\"text/html;charset=iso-8859-1\" http-equiv=\"Content-Type\">\n"
                            
    " <title></title>\n"
                            
    "</head>\n"
                            
    "<body>\n"
                            
    $message."\n"
                            
    "</body>\n"
                            
    "</html>\n\n"
                            
    "--{$alternative_mime_boundary}--\n"


        }else{ 
                   
    $headers .= "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n" 
                              
    "Content-Transfer-Encoding: 7bit\n\n"
                
    $content  =  convert_links($message)."\n"
        } 
        if(@
    mail($recipient$subject$content$headers)) return TRUE
        return 
    FALSE

    ?>

  2. #2
    Join Date
    Sep 2006
    Posts
    5

    the form

    here is the code used for the form:

    form.php:
    PHP Code:
    <div id="form"> 
            <form  action="email.php" enctype="multipart/form-data" method="post" onsubmit=" 
            document.getElementById('container0').style.display=''; 
            document.getElementById('container2').style.display='none'; 
            progress(); 
            return true;"> 
            <fieldset id="fieldset"> 
            <label for="name">Contact name<span style="color: red;">*</span>:</label> 
            <input <?php if (!empty ($hidden)) { if (empty ($name)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="name" type="text" name="name" value="<?php print "$name"?>" tabindex="1" /> 
            <br /> 
            <label for="email">E-mail address<span style="color: red;">*</span>:</label> 
            <input <?php if (!empty ($hidden)) { if (empty ($email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="email" type="text" name="email" value="<?php print "$email"?>" tabindex="1" /> 
            <br />     
            <label for="confirm_email">Confirm e-mail<span style="color: red;">*</span>:</label> 
            <input <?php if (!empty ($hidden)) { if (empty ($confirm_email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="confirm_email" type="text" name="confirm_email" value="<?php print "$confirm_email"?>" tabindex="1" /> 
            <br />     
            <label for="subject">Subject<span style="color: red;">*</span>:</label> 
            <input <?php if (!empty ($hidden)) { if (empty ($subject)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="subject" type="text" name="subject" value="<?php print "$subject"?>" tabindex="1" /> 
            <br />     
            <label for="comments">Comments<span style="color: red;">*</span>:</label> 
            <textarea <?php if (!empty ($hidden)) { if (empty ($comments)) { print 'style="background: pink;"'; } } ?>class="form_elements" id="comments" name="comments" cols="19" rows="5" tabindex="1"><?php print "$comments"?></textarea> 
            <br /><br /> 
            
            <label for="fileatt1">Attach document:</label> 
            <input id="fileatt1" type="file" name="fileatt1"  tabindex="1" /> 
            <br /> 
            
            <label for="fileat2t">Attach document:</label> 
            <input id="fileatt2" type="file" name="fileatt2"  tabindex="1" /> 
            <br /> 
            
            <label for="fileatt3">Attach document:</label> 
            <input id="fileatt3" type="file" name="fileatt3"  tabindex="1" /> 
            <br /> 
            
            <input  type="hidden" name="hidden"  value="1" /><br /> 
            <label for="submit"><span style="color: red;">*</span> Compulsory fields.</label> 
            <input id="submit" type="submit" value="Send" tabindex="1" /> 
            </fieldset> 
            </form> 
            </div>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles