Using PHP to Attach File to Email

I could use help from an experienced programmer - I have PHP code that I’m using with a registration form (that works fine, sending emails). I’ve added a file input field to the HTML form:


<form action="php/event_reg_attachment.php" enctype="multipart/form-data" method="post" name="EventRegistration" id="EventRegistration">
    <input type="file" name="Userfile" id="Userfile" name="Userfile" size="29" />
</form>

(I’m leaving out the extraneous details.)

And the PHP code that processes things:


<?php

include '../../includes/connect.php'; // Change as appropriate

if(!empty($_POST['First']) || !empty($_POST['Last']) || !empty($_POST['Email']) || !empty($_POST['Street']))
{
    $from = "[email protected]";
    $to = "[email protected]";

    $subject = "mydomain.com: " . stripslashes($_POST['EventName']) . " Event Registration";
    
    $body = stripslashes($_POST['EventName']) . " (" . stripslashes($_POST['EventDate']) . ") Registration

";
    $body .= stripslashes($_POST['First']) . " " . $_POST['Last'] . "
";
    $body .= stripslashes($_POST['Organization']) . "
";
    $body .= stripslashes($_POST['Street']) . ", " . stripslashes($_POST['Suite']) . "
";
    $body .= stripslashes($_POST['City']) . ", " . stripslashes($_POST['State']) . "
";
    $body .= stripslashes($_POST['Country']). "
";
    $body .= stripslashes($_POST['Zipcode']). "
";
    $body .= "
---------------------------

";
    $body .= "Email:
";
    $body .= stripslashes($_POST['Email']). "
";
    $body .= "
---------------------------

";
    $body .= "Home Telephone:
";
    $body .= stripslashes($_POST['HomePhone']). "
";
    $body .= "Mobile Telephone:
";
    $body .= stripslashes($_POST['MobilePhone']). "
";
    $body .= "Fax:
";
    $body .= stripslashes($_POST['Fax']). "
";
    $body .= "
---------------------------

";
    $body .= "Level:
";
    $body .= stripslashes($_POST['Level']). "
";
    $body .= "Experience/Years:
";
    $body .= stripslashes($_POST['Experience']). "
";
    $body .= "
---------------------------

";
    $body .= "Interests:
";
    $body .= stripslashes($_POST['Interests']);
    $body .= "
---------------------------

";

    $header = "From: " . $from . " <" . $from . ">
";
    $header .= "Reply-To: " . $_POST['First'] . " " . $_POST['Last'] . " <" . $_POST['Email'] . ">
";
    $header .= "Content-type: text/plain; charset=us-ascii
";
    $header .= "Content-Transfer-Encoding:7bit
";
    //$header .= "X-Mailer: PHP/" . phpversion() . "
"; //GoDaddy Disallows This Header
    $header .= "X-Priority: 1";
    if(@mail($to, $subject, $body, $header))
    {
        header( 'Location: http://www.mydomain.com/thankyou.html' ) ;
        
    } else {
        print ('<font size=\"2\">Output Error: Please be sure to fill in all form fields.<br>');
        print("<a href=\"javascript:history.go(-1);\">Return to the form</a></font>");
    }
} else {
        print ('<font size=\"2\">Error! Form variables not received. Please contact the website <a href=\"mailto:[email protected]\">administrator</a>.<br>');
        print("<a href=\"javascript:history.go(-1);\">Return to the form</a></font>");
}

?>

I need to modify it by adding file attachment capability. I’ve found some code that relates to the issue but I’m not familiar with the problems in dealing with a multipart message, so I’m not entirely sure how to combine the two scripts…


   global $Userfile, $userfile_type, $userfile_name, $userfile_size;
  
   $mail_parts["Userfile"] = $_POST['Userfile'];
   $mail_parts["userfile_type"] = $userfile_type;
   $mail_parts["userfile_name"] = $userfile_name;
   $mail_parts["userfile_size"] = $userfile_size;
   
   if($userfile_size > 0)
   {
      $mail_boundary = md5(uniqid(time()));
      $header .= "MIME-Version: 1.0
";
      $header .= "Content-type: multipart/mixed;
                                      boundary=\"$mail_boundary\"

";
      $header .= "This is a multi-part message in MIME format.

";

      $fp = fopen($Userfile, "r");
      $file = fread($fp, filesize($Userfile));
      $file = chunk_split(base64_encode($file));

      $body = "--$mail_boundary
";
      $body .= "Content-type:text/plain;charset=$mail_charset
";
      $body .= "Content-transfer-encoding:$mail_encoding

";
      $body .= "$mail_body
";
      $body .= "--$mail_boundary
";
      if(!empty($userfile_type)) $mime_type = $userfile_type;
      else $mime_type = "application/octet-stream";

      $body .= "Content-type:$mime_type;name=$userfile_name
";
      $body .= "Content-transfer-encoding:base64

";
      $body .= $file . "

";
      $body .= "--$mail_boundary--";
      $mail_body = $body;
   }

Thanks for any help.