title: How to pass on a file from jQuery to phpMailer as an attachment?
body: <pre><code>$(".feedback input.sent").click(function(e) {
    e.preventDefault();
    var name = $(".feedback textarea.name").val();
    var mail = $(".feedback textarea.mail").val();
    var phone = $("feedback textarea.phone").val();
    // the file field probably has to be here somewhere as well?
    var info = $(".feedback textarea.info").val();
  
    if (name &amp;&amp; mail &amp;&amp; info) {
      var data = {name: name, mail: mail, phone: phone, info: info};
      // I guess I need to somehow get the file itself to this array to pass it to phpMailer?
  
      $.post(baseUrl + "templates/feedback.php", data, function() {
        $(".contact-form input, .contact-form textarea").val("");
          console.log("great");
      });
    }
    else {
      console.log("not great");
    }
  });
  </code></pre>
  
  <p>Obviously I need to add multiple attachments, but I do not know how to pass them to the <code>$mail-&gt;AddAttachment($target_path);</code> which is what I need help with.</p>
  
  <p>I am using <a href="http://malsup.com/jquery/form/" rel="nofollow">this</a> to add multiple files.</p>
  
  <p>My phpMailer works well until I add the AddAttachment() there, then it won't send an e-mail. I added a link for a picture there for example. It doesn't give me any errors as well, which is why I came here.</p>
  
  <p>My primary need is still passing the what ever file(s) it is from the form to my feedback.php file, where the mailer does it's thing and attaching it/them all to the e-mail.</p>
  
  <p>feedback.php file:</p>
  
  <pre><code>&lt;?php
  
  require_once "phpmailer.inc.php";
  
  if ($_POST['name'] &amp;&amp; $_POST['info']) {
    $message = "";
    $message .= "Name:\n" . $_POST['name'] . "\n\n";
    $message .= "E-Mail:\n" . $_POST['mail'] . "\n\n";
    $message .= "Phone:\n" . $_POST['phone'] . "\n\n";
    $message .= "Message:\n" . $_POST['info'] . "\n\n";
  
    $mail = new PHPMailer();
  
    $mail-&gt;IsSMTP();
    $mail-&gt;Host     = "xxx.xxx.xxx";
  
    $mail-&gt;Port     = 25;
  
    $mail-&gt;From     = "xxx@xxx.xxx";
  
    $mail-&gt;AddAddress("xxx@xxx.xxx");
  
  
    foreach($_FILES as $key =&gt; $file){
      $target_path = "uploads/";
      $target_path = $target_path .basename($file['name']);
  
      if(move_uploaded_file($file['tmp_name'], $target_path)) {
      echo "the file ".basename($file['name'])." has been uploaded";
      }
      else {
       echo "there was an error";
      }
      $mail-&gt;AddAttachment($target_path);
    }
  
    $mail-&gt;Subject  = "Feedback";
    $mail-&gt;Body     = $message;
    $mail-&gt;WordWrap = 50;
  
    if(!$mail-&gt;Send()) {
      echo 'Error.';
      echo 'Problem: ' . $mail-&gt;ErrorInfo;
    } else {
      echo 'Good';
    }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>I am really not sure about the Attachment part there, I found it from here somewhere, but using it without the fancy loop doesn't work as well...</p>