title: PHPMailer Ajax: File Attachment
body: <p>I'm trying to send an attachment via email with Phpmailer, but I get the error: <strong>Could not access file: pdf_consenso/</strong>.<br>
  What am I doing wrong ?<br>
  File attachments aren't included in the email sent by PHPMailer when submitting the form via AJAX.</p>
  <p>Here is the PHP Mailer:</p>
  <pre><code>&lt;?php
     
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
     
  require 'PHPMailer/Exception.php';
  require 'PHPMailer/PHPMailer.php';
  require 'PHPMailer/SMTP.php';
  
  $codPre = $_POST['codPre'];
  
  try {
     if (isset($_FILES['pdf_consenso']['name']) &amp;&amp; $_FILES['pdf_consenso']['name'] != &quot;&quot;) {
        $file = &quot;pdf_consenso/&quot; . basename($_FILES['pdf_consenso']['name']);
        move_uploaded_file($_FILES['pdf_consenso']['tmp_name'], $file);
     } else
        $file = &quot;&quot;;
  
  
  $mail = new PHPMailer();
  
  $mail-&gt;isSMTP(); // Set mailer to use SMTP
  $mail-&gt;SMTPDebug = 2; // Enable verbose debug output
  //$mail-&gt;CharSet = 'UTF-8';
  $mail-&gt;Host = ; // Specify main and backup SMTP servers
  $mail-&gt;SMTPAuth = true; // Enable SMTP authentication
  $mail-&gt;Username = ; // SMTP username
  $mail-&gt;Password = ; // SMTP password
  $mail-&gt;SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
  $mail-&gt;Port = 587; // TCP port to connect to
  
  $mail-&gt;addAttachment($file);
  
  $mail-&gt;setFrom('');
  $mail-&gt;addAddress(''); // Add a recipient
  $mail-&gt;Subject = &quot;Consenso Informato - Cod.Prenotazione: &quot; . $codPre;
  $mail-&gt;Body =&quot;Consenso Informato&quot;;
  $mail-&gt;AltBody = &quot;Consenso Informato&quot;;
  
  if(!$mail-&gt;Send())
  {
  echo &quot;Message could not be sent. &lt;p&gt;&quot;;
  echo &quot;Mailer Error: &quot; . $mail-&gt;ErrorInfo;
  exit;
  }
  
  echo &quot;&lt;script&gt;alert('Message has been sent')&lt;/script&gt;&quot;;
  }
  catch (Exception $e) {
     echo &quot;Message could not be sent. Mailer Error: {$mail-&gt;ErrorInfo}&quot;;
    }
  </code></pre>
  <p>Here is the contact form :</p>
  <pre><code>&lt;form id=&quot;uploadFile&quot; enctype=&quot;multipart/form-data&quot;&gt;
    &lt;div class=&quot;field has-addons&quot;&gt;
      &lt;p class=&quot;control&quot;&gt;
        &lt;input class=&quot;input&quot; id=&quot;codPre&quot; type=&quot;text&quot; placeholder=&quot;Codice prenotazione&quot;&gt;
      &lt;/p&gt;
    &lt;/div&gt;
    &lt;input type=&quot;file&quot; id=&quot;pdf_consenso&quot; name=&quot;pdf_consenso&quot; hidden&gt;
    &lt;label class=&quot;button&quot; for=&quot;pdf_consenso&quot;&gt;Scegli file&lt;/label&gt;
      &lt;span id=&quot;file-chosen&quot;&gt;Nessun file scelto&lt;/span&gt;
    &lt;footer class=&quot;modal-card-foot&quot;&gt;
      &lt;button type=&quot;submit&quot; class=&quot;button is-rounded bt-send&quot;&gt;&lt;strong&gt;INVIA DOCUMENTO&lt;/strong&gt;&lt;/button&gt;
      &lt;button class=&quot;button is-rounded bt-erase&quot;&gt;ANNULLA&lt;/button&gt;
    &lt;/footer&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Here is AJAX Call:</p>
  <pre><code>    // FORM DI CONTATTO - PAGINA DOVE SIAMO
  $(&quot;#uploadFile&quot;).submit(function (event) {
      // cancels the form submission
      event.preventDefault();
      submitForm();
    });
    
    function submitForm() {
       //Initiate Variables With Form Content
       //Creazione di un oggetto FormData…
       var datiForm = new FormData();
       //… aggiunta del nome…
       datiForm.append('codPre',$(&quot;#codPre&quot;).val());
  
       //… aggiunta del file
       datiForm.append('pdf_consenso',$('#pdf_consenso')[0].files[0]);
       //Toast messaggio in uscita
       
       iziToast.show({
        color: 'light',
          timeout: false,
        icon: 'fas fa-paper-plane',
        message: '&lt;strong&gt;Invio!&lt;/strong&gt;&lt;/br&gt;Il tuo documento sta per essere inviato...',
        messageLineHeight: 20,
        messageSize: 15,
        position: 'center', // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter
        progressBarColor: '#00909D',
        backgroundColor: ' #9ad3ca',
        titleColor: '#222',
        messageColor: '#222',
        iconColor: '#F7F7F7',
        closa: false,
        displayMode: 'once',
        pauseOnHover: false,
        resetOnHover: false,
      });
    
      $.ajax({
        type: &quot;POST&quot;,
        url: &quot;fileUpload.php&quot;,
        data: datiForm,
        cache: false,
        processData: false,
        contentType: false,
        success: function (data) {
          console.log(data);
          if (data == 'Message sent') {
            iziToast.destroy();
            formSuccess();
          }
        }
      });
    }
    
    function formSuccess() {
      $(&quot;#uploadFile&quot;)[0].reset();
      // custom toast
      iziToast.show({
        color: 'light',
        icon: 'fas fa-paper-plane',
        message: '&lt;strong&gt;Grazie!&lt;/strong&gt;&lt;/br&gt;Il tuo messaggio è stato inviato',
        messageLineHeight: 20,
        messageSize: 15,
        position: 'center', // bottomRight, bottomLeft, topRight, topLeft, topCenter, bottomCenter
        progressBarColor: '#00909D',
        backgroundColor: ' #9ad3ca',
        titleColor: '#222',
        messageColor: '#222',
        iconColor: '#F7F7F7',
        closa: false,
        displayMode: 'once',
        pauseOnHover: false,
        resetOnHover: false,
      });
    }
  </code></pre>