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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.16648 seconds
  • Memory Usage 2,975KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates