I dont think there is a non complicated way to do this, I think your better taking a slightly different approach using a system such as below. Hopefully I can explain how to use it for what you want to do and how it will work in substitute to what you are currently using.
A straight C&P from
http://www.texelate.co.uk/ regarding adding attachments to emails,
It looks like a lot more work but it isn't really.
http://www.texelate.co.uk/blog/send-email-attachment-with-php/
To send email attachments you need to make use of MIME (Multipurpose Internet Mail Extensions) – a mechanism that allows email to go beyond a basic, limited character set. MIME has many uses but for the purposes of this tutorial we will send a multipart/mixed MIME email; this means we can send a text email and attach a PDF file to it (for information on attaching other file types please see the end of this tutorial). The MIME side of things will be explained as we go. Firstly, let’s set the email details and the attachment details up.
Set the email and attachment Details
You will fill this in as required.
PHP:
<?php
$to = $name .'<'.$email.'>';
// The destination e-mail address
$from = 'John-Smith <[email protected]>';
// Your email address for reply purposes
$subject = 'Application Form';
// Self explanitory
$fileatt = '/public_html/pdfs/mypdf.pdf';
// The name and location of the file you wish to send on your server
$fileatttype = 'application/pdf';
// The file type of the above document
// in this example a pdf - application/pdf
// a GIF image file would show $fileatttype = 'image/GIF'; for example
$fileattname = 'newname.pdf';
// The new name of the file that will show on the sent email
// It can have the same name as the original file
$headers = 'From: $from';
// No need to edit this
$body = 'name: john, email: [email protected], number: 0232323323';
// This is your original variable and is the text shown in the email sent, change it to whatever you wish
?>
The email details are obvious; for the attachment we need the path to the file and the headers for the file type (PDF in this case). The $fileattname variable determines the name of the attachment – it doesn’t have to match the name of the original file. Next, we need to transfer the file into a variable which we’ll call $file.
Read in the attachment. This part of code does not have to be edited and just has to sit in your php code directly underneath the above
PHP:
<?php
$file = fopen( $fileatt, 'rb' );
$data = fread( $file, filesize( $fileatt ) );
fclose( $file );
?>
Now the file has been read in it needs to be converted a format that is compatible with standard email: 7-bit ASCII. Before that, the appropriate headers need to be added to the email so the recipient knows what to expect.
Add the MIME content
PHP:
<?php
$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 = $body.'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';
?>
The conversion to 7-bit ASCII takes place at the $data = chunk_split( base64_encode( $data ) ) line. The file is then attached using the appropriate headers.
Finally – send the email:
PHP:
<?php
if( mail( $to, $subject, $message, $headers ) ) {
echo '<p>The email was sent.</p>';
}
else {
echo '<p>There was an error sending the mail.</p>';
}
}
?>
This is almost exactly what you have in your file, with the exception it will error out if the email is not sent successfully.
TLDR::
So your whole code will read:
PHP:
<?php
// This block of code is to send an email with an attachment.
// --------------------------------------------------------
// FILL IN THE DETAILS BELOW
$to = “Me <[email protected]>”;
// The destination e-mail address, in your example you used the variable $my_email
$from = “John-Smith <[email protected]>”;
// Your email address for reply purposes, this goes in to the headers information
$subject = “Application Form”;
// Self explanatory
$fileatt = “/public_html/pdfs/mypdf.pdf”;
// The name and location of the file you wish to send, found on your server
$fileatttype = “application/pdf”;
// The file type of the above document
// in this example a pdf - application/pdf
// a GIF image file would show $fileatttype = “image/GIF”; for example
$fileattname = “newname.pdf”;
// The new name of the file that will show on the sent email
// it can have the same name as the original file
$body = “name: john, email: [email protected], number: 0232323323”;
// This is your original variable and is the email body
// **** NO NEED TO EDIT ANYTHING PAST THIS LINE ****
$headers = “From: $from”;
// No need to edit this
// OPEN FILE, READ AND CLOSE READY FOR FORMATTING
$file = fopen( $fileatt, ‘rb’ );
$data = fread( $file, filesize( $fileatt ) );
fclose( $file );
// CREATE THE HEADERS
$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 = “{$body}.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”;
if( mail( $to, $subject, $message, $headers ) ) {
echo “<p>The email was sent.</p>”;
} else {
echo “<p>There was an error sending the mail.</p>”;
}
}
?>
p.s. I have not changed any of the code from the original posted on this site with a few very small exceptions. Let me know what does not work and I'll try to put it straight. I should not really post at 1am on a Saturday night with php advice :S
Edit: Forums seems to have borked up the "s and such. Will sort it tomorrow