email attachment not working

Discussion in 'PHP' started by dougvcd, Jul 21, 2007.

  1. #1
    ok i have this code to attach a photo to email but the photos do not arrive or they seem corupt if any one can help thanks
    Doug
    <?php
    define("EMAIL_TO",            'caravan.exchange@googlemail.com');
    define("EMAIL_SUBJECT",     'New POC from %s' );
    define("EMAIL_FROM",                    'caravan.exchange@googlemail.com' );
    define("EMAIL_MAX_ATTACHMENT_SIZE",  '300000' );
    function Attach( $message, $headers = null)
    {   
     // Build your message from your posted form    
           $message = nl2br( sprintf( 
           "Name : %s\n". 
    	   "Username : %s\n".
    	   "Email : %s\n".        
    	   "%s",        
    	   $_POST['Name'],        
    	   $_POST['Username'],        
    	   $_POST['Email'],        
    	   $message    
    	   ) ); 
    	 // start to add to headers    
    	 $headers .= sprintf( "From: %s\n", EMAIL_FROM );
    		      // check to see if we are attaching    
    	 if( is_uploaded_file( $_FILES['attach']['tmp_name'] ) ) 
    	 {       
    	// the content needs a boundary so that email clients know where to get data        
    	$boundary = sprintf( '==Multipart_Boundary_x%s', md5( time( ) ) ) ;
    	 // continue to build headers, adding the boundary for attached data        
    		$headers .= sprintf(                             
    		 "MIME-Version: 1.0\n".                            
    		"Content-Type: multipart/mixed;\n boundary=\"%s\"\n",                            
    		$boundary );
    	 // edit message to include data and boundaries        
    		$message = sprintf(                            
    			"This is a multi-part message in MIME format.\n\n".                            
    			"--%s\n".                            
    			"Content-Type: text/html; charset=\"iso-8859-1\"\n".                            
    			"Content-Transfer-Encoding: 7bit\n\n".                            
    			"%s\n\n".                            
    			"--%s\n".                            
    			"Content-Type: %s;\n name=\"%s\"\n".                            
    			"Content-Disposition: attachment;\n filename=\"%s\"\n".               
    			"Content-Transfer-Encoding: base64\n\n".                            
    			"%s\n\n".                            
    			"--%s-\n",                            
    				$boundary,                            
    				$message,                            
    				$boundary,                            
    				$_FILES['attach']['type'],                            
    				$_FILES['attach']['name'],                            
    				$_FILES['attach']['name'],                            
    				chunk_split( base64_encode( file_get_contents( $_FILES['attach']['tmp_name'] ) ) ),    
    				$boundary                    
    	);      
    	}   
    		 // send the original message with the file attached    
    			return mail( EMAIL_TO, sprintf( EMAIL_SUBJECT, $_SERVER['REMOTE_ADDR'] ), $message, $headers);
    	}
    			if( $_POST )
    	{    
    			if( Attach( $_POST['message'] ) )    
    	{        
    			printf( "<center><font color=blue>Your message was sent to %s</font></center>", EMAIL_TO );
    			echo'<meta http-equiv="REFRESH" content="2;url=index.html">';    
    	}       
    			else    
    	{        
    			print("<center><font color=red>Failed to deliver your message</font></center>");
    	}
    	}
    ?>
    PHP:
    <form action="" method="post" enctype="multipart/form-data">    
      <input type="hidden" name="MAX_FILE_SIZE" value="<?=EMAIL_MAX_ATTACHMENT_SIZE ?>" />
            <table width="600" border="0" cellspacing="0" cellpadding="2" align="center">         
            <tr>            
            <td align="left"><span>Name:</span></td>            
            <td align="left"><input type="text" name="Name"></td>         
            </tr>         
            <tr>            
            <td align="left"><span>Username:</span></td>            
            <td align="left"><input type="text" name="Username"></td>         
            </tr>        
            <tr>            
            <td align="left"><span>Email:</span></td>            
            <td align="left"><input type="text" name="Email"></td>        
            </tr>         
            <tr>            
            <td align="left" valign="top"><span>Message:</span></td>            
            <td align="left"><textarea name="MsgBody" cols="40" rows="5"></textarea></td>         
            </tr>         
            <tr>            
            <td align="left" valign="top">Attach Image :</td>            
            <td><input type="file" name="attach" /></td>         
            </tr>         
            <tr>            
            <td align="left"><input type="submit" value="Send"></td>         
            </tr>      
            </table>    
            </form>
    HTML:

     
    dougvcd, Jul 21, 2007 IP
  2. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    not sure if it is googlemail that is the problem
    just changed to hotmail and was ok
    cheers
    Doug
     
    dougvcd, Jul 21, 2007 IP
  3. amf-flt

    amf-flt Active Member

    Messages:
    100
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Instead of fighting your way through building the mail headers to support mime attachments, consider the pear module Mail_Mime, if you either have control over your server or can convince your hoster to load it for you (most will, since it's a pretty common pear module).
     
    amf-flt, Jul 21, 2007 IP
  4. ds316

    ds316 Peon

    Messages:
    154
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I agree, messing around with headers can give you a serious headache.
     
    ds316, Jul 22, 2007 IP