Send attachment through email
-
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.
-
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.
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";