title: How to send email to particular person on submit button click using php and imap?
body: <p>I used gmail imap function to get all my emails, and saved my all emails to database. my all the attachments to a folder name attachment. it all runs successfully. now i want to send email to a particular user on submit button click. and i want when i click on send button, the row against the send button email will send to the user i mentioned in To:-. and if the email has any attachment the attachment also sent to user. i used a code, but it did not work.</p>
  <p>please help</p>
  <pre><code>
      &lt;h1 align=&quot;center&quot;&gt;Gmail Email Inbox using PHP with IMAP&lt;/h1&gt;
      &lt;?php
          include('header.php');
          if (! function_exists('imap_open'))
          {
              echo &quot;IMAP is not configured.&quot;;
              exit();
          } else {
              ?&gt;
      &lt;div id=&quot;listData&quot; class=&quot;list-form-container&quot;&gt;
          &lt;?php
      
      // include_once('lib/class.imap.php');
      
      // $email = new Imap();
      $connection = imap_open('{imap.gmail.com:993/imap/ssl}INBOX','myemail@gmail.com','mypassword');
  
              $emailData = imap_search($connection, 'All');       
              if (! empty($emailData)) {
                  ?&gt;
                      &lt;div class=&quot;container&quot;&gt;
          &lt;table class=&quot;table table-striped  mail&quot;&gt;
                  &lt;thead&gt;
                  &lt;tr class=&quot;justify-content-center&quot;&gt;
                      &lt;th&gt;From&lt;/th&gt;
                      &lt;th&gt;Subject&lt;/th&gt;
                      &lt;th&gt;Date&lt;/th&gt;
                      &lt;th&gt; Action &lt;/th&gt;
                      &lt;/tr&gt;
                  &lt;/thead&gt;
              &lt;?php
              $one=&quot;&quot;;
              $two=&quot;&quot;;
              foreach ($emailData as $emailIdent) {
                      $overview = imap_fetch_overview($connection, $emailIdent, 0);
                      $message = imap_fetchbody($connection, $emailIdent,'1'); 
                      // $attachment = imap_fetchstructure($connection, $emailIdent, '8');              
                      $partialMessage = trim(quoted_printable_decode($message));
                      $date = date(&quot;d F, Y&quot;, strtotime($overview[0]-&gt;date));
                              ?&gt;
                              &lt;tbody&gt;
                          &lt;tr&gt;
                          &lt;td&gt;&lt;span class=&quot;column&quot;&gt;
                                  &lt;?php echo $overview[0]-&gt;from; 
                                  $one = $overview[0]-&gt;from;
                                  ?&gt;
                          &lt;/span&gt;&lt;/td&gt;
                          &lt;td class=&quot;content-div&quot;&gt; &lt;span class=&quot;column&quot;&gt;
                                  &lt;?php echo $overview[0]-&gt;subject; 
                                      $two = $overview[0]-&gt;subject;
                                  ?&gt;  &lt;/td&gt;
                          &lt;/span&gt;  &lt;td&gt; &lt;span class=&quot;date&quot;&gt;
                                  &lt;?php echo $date; ?&gt;
                          &lt;/span&gt; &lt;/td&gt;
                                      &lt;td&gt;
                                          &lt;input type=&quot;submit&quot; value=&quot;Send&quot; name=&quot;sendmail&quot; /&gt; 
                                      &lt;/td&gt;
                          &lt;!-- &lt;td class=&quot;column&quot;&gt;
                              &lt;?php print_r($message); ?&gt;
                          &lt;/td&gt; --&gt;
                      &lt;/tr&gt;
                          &lt;/tbody&gt;
                      &lt;?php
      
                      $structure = imap_fetchstructure($connection, $emailIdent);  
                      $attachments = array();
                      
                      if(isset($structure-&gt;parts) &amp;&amp; count($structure-&gt;parts)) 
                      {
                      for($i = 0; $i &lt; count($structure-&gt;parts); $i++) 
                      {
                      $attachments[$i] = array(
                          'is_attachment' =&gt; false,
                          'filename' =&gt; '',
                          'name' =&gt; '',
                          'attachment' =&gt; ''
                      );
      
                      if($structure-&gt;parts[$i]-&gt;ifdparameters) 
                      {
                          foreach($structure-&gt;parts[$i]-&gt;dparameters as $object) 
                          {
                              if(strtolower($object-&gt;attribute) == 'filename') 
                              {
                                  $attachments[$i]['is_attachment'] = true;
                                  $attachments[$i]['filename'] = $object-&gt;value;
                              }
                          }
                      }
      
                      if($structure-&gt;parts[$i]-&gt;ifparameters) 
                      {
                          foreach($structure-&gt;parts[$i]-&gt;parameters as $object) 
                          {
                              if(strtolower($object-&gt;attribute) == 'name') 
                              {
                                  $attachments[$i]['is_attachment'] = true;
                                  $attachments[$i]['name'] = $object-&gt;value;
                              }
                          }
                      }
      
                      if($attachments[$i]['is_attachment']) 
                      {
                          $attachments[$i]['attachment'] = imap_fetchbody($connection, $emailIdent, $i+1);
      
                          /* 3 = BASE64 encoding */
                          if($structure-&gt;parts[$i]-&gt;encoding == 3) 
                          { 
                              $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
                          }
                          /* 4 = QUOTED-PRINTABLE encoding */
                          elseif($structure-&gt;parts[$i]-&gt;encoding == 4) 
                          { 
                              $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                          }
                      }
                  }
              }
                  $filename=&quot;&quot;;
              foreach($attachments as $attachment)
              {
                  if($attachment['is_attachment'] == 1)
                  {
                      $filename = $attachment['name'];
                      if(empty($filename)) $filename = $attachment['filename'];
      
                      if(empty($filename)) $filename = time() . &quot;.dat&quot;;
                      $folder = &quot;attachment&quot;;
                      if(!is_dir($folder))
                      {
                           mkdir($folder);
                      }
                      $fp = fopen(&quot;./&quot;. $folder .&quot;/&quot;. $emailIdent . &quot;-&quot; . $filename, &quot;w+&quot;);
                      fwrite($fp, $attachment['attachment']);
                      fclose($fp);
                  }
              }
      
              $servername = &quot;localhost&quot;;
              $username = &quot;root&quot;;
              $password = &quot;&quot;;
              $dbname = &quot;email&quot;;
              $conn = new mysqli($servername, $username, $password, $dbname);
                  if ($conn-&gt;connect_error) {
                      die(&quot;Connection failed: &quot; . $conn-&gt;connect_error);
                  }   
      
                  $query= &quot;insert into access (sender,date,subject,body,attachments) values ('$one','$date','$two','$message','$filename')&quot;;
                  $result = mysqli_query($conn,$query);
              }
                          if(isset($_POST['sendmail'])) 
                          {
                             $one;
                             $date;
                             $two;
                             $message;
                             $filename;
      
                             $email_to = &quot;senderemail@gmail.com&quot;;
                             $header = 'MIME-Version: 1.0' . &quot;\r\n&quot;;
                             $header.= 'From: NAME &lt;parteek.gwebs@gmail.com&gt;' . &quot;\r\n&quot;;
                             $header.= 'Content-type: text/html; charset=iso-8859-1' . &quot;\r\n&quot;;
                             $header.= 'X-Priority: 3 \r\n';
      
                             mail($email_to,$one,$two,$message,$filename,$headers);
                          }
      
                // End foreach
                  ?&gt;
          &lt;/table&gt;
              &lt;/div&gt;
          &lt;?php
              }         
              // end if        
              imap_close($connection);
          }
          ?&gt;
      &lt;/div&gt;
      
      &lt;script&gt;
          $(document).ready( function () 
          {
          $('.mail').DataTable();
          });
      &lt;/script&gt;
  
  </code></pre>