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:
<?php 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.
Example:
<?php $Msg = '';   if(isset($_FILES["file"]["name"]) && isset($_POST['email'])) {     $toemail = $_POST['email'];     $name = $_POST['name'];     $subject = $_POST['subject'];     $message = $_POST['message'];     $fromemail = "hosth914@gmail.com";     $msg = '<h2 style="color:green;">neveropen</h2>                     <p><b>Hello, '.$name.'</b></p></br>                     <p><b>Message:</b>'.$message.'</p>';                           $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(             file_get_contents($_FILES["file"]["tmp_name"])));              $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" .             "--{$mime_boundary}--\n";     }        if(mail($toemail, $subject, $msg, $headers)){         $Msg= "Email send successfully with attachment";     }else{         $Msg= "Email Not sent";     } } ?>    <!DOCTYPE html> <html>    <head>     <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>     <script src=     </script>     <script src=     </script>     <style>         form {             box-shadow: 10px 10px 40px grey;             padding: 50px;             margin: 20px;         }     </style> </head>    <body>     <?php if(!empty($Msg)){ ?>         <p class="text-success text-center">             <?php echo $Msg; ?>         </p>     <?php } ?>           <form method="post" action=""            enctype="multipart/form-data"            class="w-75 mx-auto">         <h1 class="text-success text-center">             neveropen         </h1>                   <h5 class="text-success text-center">             Sending email with a             file attachment         </h5>                   <div class="form-group">             <input type="text" name="name"                class="form-control"                placeholder="Name" required="">         </div>                   <div class="form-group">             <input type="email" name="email"                class="form-control"                placeholder="Email address" required="">         </div>                   <div class="form-group">             <input type="text" name="subject"                class="form-control"                placeholder="Subject" required="">         </div>                   <div class="form-group">             <textarea name="message"                class="form-control"                placeholder="Write your message here..."                required="">             </textarea>         </div>                   <div class="form-group">             <input type="file" name="file">         </div>                   <div class="submit text-center">             <input type="submit" name="submit"                class="btn btn-success "                value="SEND MESSAGE">         </div>     </form> </body>    </html> |
Output:
- 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
SMTP=smtp.gmail.com smtp_port=587 sendmail_from = gmail-id@gmail.com 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:
[sendmail] smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=gmail-id@gmail.com auth_password=gmail-password force_sender=gmail-id@gmail.com
- Step 4: Change gmail-id@gmail.com 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.

