How to attach file with feedback form in PHP ?

Feedback form is used to get the review from the user through mail services. Mailing is one of the server-side utilities that is required in most of the web servers today. In PHP, mail() is the built-in function that is used to send emails from PHP scripts either in a plain-text form or formatted HTML. You can also write a script to attach any files into your mail from send Attachment With Email article.

The PHP mail function has the following basic Syntax:

mail($to_email_address, $subject, $message, [$headers], [$parameters]);

Attaching file in feedback form: To send an email with attachment as feedback, we need to use the multipart/mixed MIME type(set Content-type header to multipart/mixed) that specifies that mixed types will be included in the email. Moreover, we want to use a multipart/alternative MIME type to send both plain-text and HTML versions of the email. Text and attachment sections can be specified within boundaries. A boundary is started with two hyphens followed by a unique number that can not appear in the message part of the email. A PHP function md5() is used to create a 32 digit hexadecimal number to create a unique number. A final boundary denoting the email’s final section must also end with two hyphens.

To include an attachment to our message, we read the data from the specified file into a string, encode it with base64_encode() function for safer transmission, split it in smaller chunks with the chunk_split() function to make sure that it matches the MIME specifications and then include it as an attachment.






$Msg = '';
if(isset($_FILES["file"]["name"]) && isset($_POST['email'])) {
    $toemail = $_POST['email'];
    $name = $_POST['name'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $fromemail "";
    $msg = '<h2 style="color:green;">GeeksforGeeks</h2>
                    <p><b>Hello, '.$name.'</b></p></br>
    $msg = "Please find the attachment<br/>Thank You.";
    $s_m = md5(uniqid(time()));
    $headers = "From: ".$fromemail;
    $mime_boundary = "==Multipart_Boundary_x{$s_m}x";
    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";
    if($_FILES["file"]["name"]!= ""){  
        $file_name = $_FILES["file"]["name"];  
        $content = chunk_split(base64_encode(
        $msg .= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" .
            "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
            "Content-Transfer-Encoding: 7bit\n\n" .
            $msg .= "\n\n";
        $msg .= "--{$mime_boundary}\n" .
            "Content-Type: application/octet-stream;\n" .
            " name=\"{$file_name}\"\n" .
            // "Content-Disposition: attachment;\n" .
            // " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
            $content  .= "\n\n" .
    if(mail($toemail, $subject, $msg, $headers)){
        $Msg= "Email send successfully with attachment";
        $Msg= "Email Not sent";
<!DOCTYPE html>
    <title>Sending file attachment with email</title>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    <script src=
    <script src=
        form {
            box-shadow: 10px 10px 40px grey;
            padding: 50px;
            margin: 20px;
    <?php if(!empty($Msg)){ ?>
        <p class="text-success text-center">
            <?php echo $Msg; ?>
    <?php } ?>
    <form method="post" action=""
            class="w-75 mx-auto">
        <h1 class="text-success text-center">
        <h5 class="text-success text-center">
            Sending email with a
            file attachment
        <div class="form-group">
            <input type="text" name="name"
                placeholder="Name" required="">
        <div class="form-group">
            <input type="email" name="email"
                placeholder="Email address" required="">
        <div class="form-group">
            <input type="text" name="subject"
                placeholder="Subject" required="">
        <div class="form-group">
            <textarea name="message"
                placeholder="Write your message here..."
        <div class="form-group">
            <input type="file" name="file">
        <div class="submit text-center">
            <input type="submit" name="submit"
                class="btn btn-success "
                value="SEND MESSAGE">



  • Feedback form GUI:
  • After receiving mail:

Using mail() on localhost on XAMPP: Sending mail from the localhost can be done using Sendmail package, Sendmail package is inbuild in XAMPP.

  • Step 1: In C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
  • Step 2: In php.ini file find [mail function] and change
    sendmail_from =
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
  • Step 3: In C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code:
  • Step 4: Change and gmail-password as required. Restart the server the XAMMP control panel so the changes take effect.
  • Step 5: Create a php file and send mail.

Note: However, the PHP mail() function is a part of the PHP core but you need to set up a mail server on your machine to make it really work.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.