Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Send attachment through email

Send attachment through email

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
phpcomquestionannouncement
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DinoRondelly
    wrote on last edited by
    #1

    I am having trouble attaching a wav file to email through PHP.

                    $to = "John.Doe@TryHarder.com";
    		
    		$from = "John.Doe@TryHarder.com";
    		
    		$subject = "Test Email";
    		
    		$message = "This is your test email";
    		
    		$fileatt = "[Removed]\E1002.wav";
    		
    		$fileatttype = "application/wav";
    		
    		$fileattname = "E1002.wav";
    		
    		$headers = "From: $from";
    		
    		
    		//Open File
    		$file = fopen( $fileatt, 'rb');
    		
    		$data = fread( $file, filesize( $fileatt ) );
    		
    		fclose( $file );
    		
    		
    		//Add MIME content 
    		$semi_rand = md5( time() );
    		
    		$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    		
    		$headers .= "nMIME-Version: 1.0n". 
    		
    		"Content-Type: multipart/mixed;n" .
    		
    		" boundary= {$mime_boundary}";
    		
    		$message = "This is a multi-part message in MIME format.nn" .
    		
    		"–{$mime_boundary}n" .
    		
    		"Content-Type: text/plain; charset= iso-8859-1 n".
    		
    		"Content-Transfer-Encoding: 7bitnn" .
    
    		$message . "nn";
    
    		$data = chunk_split( base64_encode( $data ) );
    		
    		$message .= "–{$mime_boundary}n" .
    		
    		"Content-Type: {$fileatttype};n".
    		
    		" name= {$fileattname} n" .
    
    		"Content-Disposition: attachment;n".
    		
    		" filename={$fileattname} n".
    		
    		"Content-Transfer-Encoding: base64nn" .
    		
    		$data . "nn" .
    		
    		"–{$mime_boundary}–n";
    		
    		//Send Email
    		$mail_sent = mail($to,$subject,$message,$headers);
    		
    		//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
    		echo $mail_sent ? "Mail sent" : "Mail failed";
    

    It seems what ever MIME is doing ends up being the body of the email and the attachment doesn't come though. If someone could point out what i am doing wrong i would be forever grateful. I am fairly new to PHP so please forgive me if this comes of as basic.

    D 1 Reply Last reply
    0
    • D DinoRondelly

      I am having trouble attaching a wav file to email through PHP.

                      $to = "John.Doe@TryHarder.com";
      		
      		$from = "John.Doe@TryHarder.com";
      		
      		$subject = "Test Email";
      		
      		$message = "This is your test email";
      		
      		$fileatt = "[Removed]\E1002.wav";
      		
      		$fileatttype = "application/wav";
      		
      		$fileattname = "E1002.wav";
      		
      		$headers = "From: $from";
      		
      		
      		//Open File
      		$file = fopen( $fileatt, 'rb');
      		
      		$data = fread( $file, filesize( $fileatt ) );
      		
      		fclose( $file );
      		
      		
      		//Add MIME content 
      		$semi_rand = md5( time() );
      		
      		$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      		
      		$headers .= "nMIME-Version: 1.0n". 
      		
      		"Content-Type: multipart/mixed;n" .
      		
      		" boundary= {$mime_boundary}";
      		
      		$message = "This is a multi-part message in MIME format.nn" .
      		
      		"–{$mime_boundary}n" .
      		
      		"Content-Type: text/plain; charset= iso-8859-1 n".
      		
      		"Content-Transfer-Encoding: 7bitnn" .
      
      		$message . "nn";
      
      		$data = chunk_split( base64_encode( $data ) );
      		
      		$message .= "–{$mime_boundary}n" .
      		
      		"Content-Type: {$fileatttype};n".
      		
      		" name= {$fileattname} n" .
      
      		"Content-Disposition: attachment;n".
      		
      		" filename={$fileattname} n".
      		
      		"Content-Transfer-Encoding: base64nn" .
      		
      		$data . "nn" .
      		
      		"–{$mime_boundary}–n";
      		
      		//Send Email
      		$mail_sent = mail($to,$subject,$message,$headers);
      		
      		//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
      		echo $mail_sent ? "Mail sent" : "Mail failed";
      

      It seems what ever MIME is doing ends up being the body of the email and the attachment doesn't come though. If someone could point out what i am doing wrong i would be forever grateful. I am fairly new to PHP so please forgive me if this comes of as basic.

      D Offline
      D Offline
      DinoRondelly
      wrote on last edited by
      #2

      I have figured out the issue i was a little off but got it working with the following code.

      $to = "Brad.Harris@prosodiecrm.com";

      	$from = "John Do";
      	
      	$subject = "Test Email With WAV";
      	
      	$message = "This is your test email";
      	
      	$fileatt = "Removed\\E1002.wav";
      	
      	$fileatt\_name = "E1002.wav";
      	
      	$type = "audio/x-wav";
      	
      	$name = "E1002.wav";
      	
      	$headers = "From: $from";
      	
      	//Add MIME content 
      	$semi\_rand = md5( time() );
      	
      	$mime\_boundary="==Multipart\_Boundary\_x".md5(mt\_rand())."x";
      
         $headers = "From: $from\\r\\n" .
         "MIME-Version: 1.0\\r\\n" .
         "Content-Type: multipart/mixed;\\r\\n" .
         " boundary=\\"{$mime\_boundary}\\""; 
      	
      	$message = "This is an example" ;
      	
      	$message = "This is a multi-part message in MIME format.\\n\\n" .
        	"--{$mime\_boundary}\\n" .
        	"Content-Type: text/plain; charset=\\"iso-8859-1\\"\\n" .
        	"Content-Transfer-Encoding: 7bit\\n\\n" .
      	$message . "\\n\\n";
      	
      	//Open File
      	$file = fopen( $fileatt, 'rb');
      	
      	$data = fread( $file, filesize( $fileatt ) );
      	
      	fclose( $file );
      	
      	$data = chunk\_split(base64\_encode($data));
      	
           $message .= "--{$mime\_boundary}\\n" .
              "Content-Type: {$type};\\n" .
              " name=\\"{$name}\\"\\n" .
              "Content-Disposition: attachment;\\n" .
              " filename=\\"{$fileatt\_name}\\"\\n" .
              "Content-Transfer-Encoding: base64\\n\\n" .
           $data . "\\n\\n";
      	
           $message.="--{$mime\_boundary}--\\n";
           
      	//Send Email
      	$mail\_sent = mail($to,$subject,$message,$headers);
      	
      	//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
      	echo $mail\_sent ? "Mail sent" : "Mail failed";
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups