SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    email attachment to detect the content_type for each file in directory

    Hi, everybody!
    I have this function from php.net.
    I would like to change this function to read 'att' directory and automatically detect the the content_type for each file in this directory.
    Any help would be appreciated.
    PHP Code:
    function send_mail($emailaddress$fromaddress$emailsubject$body$attachments false)
    {
         
    $eol "\r\n";
         
    $mime_boundary md5(time());
        
         
    # Common Headers
        
    $headers 'From: MyName<' $fromaddress '>' $eol;
         
    $headers .= 'Reply-To: MyName<' $fromaddress '>' $eol;
         
    $headers .= 'Return-Path: MyName<' $fromaddress '>' $eol// these two to set reply address
         
    $headers .= "Message-ID: <" time() . " TheSystem@" $_SERVER['SERVER_NAME'] . ">" $eol;
         
    $headers .= "X-Mailer: PHP v" phpversion() . $eol// These two to help avoid spam-filters
        
        
         # Boundry for marking the split & Multitype Headers
        
    $headers .= 'MIME-Version: 1.0' $eol;
         
    $headers .= "Content-Type: multipart/related; boundary=\"" $mime_boundary "\"" $eol;
        
         
    $msg "";
        
         if (
    $attachments !== false)
        {
            
             for(
    $i 0$i count($attachments); $i++)
            {
                 if (
    is_file($attachments[$i]["file"]))
                    {
                     
    # File for Attachment
                    
    $file_name substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/") + 1));
                    
                     
    $handle fopen($attachments[$i]["file"], 'rb');
                     
    $f_contents fread($handlefilesize($attachments[$i]["file"]));
                     
    $f_contents chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
                     
    fclose($handle);
                    
                     
    # Attachment
                    
    $msg .= "--" $mime_boundary $eol;
                     
    $msg .= "Content-Type: " $attachments[$i]["content_type"] . "; name=\"" $file_name "\"" $eol;
                     
    $msg .= "Content-Transfer-Encoding: base64" $eol;
                     
    $msg .= "Content-Disposition: attachment; filename=\"" $file_name "\"" $eol $eol// !! This line needs TWO end of lines !! IMPORTANT !!
                     
    $msg .= $f_contents $eol $eol;
                    
                     }
                 }
             }
        
         
    # Setup for text OR html
        
    $msg .= "Content-Type: multipart/alternative" $eol;
        
         
    # Text Version
        
    $msg .= "--" $mime_boundary $eol;
         
    $msg .= "Content-Type: text/plain; charset=iso-8859-1" $eol;
         
    $msg .= "Content-Transfer-Encoding: 8bit" $eol;
         
    $msg .= strip_tags(str_replace("<br>""\n"$body)) . $eol $eol;
        
         
    # HTML Version
        
    $msg .= "--" $mime_boundary $eol;
         
    $msg .= "Content-Type: text/html; charset=iso-8859-1" $eol;
         
    $msg .= "Content-Transfer-Encoding: 8bit" $eol;
         
    $msg .= $body $eol $eol;
        
         
    # Finished
        
    $msg .= "--" $mime_boundary "--" $eol $eol// finish with two eol's for better security. see Injection.
        
         # SEND THE EMAIL
        
    mail($emailaddress$emailsubject$msg$headers);
        echo 
    "mail send";
        }


    # To Email Address
    $emailaddress "xxx@xxx.com";

    # From Email Address
    $fromaddress "webmaster@xxx.org";

    # Message Subject
    $emailsubject "This is a test mail with some attachments";

    # Use relative paths to the attachments
    $attachments = Array(
        Array(
    "file" => "att/close.gif""content_type" => "image/gif"),
         Array(
    "file" => "att/capture.jpg""content_type" => "image/jpg")
        );

    # Message Body
    $body "This is a message with <b>" count($attachments) . "</b> attachments and maybe some <i>HTML</i>!";

    send_mail($emailaddress$fromaddress$emailsubject$body$attachments); 
    my mobile portal
    ghiris.ro

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solved
    PHP Code:
    <?php
    function send_mail($emailaddress$fromaddress$emailsubject$body$attachments false)
    {
         
    $eol "\r\n";
         
    $mime_boundary md5(time());
        
         
    # Common Headers
        
    $headers 'From: MyName<' $fromaddress '>' $eol;
         
    $headers .= 'Reply-To: MyName<' $fromaddress '>' $eol;
         
    $headers .= 'Return-Path: MyName<' $fromaddress '>' $eol// these two to set reply address
         
    $headers .= "Message-ID: <" time() . " TheSystem@" $_SERVER['SERVER_NAME'] . ">" $eol;
         
    $headers .= "X-Mailer: PHP v" phpversion() . $eol// These two to help avoid spam-filters
        
        
         # Boundry for marking the split & Multitype Headers
        
    $headers .= 'MIME-Version: 1.0' $eol;
         
    $headers .= "Content-Type: multipart/related; boundary=\"" $mime_boundary "\"" $eol;
        
         
    $msg "";
        
         if (
    $attachments !== false)
        {
            
             for(
    $i 0$i count($attachments); $i++)
            {
                 if (
    is_file($attachments[$i]["file"]))
                    {
                     
    # File for Attachment
                    
    $file_name substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/") + 1));
                    
                     
    $handle fopen($attachments[$i]["file"], 'rb');
                     
    $f_contents fread($handlefilesize($attachments[$i]["file"]));
                     
    $f_contents chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
                     
    fclose($handle);
                    
                     
    # Attachment
                    
    $msg .= "--" $mime_boundary $eol;
                     
    $msg .= "Content-Type: " $attachments[$i]["content_type"] . "; name=\"" $file_name "\"" $eol;
                     
    $msg .= "Content-Transfer-Encoding: base64" $eol;
                     
    $msg .= "Content-Disposition: attachment; filename=\"" $file_name "\"" $eol $eol// !! This line needs TWO end of lines !! IMPORTANT !!
                     
    $msg .= $f_contents $eol $eol;
                    
                     }
                 }
             }
        
         
    # Setup for text OR html
        
    $msg .= "Content-Type: multipart/alternative" $eol;
        
         
    # Text Version
        
    $msg .= "--" $mime_boundary $eol;
         
    $msg .= "Content-Type: text/plain; charset=iso-8859-1" $eol;
         
    $msg .= "Content-Transfer-Encoding: 8bit" $eol;
         
    $msg .= strip_tags(str_replace("<br>""\n"$body)) . $eol $eol;
        
         
    # HTML Version
        
    $msg .= "--" $mime_boundary $eol;
         
    $msg .= "Content-Type: text/html; charset=iso-8859-1" $eol;
         
    $msg .= "Content-Transfer-Encoding: 8bit" $eol;
         
    $msg .= $body $eol $eol;
        
         
    # Finished
        
    $msg .= "--" $mime_boundary "--" $eol $eol// finish with two eol's for better security. see Injection.
        
         # SEND THE EMAIL
        
    mail($emailaddress$emailsubject$msg$headers);
        echo 
    "mail sent";
        }


    # To Email Address
    $emailaddress "xxx@xxx.com";

    # From Email Address
    $fromaddress "webmaster@xxx.org";

    # Message Subject
    $emailsubject "This is a test mail with some attachments";

    $path "att/";
    $dir_handle opendir($path) or die("Unable to open $path");
    while (
    $file readdir($dir_handle)){
         if(!
    is_dir($file)){
             
    $filesize filesize($path $file);
            
            
    $fileatt $file;
            
             
    // Gather relevent info about file
            
    $len filesize($path $fileatt);
             
    $filename basename($fileatt);
             
    $file_extension strtolower(substr(strrchr($filename"."), 1));
            
             
    // This will set the Content-Type to the appropriate setting for the file
            
    switch($file_extension){
             case 
    "pdf"$ctype "application/pdf";
                break;
             case 
    "exe"$ctype "application/octet-stream";
                break;
             case 
    "zip"$ctype "application/zip";
                break;
             case 
    "doc"$ctype "application/msword";
                break;
             case 
    "xls"$ctype "application/vnd.ms-excel";
                break;
             case 
    "ppt"$ctype "application/vnd.ms-powerpoint";
                break;
             case 
    "gif"$ctype "image/gif";
                break;
             case 
    "png"$ctype "image/png";
                break;
             case 
    "jpeg":
             case 
    "jpg"$ctype "image/jpg";
                break;
             case 
    "mp3"$ctype "audio/mpeg";
                break;
             case 
    "wav"$ctype "audio/x-wav";
                break;
             case 
    "mpeg":
             case 
    "mpg":
             case 
    "mpe"$ctype "video/mpeg";
                break;
             case 
    "mov"$ctype "video/quicktime";
                break;
             case 
    "avi"$ctype "video/x-msvideo";
                break;
            
             
    // The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
            
    case "php":
             case 
    "htm":
             case 
    "html":
            
             default: 
    $ctype "application/force-download";
                 }

             
    $fileatt_name $filename;
             
             
    $attachments[] = Array("file" => "{$path}{$fileatt_name}""content_type" => "$ctype");
             
             }
        }
        
        
    closedir($dir_handle);
        
        
    # Message Body
        
    $body "This is a message with <b>" count($attachments) . "</b> attachments and maybe some <i>HTML</i>!";
        
        
    send_mail($emailaddress$fromaddress$emailsubject$body$attachments);
    ?>
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

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