PHP – Email with Attachments

A while back, a friend of mine asked me to have a look at writing a PHP function for sending email and attaching any uploaded files from a HTML form.

I’m very glad she did ask me as this bit of code has been very useful to me over the years:

< ?php
$fileatt = $_FILES['uploadfile']['tmp_name']; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = $_POST['name']; // Filename that will be used for the file as the attachment
 
$email_from = $_POST['email']; // Who the email is from
$email_subject = "Email Subject"; // The Subject of the email
$email_txt = $_POST['message']; // Message that the email has in it
 
$email_to = "contactEmail@example.com"; // Who the email is to
 
$headers = "From: ".$email_from;
 
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
 
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
 
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
 
$email_message .= $_POST['message'] .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
 
$data = chunk_split(base64_encode($data));
 
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
 
$ok = @mail($email_to, $email_subject, $email_message, $headers);
 
if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>

Here is a simple php/html form to use with this function.

< ?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
$page ="join";
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Test Page for sending an attachment by email</title>
</head>                            
<body>
 
  <p><a href="mailto:ContactEmail@example.com">contactEmail@example.com</a>
 
  <form action="mail.php" method="post" enctype="multipart/form-data">
 
      <fieldset>
 
          <label for="name">Name</label>
          <input type="text" id="name" name="name" value="" />
 
          <label for="tel">Phone</label>
          <input type="text" id="tel" name="tel" value="" />
 
          <label for="email">Email</label>
          <input type="text" id="email" name="email" value="" />
 
          <label for="message">Message</label>
          <textarea id="message" name="message"></textarea>
 
          <a href="#" id="attach">Attach</a>
 
          <label for="uploadfile">Choose a file</label>
          <input type="file" name="uploadfile" id="uploadfile" />
 
          <input type="submit" class="submit" value="Send" />  
 
          <input type="hidden" name="submitted" value="true" />
 
      </fieldset>
 
      < ?php if($err!="") {?><div class="err">< ?php echo $err ?></div>< ?php } ?>
 
  </form>
</p></body>
</html>