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 exaplined as we go. Firstly, let’s set the email details and the attachment details up.
Set the email and attachment Details
<?php
$to = “$name <$email>”;
$from = “John-Smith <john.smith@domain.com>”;
$subject = “Here is your attachment”;
$fileatt = “/public_html/pdfs/mypdf.pdf”;
$fileatttype = “application/pdf”;
$fileattname = “newname.pdf”;
$headers = “From: $from”;
?>
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
<?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
$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”;
?>
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:
Send the email
<?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>”;
}
}
?>
You can attach any kind of file you like – you need to make sure that change the $fileatttype variable to reflect the content type of the file you are attaching (e.g. image/gif for a GIF file).
For more help call me on 07843 483 078 or get a free quote now!











Send an email attachment with PHP





Is it possible to do a form, where the visitor can click a browser button. Find a file on computer e.g. PDF and when they click Send the form AND the PDF are emailed to someone?
Yes.
This script shows you how to mail a file on your server as an attachment. So, to do what you are asking all you need to do is upload the file first THEN use this script. In fact, if you attach the temporary uploaded file and don’t bother moving it anywhere, you wouldn’t even have to worry about deleting it afterwards.